excluding fields in my SELECT from the GROUP BY clause

  • Hello community

    Happy 2005 for all of you

    I need to exclude on the GROUP BY clause some fields of my SELECT statment because in this example for the same 'Nome cliente' (Client Name ) this select return to me 2 lines ,one with (NDOC =1 and 4 - Invoices) and an other one with (NDOC=8 and 3 -Credit Notes), also i have the name of this documents in my SELECT that is NMDOC and they are different regarding the NDOC number.

    I´m attach my SELECT Statment and i hope someone could give me a method for excluding Select fields in the Group by Clause.

    My SELECT statment:

    select fi.ndoc,fi.nmdoc,fi.ref as 'codigo',ft.no,ft.nome as 'Nome cliente',fi.design,sum(fi.qtt) as 'Qt' ,(fi.epv-(fi.epv*(ft.fin/100))) as pv,(case when (fi.ndoc=1 or fi.ndoc=3) then fi.ecusto else case when (fi.ndoc=8 or fi.ndoc=4) then fi.ecusto else 0 end end) as 'CM',

    (select case when sum(fi.Etiliquido)=0 then 0 else sum(fi.etiliquido)-(sum(fi.etiliquido)*(ft.fin/100)) end) as 'Valor',

    (case when (fi.ndoc=1 or fi.ndoc=3) then (fi.ecusto*sum(fi.qtt)) else case when (fi.ndoc=8 or fi.ndoc=4) then (fi.ecusto*sum(fi.qtt)*-1) else 0 end end) as 'Valor Custo',

    ltrim(str(desconto))+'+'+ ltrim(str(desc2)) as 'Desc',

    (case when fi.ndoc=1 then (1-((fi.ecusto*sum(fi.qtt))/(case when sum(fi.Etiliquido)=0 then 1 else sum(fi.etiliquido)-(sum(fi.etiliquido)*(ft.fin/100) )end)))*100 else 0 end) as Margem,

    'Comissão' =(sum(fi.etiliquido)-sum(fi.etiliquido)*(ft.fin/100))*

    /*GERAL - sem tipo cliente e sem vendedor na ficha do cliente*/

    ((CASE WHEN PN.VENDEDOR  IN(SELECT ECOM.VENDEDOR FROM ECOM WHERE ECOM.VENDEDOR=10)  and pn.tipo like ' ' THEN

     (SELECT ECOM.COM1 from ecom WHERE ecom.tipocl=0 and ecom.PORVEND=0 AND ECOM.VENDEDOR=0 AND ECOM.QUALTIPO = ' ' AND PN.ECOMISSAO=ecom.ecom)

    ELSE 

    /*Especfica de vendedor*/

    CASE WHEN PN.VENDEDOR IN(SELECT ECOM.VENDEDOR FROM ECOM WHERE ECOM.VENDEDOR=10)  THEN

    (SELECT ECOM.COM1 from ecom WHERE ecom.PORVEND=1 AND ECOM.VENDEDOR=10 )

     else

    /*Tipo de Cliente com vendedor na ficha */

    CASE WHEN PN.VENDEDOR not IN(SELECT ECOM.VENDEDOR FROM ECOM WHERE ECOM.VENDEDOR=10) and pn.tipo not like ' '  then (select ecom.com1 from ecom where  ecom.tipocl=1 and ecom.porvend=0 and ecom.qualtipo=pn.tipo) 

    else

    /*Tipo de Cliente - sem vendedor na ficha*/

    CASE WHEN PN.VENDEDOR IN(SELECT ECOM.VENDEDOR FROM ECOM WHERE ECOM.VENDEDOR=10) and pn.tipo not like ' '  then (select ecom.com1 from ecom where  ecom.tipocl=1 and ecom.vendedor=0  and ecom.porvend=0 and ecom.qualtipo=pn.tipo) end END end END)/100)

    from fi,ft,pn where

    fi.ftstamp=ft.ftstamp  and fi.fistamp=pn.pnstamp and ft.no in(#1#) and fi.ref <> ' ' and (FI.STNS=0 or FI.STNS=1) and ft.fdata >=#2# and ft.fdata<=#3# and (fi.ndoc=1 or fi.ndoc=8 or fi.ndoc=3 or fi.ndoc=4)

    group by fi.ref,fi.epv,fi.ecusto,ltrim(str(desconto))+'+'+ ltrim(str(desc2)),ft.nome,fi.ndoc,fi.nmdoc,ft.no,fi.design,ft.efinv,ft.fin,pn.ecomissao,pn.vendedor,pn.tipo

    order by fi.ndoc,fi.ref asc

     

    Best regards

    Luis Santos

     

  • Hi there.

    Well, I can't begin to try understanding what is going on, or the purpose of it all, however it seems like there's a lot of calculations, searched cases and stuff in there... Very briefly, I don't think that you can change the group by  in any easy way... (At least not without further knowledge of the underlying tables and what the query should return)

    A common way, though, of 'reducing' columns, is to nest queries as virtual tables. If the query above is to be seen as an 'intermediate table', or 'temp table/result', you could use it that way. Something along these lines:

    SELECT

     

  • While I don't like temp tables, it might help here. Load some of the data into a temp table and get some calculations done there.

    You have to have all the non-aggregated columns in a select in the group by. If you need less, you'd need to rework the statement by doing the group by in a tmep table or subselect and then adding in more fields in an outer select that joins to the inner select.

  • For others bennifit and the fact it helps me understand better could you post an example of the data you are currently getting and an example of what you want to get along with the reason why? I agree with Steve a temp table might help but you might also find a subquery will sometimes do the trick.

  • Hello Luis Santos,

    I agree with Antares686, a sample output -- particularly of the two lines that you want consolidated into 1 -- would make this easier to solve.  However, I'll hazard a guess:

    You say above: "also I have the name of this documents in my SELECT --  that is NMDOC and they are different regarding the NDOC number."

    If you mean the column NMDOC contains something like 'INV' when NDOC=1, and it contains something like 'CRD' when NDOC = 8, then column NMDOC is the culprit. In fact, on closer look I see that column NDOC is also in the GROUP BY phrase.  I would remove NDOC and NMDOC from the GROUP BY phrase  (and I see that NDOC is also in the ORDER BY, so remove that too). 

    Perhaps that's your solution.


    Regards,

    Bob Monahon

  • Hello

    Replie for Antares686 as requested :

     

    NdocNmdocCodigoQtPvValorValor_custoDescMargemComissão
    1Factura             20180ANB-1        12568,3510487,67787,20+0                  25,7486
    1Factura             20180ANB-2        24008,3520040148800+0                  25,7486
    1Factura             22520ANB-1        15008,25123758572,54950+0                  30,7269
    1Factura             22520ANB-2        15008,251237585800+0                  30,6667
    1Factura             24020ANB-1        25005125008581,85250+0                  31,3452
    1Factura             24020ANB-1        31005,51705010641,49710+0                  37,5866
    1Factura             24040ANB-1        13008104006290,67010+0                  39,5128
    1Factura             24040ANB-2        12008960061200+0                  36,25
    1Factura             24040ANB-3        860868804162,40+0                  39,5
    1Factura             24040ANB-4        150081200076500+0                  36,25
    1Factura             24520ANB01-04     310061860011791,07010+0                  36,6072
    1Factura             27640ANB-1        420625201377,60+0                  45,3334
    1Factura             27640ANB-2        1290677404231,20+0                  45,3334
    1Factura             27670ANB-1        32006192009123,92960+0                  52,4796
    1Factura             27670ANB-2        22006132007026,55140+0                  46,7686
    1Factura             27670ANB-2        24006144007665,32880+0                  46,7686
    1Factura             27730ANB-1        10805,4583231860+0                  45,3704
    1Factura             27730ANB-2        8205,4442824190+0                  45,3704
    1Factura             PRV108-1          351072457018145,310040+0                  26,1486
    1Factura             PRV108-2          48007,937920278880+0                  26,4557
    1Factura             PRV108-3          70007,95530041006,8820+0                  25,8466
    4Nota de Crédito     NSINDEMNIZ        112000-12000-0,652870+0                  0-480
    4Nota de Crédito     NSRAPPEL          141217,49-41217,4900+0                  0-1648,6996
    0                                      0000                     00
    29                                      4693853362,79274200,11217126,3883                     775,8179-2128,6996

    that is the result , but i need the result like this :

    for this Client i need only one line with the SUM of "Valor" ; "Valor_custo";"Comissões"and "Margem" , where "Valor" as Sails;"Valor_custo" as Cost; "comissões" as comissions and "margem" as benefits.

    I hope you understand better what i want to do.

    Best regards

    Luis Santos

     

     

     

     

     

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply