我正在尝试合并具有相似标识的行。我的桌子看起来像这样:
如果值是来自同一行的另一行中的'0.00'
,我想填写“借方”或“贷方”,然后执行Debit - Credit
计算,如下所示:
+------------+----------+---------+----------+----------+-------+
|Source | Property | ID | Debit | Credit |Balance|
+------------+----------+---------+----------+----------+-------+
|HOCV1900091 | PROP0003 | 1.01.86 | 16800.00 | 16800.00 | 0.00 |
+------------+----------+---------+----------+----------+-------+
如何?
我做到了:
SELECT
a.[Date],
a.[Source],
a.[Property],
a.[ID],
[Debit] = STUFF((SELECT DISTINCT Debit FROM #ProcSubLegder b WHERE b.Source = a.Source FOR XML PATH('')),1,2,''),
[Credit] = STUFF((SELECT DISTINCT Credit FROM #ProcSubLegder b WHERE b.Source = a.Source FOR XML PATH('')),1,2,''),
a.[Balance]
FROM #ProcSubLegder a
GROUP BY [Date],[Source],[Property],[ID],[Credit],[Balance]
但这给了我一个非常糟糕的结果。
答案 0 :(得分:0)
您应该仅按来源和属性进行汇总:
SELECT
Source,
Property,
MAX(CASE WHEN ID LIKE '%[0-9].[0-9]%' THEN ID END) AS ID,
MAX(Debit) AS Debit,
MAX(Credit) AS Credit,
MAX(Credit) - MAX(Debit) AS Balance
FROM #ProcSubLegder
GROUP BY
Source,
Property;
注意:此答案假定每个源/属性组最多具有两个记录的交易价值。如果没有,则上述答案必须更改,您应该显示实际数据。