T-Sql组/和查询问题
这是一个糟糕的星期一,我不能正确思考。有人能帮我找出如何对返回的行进行分组/求和,以便只有一个AssessorParcelNumber实例吗 因此,不使用以下结果集:T-Sql组/和查询问题,sql,tsql,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-
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
救命啊!谢谢
考虑到结果集的格式和缺少列标题,很难说清楚,但我的第一个猜测是,您的GROUP BY需要取消使用cd.Name1和use SUMCASE。。。在你的专栏里 对于列列表,您可以尝试以下操作:
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中删除名称。假设SQL Server 2005或更高版本:
我将使用您当前的查询作为CTE,然后根据该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
看起来您需要对各个余额列求和
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
问题源于在GROUPBY子句中包含cd.Name1。在您列出的结果集中,我在这里做了一些假设,您得到了cd.Name=Association和RRFS各一行,groupby将其搅动成两行。将该列从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;
我试过SUMCASE。。。但出现了错误:无法对包含聚合或子查询的表达式执行聚合函数。我添加了代码,以显示我认为SUMCASE。。。将需要设置。实际上,您应该将现有的总和拉出一个级别,而不仅仅是在上面添加一个总和。希望这个例子现在已经很清楚了。
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;
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;