Sql group records

4

I have the following query:

select T1.dt_producao, T1.cod_turno, T1.cod_periodo,
    CASE
    WHEN T2.TP_PALLET = 'DZ' THEN
      sum(T1.QT_PROD* T2.QT_EMBALAGEM*12)
    ELSE
     sum(T1.QT_PROD* T2.QT_EMBALAGEM)
    END AS "APON.TOTAL",
    (select sum(T3.qt_entregue) from tb_romaneio_ovos T3
    where (T3.dt_romaneio = T1.dt_producao)
    and (T3.cod_turno >= T1.cod_turno) and (T3.cod_periodo = T1.cod_periodo)) as "QT.ENTREGUE",
    (select sum(T3.clas_totalovos) from tb_romaneio_ovos T3
   where (T3.dt_transf = T1.dt_producao)
   and (T3.cod_turno >= T1.cod_turno) and (T3.cod_periodo = T1.cod_periodo)) as "QT.CLASSIFICADO",
   (select sum(T3.clas_totalovostrin) from tb_romaneio_ovos T3
   where (T3.dt_transf = T1.dt_producao)
    and (T3.cod_turno >= T1.cod_turno) and (T3.cod_periodo = T1.cod_periodo)) as "QT.TRINCADOS"
    from tb_movimento T1, tb_cadastro T2
    where
    (T1.COD_TURNO >= 0)AND
    (T1.DT_PRODUCAO BETWEEN '26.09.2016' AND '05.10.2016') AND
    ((T1.COD_PERIODO >=0)) AND
    (T1.COD_SICOP = T2.COD_SICOP) AND
    (T1.COD_FILIAL = 'UDM')
    GROUP BY
    T1.DT_PRODUCAO, T1.cod_turno, T1.cod_periodo,t2.TP_PALLET

It returns me like this:

|DT_PROD |COD_TURNO|COD_PERIODO|APONT.TOTAL|QT.ENTRE|QT.CLASSI|QT.TRINCADOS|
|01.10.16|   1     |     1     |   180600  | 248630 | 185182  | 7079       |
|03.10.16|   1     |     1     |   163260  | 520173 | 387798  | 15063      |
|03.10.16|   1     |     1     |   211140  | 520173 | 387798  | 15063      |
|04.10.16|   1     |     1     |   392160  | 519086 | 400622  | 18436      |

I would like to show only one day 03, adding the "APONT.TOTAL", thus:

|DT_PROD |COD_TURNO|COD_PERIODO|APONT.TOTAL|QT.ENTRE|QT.CLASSI|QT.TRINCADOS|
|01.10.16|   1     |     1     |   180600  | 248630 | 185182  | 7079       |
|03.10.16|   1     |     1     |   374400  | 520173 | 387798  | 15063      |
|04.10.16|   1     |     1     |   392160  | 519086 | 400622  | 18436      |
    
asked by anonymous 05.10.2016 / 16:20

1 answer

4

Try using a subselect.

select dt_producao as DT_PROD ,cod_turno as COD_TURNO, cod_periodo as COD_PERIODO, sum(APONTTOTAL) as "APONT.TOTAL", 
QTENTREGUE as "QT.ENTREGUE"", QTCLASSIFICADO as "QT.CLASSIFICADO",
 QT.TRINCADOS as 'QTTRINCADOS' from 
(
    select T1.dt_producao, T1.cod_turno, T1.cod_periodo,
        CASE WHEN T2.TP_PALLET = 'DZ' THEN  sum(T1.QT_PROD* T2.QT_EMBALAGEM*12)
        ELSE sum(T1.QT_PROD* T2.QT_EMBALAGEM)
        END AS "APONTOTAL",
        (select sum(T3.qt_entregue) from tb_romaneio_ovos T3
        where (T3.dt_romaneio = T1.dt_producao)
        and (T3.cod_turno >= T1.cod_turno) and (T3.cod_periodo = T1.cod_periodo)) as "QTENTREGUE",

        (select sum(T3.clas_totalovos) from tb_romaneio_ovos T3
        where (T3.dt_transf = T1.dt_producao)
        and (T3.cod_turno >= T1.cod_turno) and (T3.cod_periodo = T1.cod_periodo)) as "QTCLASSIFICADO",

        (select sum(T3.clas_totalovostrin) from tb_romaneio_ovos T3
         where (T3.dt_transf = T1.dt_producao)
         and (T3.cod_turno >= T1.cod_turno) and (T3.cod_periodo = T1.cod_periodo)) as "QTTRINCADOS"
    from tb_movimento T1, tb_cadastro T2
    where
    (T1.COD_TURNO >= 0)AND
    (T1.DT_PRODUCAO BETWEEN '26.09.2016' AND '05.10.2016') AND
    ((T1.COD_PERIODO >=0)) AND
    (T1.COD_SICOP = T2.COD_SICOP) AND
    (T1.COD_FILIAL = 'UDM')
    GROUP BY
    T1.DT_PRODUCAO, T1.cod_turno, T1.cod_periodo,t2.TP_PALLET
)D
GROUP BY
    dt_producao ,COD_TURNO, COD_PERIODO, QTENTREGUE, QTCLASSIFICADO, QTTRINCADOS
    
05.10.2016 / 16:55