我的查询中存在一些小问题。
这是我的查询
declare @table table (
groupname varchar(50),
credit numeric(18,2),
debit numeric(18,2),
undergroup varchar(50)
)
insert into @table
values ('Sales', 1210.00, 0.00, 'Sales'),
('Sales Return', 0.00, 0.00, 'Sales'),
('Closing Stock', 1400.00, 0.00, 'Closing Stock')
SELECT ROW_NUMBER() OVER ( ORDER BY undergroup ) Cr_RN,
groupname Cr_GroupName,
ABS(Credit+Debit) Cr_Amount,
ABS(LAG(Credit, 1, 0) OVER ( ORDER BY groupname ) -Debit ) AS Cr_Total
FROM(
SELECT
GroupName GroupName,
undergroup,
SUM(ISNULL(Credit,0.00)) OVER (PARTITION BY groupname) AS Credit,
SUM(ISNULL(Debit*-1,0.00)) OVER (PARTITION BY groupname) AS debit
FROM @table
) X
我的查询输出
一些输出提示
我想要这种类型的输出
答案 0 :(得分:0)
Alpesh,您可以检查此查询吗?这样应该可以得到预期的结果。
SELECT ROW_NUMBER() OVER (ORDER BY undergroup ) Cr_RN,
groupname Cr_GroupName,
ABS(Credit+Debit) Cr_Amount,
CASE WHEN LEAD(RID,1,0)OVER ( ORDER BY undergroup ) =RID+1 THEN 0 ELSE
ABS(UG_Credit - UG_Debit ) END AS Cr_Total
FROM(
SELECT ROW_NUMBER() OVER (PARTITION BY undergroup ORDER BY undergroup ) RID,
GroupName GroupName,
undergroup,
SUM(ISNULL(Credit,0.00)) OVER (PARTITION BY groupname) AS Credit,
SUM(ISNULL(Debit*-1,0.00)) OVER (PARTITION BY groupname) AS debit,
SUM(ISNULL(Credit,0.00)) OVER (PARTITION BY undergroup) AS UG_Credit,
SUM(ISNULL(Debit*-1,0.00)) OVER (PARTITION BY undergroup) AS UG_Debit
FROM @table
) X