在特定条件下计算和交换某些列中的结果
calculate and swap result in some columns on certain condition
各位极客们,
我有一个 table ACNT 是这样的
P_date P_Supplier P_parti P_opnbal P_Credit P_Debit P_Remaining
NULL varsha opening 2000 0 0 2000
2014-01-25 varsha purchase 0 500 0 500
NULL nipun opening 1000 0 0 1000
2015-01-28 nipun purchase 0 200 0 200
2016-01-25 varsha purchase 0 350 0 350
现在我触发此查询以获取总和或所有列
SELECT P_sname, SUM(P_opnbal) AS opneningbal, SUM(P_credit) AS credit, SUM(P_debit) AS debit, SUM(P_opnbal) + SUM(P_credit) - SUM(P_debit) AS closingbal
FROM ACNT
GROUP BY P_sname
ORDER BY P_sname DESC
所以我得到了这个结果
P_Supplier P_opnbal P_Credit P_Debit CLossing
varsha 2000 850 0 2850
nipun 1000 200 0 1200
但现在我希望在特定日期范围内计算此列。
例如。
如果我想要 2015-01-28
和 2016-01-28
之间的数据。
那么日期 2014-01-28
的 table 中存在的所有数据应显示为期初余额,如果给定日期范围内没有条目,贷方应为 0。
在第一个 table 中看到,该行有日期 2014-01-28
那么它的 P_credit
值应该加上它的 P_opnbal
值 i.i 2000 。所以结果应该显示 2500 作为期初余额。
期望的结果应该是
Supplier Opnbal credit Debit closing balance
varsha 2500 350 0 2850
nipun 1000 200 0 1200
我知道我需要不止 1 个查询,我正处于学习阶段,这就是我尝试过的
SELECT P_sname, opneningbal + credit AS openingbal, credit, debit, closingbal
FROM
(SELECT TOP (100) PERCENT P_sname, SUM(P_opnbal) AS opneningbal, SUM(P_credit) AS credit, SUM(P_debit) AS debit, SUM(P_opnbal) + SUM(P_credit)- SUM(P_debit) AS closingbal
FROM ACNT
WHERE (P_date BETWEEN '2015-01-25' AND '2016-01-25')
GROUP BY P_sname
ORDER BY P_sname DESC) AS abc
但我没有得到想要的结果,请帮助我改进此查询。谢谢
根据我们在 asp.net room
中的聊天记录
我发现你的最终查询应该是
SELECT P_sname, (isnull((select sum(isnull(ac.P_credit,0)) from ACNT as ac where ac.P_sname=abc.P_sname and P_date<'2015-01-25'),0)
+(select top(1) isnull(nt.p_opnbal,0) from acnt as nt where nt.p_sname=abc.P_sname and p_parti='Opening')) as opening_bal, credit, debit, ((isnull((select sum(isnull(ac.P_credit,0)) from ACNT as ac where ac.P_sname=abc.P_sname and P_date<'2015-01-25'),0)
+(select top(1) isnull(nt.p_opnbal,0) from acnt as nt where nt.p_sname=abc.P_sname and p_parti='Opening'))+(credit-debit)) as closingBal
FROM (SELECT TOP (100) PERCENT P_sname, SUM(P_opnbal) AS opneningbal, SUM(P_credit) AS credit, SUM(P_debit) AS debit, SUM(P_opnbal) + SUM(P_credit)
- SUM(P_debit) AS closingbal, P_date
FROM ACNT
GROUP BY P_sname, P_date
ORDER BY P_sname DESC) AS abc
WHERE (P_date BETWEEN '2015-01-25' AND '2016-01-25')
提示:有更多的改进范围scaler 函数 可以最小化您的查询,您也可以仅通过一个 select 查询来做到这一点
谢谢。
各位极客们, 我有一个 table ACNT 是这样的
P_date P_Supplier P_parti P_opnbal P_Credit P_Debit P_Remaining
NULL varsha opening 2000 0 0 2000
2014-01-25 varsha purchase 0 500 0 500
NULL nipun opening 1000 0 0 1000
2015-01-28 nipun purchase 0 200 0 200
2016-01-25 varsha purchase 0 350 0 350
现在我触发此查询以获取总和或所有列
SELECT P_sname, SUM(P_opnbal) AS opneningbal, SUM(P_credit) AS credit, SUM(P_debit) AS debit, SUM(P_opnbal) + SUM(P_credit) - SUM(P_debit) AS closingbal
FROM ACNT
GROUP BY P_sname
ORDER BY P_sname DESC
所以我得到了这个结果
P_Supplier P_opnbal P_Credit P_Debit CLossing
varsha 2000 850 0 2850
nipun 1000 200 0 1200
但现在我希望在特定日期范围内计算此列。
例如。
如果我想要 2015-01-28
和 2016-01-28
之间的数据。
那么日期 2014-01-28
的 table 中存在的所有数据应显示为期初余额,如果给定日期范围内没有条目,贷方应为 0。
在第一个 table 中看到,该行有日期 2014-01-28
那么它的 P_credit
值应该加上它的 P_opnbal
值 i.i 2000 。所以结果应该显示 2500 作为期初余额。
期望的结果应该是
Supplier Opnbal credit Debit closing balance
varsha 2500 350 0 2850
nipun 1000 200 0 1200
我知道我需要不止 1 个查询,我正处于学习阶段,这就是我尝试过的
SELECT P_sname, opneningbal + credit AS openingbal, credit, debit, closingbal
FROM
(SELECT TOP (100) PERCENT P_sname, SUM(P_opnbal) AS opneningbal, SUM(P_credit) AS credit, SUM(P_debit) AS debit, SUM(P_opnbal) + SUM(P_credit)- SUM(P_debit) AS closingbal
FROM ACNT
WHERE (P_date BETWEEN '2015-01-25' AND '2016-01-25')
GROUP BY P_sname
ORDER BY P_sname DESC) AS abc
但我没有得到想要的结果,请帮助我改进此查询。谢谢
根据我们在 asp.net room
中的聊天记录我发现你的最终查询应该是
SELECT P_sname, (isnull((select sum(isnull(ac.P_credit,0)) from ACNT as ac where ac.P_sname=abc.P_sname and P_date<'2015-01-25'),0)
+(select top(1) isnull(nt.p_opnbal,0) from acnt as nt where nt.p_sname=abc.P_sname and p_parti='Opening')) as opening_bal, credit, debit, ((isnull((select sum(isnull(ac.P_credit,0)) from ACNT as ac where ac.P_sname=abc.P_sname and P_date<'2015-01-25'),0)
+(select top(1) isnull(nt.p_opnbal,0) from acnt as nt where nt.p_sname=abc.P_sname and p_parti='Opening'))+(credit-debit)) as closingBal
FROM (SELECT TOP (100) PERCENT P_sname, SUM(P_opnbal) AS opneningbal, SUM(P_credit) AS credit, SUM(P_debit) AS debit, SUM(P_opnbal) + SUM(P_credit)
- SUM(P_debit) AS closingbal, P_date
FROM ACNT
GROUP BY P_sname, P_date
ORDER BY P_sname DESC) AS abc
WHERE (P_date BETWEEN '2015-01-25' AND '2016-01-25')
提示:有更多的改进范围scaler 函数 可以最小化您的查询,您也可以仅通过一个 select 查询来做到这一点
谢谢。