Jump to content

RENAN KAWAMOTO FAGUNDES

Members
  • Content count

    1
  • Joined

  • Last visited

Community Reputation

0 Comum

About RENAN KAWAMOTO FAGUNDES

  1. RENAN KAWAMOTO FAGUNDES

    SQL SERVER

    ! Olá, boa tarde! Estou precisando de uma ajuda em um SCRIPT (SQL). Preciso agrupar o resultado por tipo de convênio (CONVENIO). Exemplo Acima: Total por tipo de convênio. SET DATEFORMAT DMY -- RTM declare @DTINI datetime, @DTFIM datetime set @DTINI = '01/01/2019' set @DTFIM = '01/04/2019' SELECT CASE WHEN CONV_SUS = 'T' THEN 'SUS' WHEN CONV_PARTICULAR = 'T' THEN 'PARTICULAR' WHEN CONV_DS IS NOT NULL THEN 'CONVENIO' END CONVENIO, CONSUMO2.INSU_DS INSUMO, replace(CONSUMO2.EMI_QTDE_COMERCIAL,'.',',') QTDE_COMERCIAL, replace(CONSUMO2.VAL_TOTAL,'.',',') VALOR_TOTAL FROM ( SELECT CONSUMO.* FROM ( SELECT CONV_DS, CONV_SUS, CONV_PARTICULAR, INSU_DS, SUM(ISNULL(EMI_QTDE_COMERCIAL,0)) EMI_QTDE_COMERCIAL, ISNULL(SUM(ISNULL(EMI_QTDE_COMERCIAL,0) * ISNULL(PM,0)),0) VAL_TOTAL FROM ( SELECT CUREMI.*, (SELECT PCONV_DS FROM PLANO_CONVENIO PCONV WHERE PCONV.PCONV_ID = CUREMI.PCONV_ID) PCONV_DS, (SELECT CONV_DS FROM CONVENIO CONV WHERE CONV.CONV_ID = (SELECT CONV_ID FROM PLANO_CONVENIO PCONV WHERE PCONV.PCONV_ID = CUREMI.PCONV_ID) ) CONV_DS, (SELECT CONV_SUS FROM CONVENIO CONV WHERE CONV.CONV_ID = (SELECT CONV_ID FROM PLANO_CONVENIO PCONV WHERE PCONV.PCONV_ID = CUREMI.PCONV_ID) ) CONV_SUS, (SELECT CONV_PARTICULAR FROM CONVENIO CONV WHERE CONV.CONV_ID = (SELECT CONV_ID FROM PLANO_CONVENIO PCONV WHERE PCONV.PCONV_ID = CUREMI.PCONV_ID) ) CONV_PARTICULAR FROM( SELECT INSU_DS, EMI.PCONV_ID, EMI_QTDE_COMERCIAL, EMI.INSU_PRECO_MEDIO_FISCAL PM FROM EMISSAO EMI JOIN SERVICO SERV ON SERV.SERV_ID = EMI.SERV_ID JOIN INSUMO INSU ON INSU.SERV_ID = EMI.SERV_ID LEFT JOIN TIPO_EMISSAO TEMI ON EMI.TEMI_ID = TEMI.TEMI_ID LEFT JOIN ESTOQUE_LOTE ELOTE ON EMI.ELOTE_ID = ELOTE.ELOTE_ID WHERE EMI.ENTI_ID = 1 AND EMI_DT_DISPENSA >= @DTINI AND EMI_DT_DISPENSA < @DTFIM AND INSU.INSU_ID IN (255651, 243116, 243116, 246708, 562, 250466, 413, 808) UNION ALL SELECT INSU_DS, EMI.PCONV_ID, EMI_QTDE_COMERCIAL, EMI.INSU_PRECO_MEDIO_FISCAL PM FROM ITENS_EMISSAO IEMI JOIN EMISSAO EMI ON EMI.EMI_ID = IEMI.EMI_ID JOIN SERVICO SERV ON SERV.SERV_ID = IEMI.SERV_ID JOIN INSUMO INSU ON INSU.SERV_ID = IEMI.SERV_ID LEFT JOIN TIPO_EMISSAO TEMI ON EMI.TEMI_ID = TEMI.TEMI_ID LEFT JOIN ESTOQUE_LOTE ELOTE ON IEMI.ELOTE_ID = ELOTE.ELOTE_ID WHERE EMI.ENTI_ID = 1 AND EMI_DT_DISPENSA >= @DTINI AND EMI_DT_DISPENSA < @DTFIM AND INSU.INSU_ID IN (255651, 243116, 243116, 246708, 562, 250466, 413, 808) UNION ALL SELECT INSU_DS, EMI.PCONV_ID, EMI_QTDE_COMERCIAL, EMI.INSU_PRECO_MEDIO_FISCAL PM FROM ESTORNO_EMISSAO EEMI JOIN ESTORNO EST ON EEMI.EST_ID = EST.EST_ID JOIN EMISSAO EMI ON EMI.EMI_ID = EEMI.EMI_ID JOIN SERVICO SERV ON SERV.SERV_ID = EMI.SERV_ID JOIN INSUMO INSU ON INSU.SERV_ID = EMI.SERV_ID LEFT JOIN TIPO_EMISSAO TEMI ON EMI.TEMI_ID = TEMI.TEMI_ID LEFT JOIN ESTOQUE_LOTE ELOTE ON EEMI.ELOTE_ID = ELOTE.ELOTE_ID WHERE EMI.ENTI_ID = 1 AND EMI_DT_DISPENSA >= @DTINI AND EMI_DT_DISPENSA < @DTFIM AND INSU.INSU_ID IN (255651, 243116, 243116, 246708, 562, 250466, 413, 808) UNION ALL SELECT INSU_DS, EMI.PCONV_ID, EMI_QTDE_COMERCIAL, EMI.INSU_PRECO_MEDIO_FISCAL PM FROM ESTORNO_ITENS_EMISSAO ESTI JOIN ESTORNO EST ON ESTI.EST_ID = EST.EST_ID JOIN ITENS_EMISSAO IEMI ON IEMI.IEMI_ID = ESTI.IEMI_ID JOIN EMISSAO EMI ON EMI.EMI_ID = IEMI.EMI_ID JOIN SERVICO SERV ON SERV.SERV_ID = IEMI.SERV_ID JOIN INSUMO INSU ON INSU.SERV_ID = IEMI.SERV_ID LEFT JOIN TIPO_EMISSAO TEMI ON EMI.TEMI_ID = TEMI.TEMI_ID LEFT JOIN ESTOQUE_LOTE ELOTE ON ESTI.ELOTE_ID = ELOTE.ELOTE_ID WHERE EMI.ENTI_ID = 1 AND EMI_DT_DISPENSA >= @DTINI AND EMI_DT_DISPENSA < @DTFIM AND INSU.INSU_ID IN (255651, 243116, 243116, 246708, 562, 250466, 413, 808)) CUREMI ) CURCC GROUP BY INSU_DS, CONV_DS, CONV_SUS, CONV_PARTICULAR ) CONSUMO ) CONSUMO2 ORDER BY 2, 1
×

Important Information

Ao usar o fórum, você concorda com nossos Terms of Use.