繁体   English   中英

将查询结果添加到一行SQL

[英]Add results of a query into one row sql

我有一个查询,返回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

您可以从selectgroup by子句中删除ClientName

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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