我有一个返回5行,包含14列的查询。这5行是子记录,我需要根据parentID将它们滚动到Parent。我需要将每个列的结果相加。下面是我运行的返回我的5行的查询。我不确定是否有一种方法可以重构这个给我一个父行,每列的所有结果都加起来,或者如果我需要做一些完全不同的事情。任何有关如何将其推广到家长的见解将不胜感激。
select
Client.ClientName as [Client Name], client.Parent,
Sum(pr.snp_dedc) AS [CS], sum(pr.snp_dedY) as [Gar],
sum(pr.snp_dedu) as [S], sum(pr.snp_dedo) as [LST],
sum(pr.snp_dedT) as [SD], sum(pr.snp_dedz) as [k],
sum(pr.snp_dedw) as [ESC], sum(pr.snp_ded7) as [ESCP],
sum(pr.snp_ded2) as [MISC2], sum(pr.snp_ded3) as [MISC3],
sum(pr.snp_dedB) as [CAB]
FROM
pr WITH (NOLOCK, INDEX(IDX_CheckDeductions))
INNER JOIN
Client with (nolock) ON pr.ClientID = Client.clientID
WHERE
pr.[snp_wkend] > '1/1/2011 12:00:00 AM'
AND pr.[snp_wkend] < '12/19/2013 11:38:26 AM'
AND (pr.snp_dedc <> 0.00 OR pr.snp_dedY <> 0.00 OR pr.snp_dedu <> 0.00 OR pr.snp_dedo <> 0.00 OR pr.snp_dedT <> 0.00 OR pr.snp_dedz <> 0.00 OR pr.snp_dedw <> 0.00 OR pr.snp_ded7 <> 0.00 OR pr.snp_ded2 <> 0.00 OR pr.snp_ded3 <> 0.00 OR pr.snp_dedB <> 0.00)
AND pr.clientid IN (SELECT client.ClientID
FROM client
WHERE client.ClientActive = 1)
AND client.clientactive = 1
AND client.parent = 71
GROUP BY
client.ClientName, client.parent
ORDER BY
client.Parent
我会显示结果,但它包含敏感数据。
期望的输出
parent client name | parent id |sum of cs | sum of gar | sum of s | sum of lst | sum of sd | sum of k | sum of esc | sum of escp | sum of misc2 | sum of misc3 | sum of cab
答案 0 :(得分:1)
您可以从ClientName
和select
条款中删除group by
:
select client.Parent,
Sum(pr.snp_dedc) AS [CS], sum(pr.snp_dedY) as [Gar],
sum(pr.snp_dedu) as [S], sum(pr.snp_dedo) as [LST],
sum(pr.snp_dedT) as [SD], sum(pr.snp_dedz) as [k],
sum(pr.snp_dedw) as [ESC], sum(pr.snp_ded7) as [ESCP],
sum(pr.snp_ded2) as [MISC2], sum(pr.snp_ded3) as [MISC3],
sum(pr.snp_dedB) as [CAB]
FROM pr WITH (NOLOCK, INDEX(IDX_CheckDeductions))
INNER JOIN Client with (nolock) ON
pr.ClientID = Client.clientID WHERE pr.[snp_wkend] > '1/1/2011 12:00:00 AM'
and pr.[snp_wkend] < '12/19/2013 11:38:26 AM' AND (pr.snp_dedc <> 0.00
OR pr.snp_dedY <> 0.00 OR pr.snp_dedu <> 0.00 OR pr.snp_dedo <> 0.00
OR pr.snp_dedT <> 0.00 OR pr.snp_dedz <> 0.00
OR pr.snp_dedw <> 0.00 OR pr.snp_ded7 <> 0.00 OR pr.snp_ded2 <> 0.00
OR pr.snp_ded3 <> 0.00
OR pr.snp_dedB <> 0.00) AND pr.clientid in (select client.ClientID
from client where client.ClientActive = 1) and client.clientactive = 1
and client.parent = 71
GROUP BY client.parent
order by client.Parent;
编辑:
哦,你只想要父母的名字。好吧,回到你的Client
表格来获取它:
select parent.ClientName as ParentName, client.Parent,
Sum(pr.snp_dedc) AS [CS], sum(pr.snp_dedY) as [Gar],
sum(pr.snp_dedu) as [S], sum(pr.snp_dedo) as [LST],
sum(pr.snp_dedT) as [SD], sum(pr.snp_dedz) as [k],
sum(pr.snp_dedw) as [ESC], sum(pr.snp_ded7) as [ESCP],
sum(pr.snp_ded2) as [MISC2], sum(pr.snp_ded3) as [MISC3],
sum(pr.snp_dedB) as [CAB]
FROM pr WITH (NOLOCK, INDEX(IDX_CheckDeductions)) INNER JOIN
Client with (nolock)
ON pr.ClientID = Client.clientID JOIN
Client parent
on client.Parent = parent.ClientId
WHERE pr.[snp_wkend] > '1/1/2011 12:00:00 AM'
and pr.[snp_wkend] < '12/19/2013 11:38:26 AM' AND (pr.snp_dedc <> 0.00
OR pr.snp_dedY <> 0.00 OR pr.snp_dedu <> 0.00 OR pr.snp_dedo <> 0.00
OR pr.snp_dedT <> 0.00 OR pr.snp_dedz <> 0.00
OR pr.snp_dedw <> 0.00 OR pr.snp_ded7 <> 0.00 OR pr.snp_ded2 <> 0.00
OR pr.snp_ded3 <> 0.00
OR pr.snp_dedB <> 0.00) AND pr.clientid in (select client.ClientID
from client where client.ClientActive = 1) and
client.clientactive = 1
and client.parent = 71
GROUP BY client.parent, parent.ClientName
order by client.Parent;