简体   繁体   中英

SQL RETURNING SEVERAL IDENTICAL ROWS - “The query requires remerging summary statistics back with the original data.” -

Probably my question is a little dumb but I have been looking for some videos and instructions but couldn't´t find an answer.

I am running the SQL code below under SaS environment. I am getting more than 8k identical rows. I am using "group by" on all data that are not summarized.

Thanks guys,

PROC SQL; 
CREATE TABLE WORK.TARIFAS AS

SELECT
    T1.NR_DOC_SIS_OGM AS CONVENIO,
    T1.CD_CLI_VCLD_CT_OGM AS MCI,
    T2.SEGMENTO AS SEGMENTO,
    SUM(T1.VL_OPR_CBR_TARF) FORMAT=COMMAX19.2 AS SUM_VL_COBRADO
    
FROM DB2TFA.CBR_TARF_REC T1
    INNER JOIN WORK.CONVENIOS T2 ON (T2.CONVENIO = T1.NR_DOC_SIS_OGM)

WHERE
    T1.NR_CBR_TARF_AGPD = 0
    AND T1.CD_PRD_CBR_TARF IN (15)
    AND T1.CD_SPDT_CBR_TARF IN (3,6)
    AND T1.DT_EFTC_CBR_TARF BETWEEN '01OCT2020'D AND '31OCT2020'D
    AND CONVENIO = 86081

GROUP BY
    CONVENIO,
    MCI,
    SEGMENTO;

QUIT;

The problem is the CONVENIO is also a column in a table -- which is clear because it is in the WHERE clause. Hence the SELECT keys and GROUP BY keys do not match.

So, use expressions rather than aliases:

GROUP BY T1.NR_DOC_SIS_OGM T1.CD_CLI_VCLD_CT_OGM, T2.SEGMENTO 

If they are completely identical, you can solve it by specifying select distinct instead of select :

PROC SQL; 
CREATE TABLE WORK.TARIFAS AS
SELECT DISTINCT
   T1.NR_DOC_SIS_OGM AS CONVENIO,
...

Are you sure your SELECT only lists the three group variables and the one summary statistic? If so then perhaps PROC SQL does not think the three variables listed in the GROUP BY are the same three variables you have in the SELECT.

You can use position number in the GROUP BY and avoid risking confusion caused by typos.

group by 1,2,3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM