分组导致行数超过要求

时间:2018-09-27 12:49:18

标签: sql sql-server

我正在尝试根据代码对数据求和,但总和仍在起作用,但仍在显示单独的行。

SELECT  
    nom.AccntntCod, nom.AcctName, cc.PrcCode, cc.PrcName,
    SUM(line.Credit) AS CreditTotal,
    SUM(line.Debit) AS DebitTotal  
FROM 
    OJDT head
LEFT JOIN  
    JDT1 line ON head.TransId = line.TransId
LEFT JOIN 
    OACT nom ON line.Account = nom.AcctCode
LEFT JOIN 
    OPRC cc ON line.ProfitCode = cc.PrcCode
WHERE 
    DimCode IS NOT NULL
GROUP BY
    PrcCode, CC.DimCode, nom.AccntntCod, nom.AcctName, cc.PrcCode, cc.PrcName

如您所见,我正在尝试按Prc代码分组,但是我在这里仍然有些重复,因为我只希望一台HS07的信用总额和借方总金额相结合,非常欢迎任何想法帮助。

enter image description here

1 个答案:

答案 0 :(得分:0)

如果每个PrcCode只需要一行,则从查询中删除帐户信息:

SELECT cc.PrcCode, cc.PrcName, sum(line.Credit) AS CreditTotal, 
      sum(line.Debit) as DebitTotal 
FROM OJDT head LEFT JOIN
     JDT1 line
     on head.TransId = line.TransId LEFT JOIN
     OACT nom
     on line.Account = nom.AcctCode LEFT JOIN
     OPRC cc
     ON line.ProfitCode = cc.PrcCode
WHERE DimCode IS NOT NULL  -- what table is this in?  You should qualify it
group by cc.PrcCode, cc.PrcName;