T-Sql组/总和查询问题

时间:2010-10-18 17:43:58

标签: sql tsql

这是一个糟糕的星期一上午,我不能直接思考。有人可以帮我弄清楚如何对返回的行进行分组/求和,以便只有一个AssessorParcelNumber实例?

所以,而不是以下结果集:

140-31-715-164  3545    2004-09-14 00:00:00.000 1665.00 0.00    0.00    1665.00
140-31-715-164  3545    2004-09-14 00:00:00.000 0.00    534.00  0.00    534.00
140-31-715-037  3546    2004-03-11 00:00:00.000 120.00  0.00    0.00    120.00
140-31-715-037  3546    2004-03-11 00:00:00.000 0.00    0.00    0.00    0.00

我得到了这个:

140-31-715-164  3545    2004-09-14 00:00:00.000 1665.00 534.00  0.00    2199.00
140-31-715-037  3546    2004-03-11 00:00:00.000 120.00  0.00    0.00    120.00

帮助!谢谢!


select
      u.AssessorParcelNumber,
      c.CollectionKey AS [r_number],
      c.Closed,
      CASE cd.Name1 WHEN 'Association'
            THEN CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) ELSE 0 END AS [assoc_balance],
      CASE cd.Name1 WHEN 'RRFS' 
            THEN CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) ELSE 0 END AS [rr_balance],
      CASE cd.Name1 WHEN 'RRFS' THEN 0 WHEN 'Association' THEN 0
            ELSE CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) END AS [_balance],
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance]
from
      Unit u with(nolock)
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5
where
      t.Credit = 0 -- is a charge
      and t.Voided = 0 -- is not voided
      -- and u.AssessorParcelNumber = '140-31-715-164'
group by
      u.AssessorParcelNumber, c.CollectionKey, c.closed, cd.Name1
order by
      c.CollectionKey,
      cd.Name1;

5 个答案:

答案 0 :(得分:4)

看起来您想要汇总各种余额列。

SELECT
      t.AssessorParcelNumber,
      t.[r_number],
      t.Closed,
      SUM([assoc_balance]),
      SUM([rr_balance]),
      SUM([_balance]),
      SUM([balance])
    FROM (/* Insert your original query here */) t
    GROUP BY t.AssessorParcelNumber, t.r_number, t.Closed

答案 1 :(得分:1)

假设SQL Server 2005或更高版本:

我将您当前的查询用作CTE,然后查询/分组。即:

;With CTE AS(


select
      u.AssessorParcelNumber,
      c.CollectionKey AS [r_number],
      c.Closed,
      CASE cd.Name1 WHEN 'Association'
            THEN CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) ELSE 0 END AS [assoc_balance],
      CASE cd.Name1 WHEN 'RRFS' 
            THEN CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) ELSE 0 END AS [rr_balance],
      CASE cd.Name1 WHEN 'RRFS' THEN 0 WHEN 'Association' THEN 0
            ELSE CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) END AS [_balance],
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance]
from
      Unit u with(nolock)
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5
where
      t.Credit = 0 -- is a charge
      and t.Voided = 0 -- is not voided
      -- and u.AssessorParcelNumber = '140-31-715-164'
group by
      u.AssessorParcelNumber, c.CollectionKey, c.closed, cd.Name1
order by
      c.CollectionKey,
      cd.Name1)


SELECT AssessorParcelNumber, 
r_number, 
Closed, 
SUM(Assoc_balance) AS 'Assoc_Balance',
SUM(rr_balance) AS 'rr_balance',
SUM(_balance) AS '_balance',
SUM(balance) AS 'balance'
FROM CTE
GROUP BY AssessorParcelNumber, r_number, Closed

答案 2 :(得分:0)

考虑到结果集的格式和列标题的缺失,很难判断,但我的第一个猜测是你的GROUP BY需要消除cd.Name1的使用并使用SUM(CASE...)你的专栏。

对于列列表,您可以尝试:

CAST(SUM(CASE cd.Name1
             WHEN 'Association' THEN t.Amount - t.AppliedAmount
             ELSE 0
         END) AS DECIMAL(18, 2)) AS [assoc_balance],
CAST(SUM(CASE cd.Name1
             WHEN 'RRFS' THEN t.Amount - t.AppliedAmount
             ELSE 0
         END) AS DECIMAL(18, 2)) AS [rr_balance],

CAST(SUM(CASE cd.Name1
             WHEN 'RRFS' THEN 0
             WHEN 'Association' THEN 0
             ELSE t.Amount - t.AppliedAmount
         END) AS DECIMAL(18, 2)) AS [_balance],

此外,您还需要从ORDER BY中删除该名称。

答案 3 :(得分:0)

select
      u.AssessorParcelNumber,
      c.CollectionKey AS [r_number],
      c.Closed,
      [assoc_balance]=CONVERT(dec(18,2),SUM(CASE WHEN cd.Name1='Association' THEN t.Amount - t.AppliedAmount ELSE 0 END)),
      [rr_balance]=CONVERT(dec(18,2),SUM(CASE WHEN cd.Name1='RRFS' THEN t.Amount - t.AppliedAmount ELSE 0 END)),
      [_balance]=CONVERT(dec(18,2),SUM(CASE WHEN cd.Name1='RRFS' THEN t.Amount - t.AppliedAmount ELSE 0 END)),
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance]
from
      Unit u with(nolock)
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5
where
      t.Credit = 0 -- is a charge
      and t.Voided = 0 -- is not voided
      -- and u.AssessorParcelNumber = '140-31-715-164'
group by
      u.AssessorParcelNumber, c.CollectionKey, c.closed, cd.Name1
order by
      c.CollectionKey,
      cd.Name1;

答案 4 :(得分:0)

问题源于在GROUP BY子句中包含cd.Name1。在你列出的结果集中(我在这里做了一些假设),你为cd.Name =“Association”和“RRFS”分别得到一行,并将该组分为两行。将该列从GROUP BY中取出并将其移到case语句中,例如:

select 
      u.AssessorParcelNumber, 
      c.CollectionKey AS [r_number], 
      c.Closed, 
      CONVERT(dec(18,2), sum(CASE cd.Name1 WHEN 'Association' THEN t.Amount - t.AppliedAmount ELSE 0 END)) AS [assoc_balance], 
      CONVERT(dec(18,2), sum(CASE cd.Name1 WHEN 'RRFS'        THEN t.Amount - t.AppliedAmount ELSE 0 END)) AS [rr_balance], 
      CONVERT(dec(18,2), sum(CASE cd.Name1 WHEN 'RRFS' THEN 0 WHEN 'Association' THEN 0 ELSE t.Amount - t.AppliedAmount END)) AS [_balance], 
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance] 
from 
      Unit u with(nolock) 
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey 
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey 
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType 
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5 
where 
      t.Credit = 0 -- is a charge 
      and t.Voided = 0 -- is not voided 
      -- and u.AssessorParcelNumber = '140-31-715-164' 
group by 
      u.AssessorParcelNumber, c.CollectionKey, c.closed
order by 
      c.CollectionKey, 
      cd.Name1;