在同一天查询多个交易

时间:2017-01-17 08:51:12

标签: sql sql-server sql-server-2012

我正在使用sql server 2012 ..我必须在每一行上获得运行平衡..现在我使用下面的查询

 CREATE TABLE #Test(ID INT,TransDate datetime,Credit MONEY,[Debit] money)
        INSERT INTO #Test
        SELECT 1,'01/01/2017',10000,NULL UNION    
        SELECT 1,'01/05/2017',40000,NULL UNION
        SELECT 1,'01/05/2017',200,NULL UNION
        SELECT 1,'01/05/2017',200,NULL UNION   
        SELECT 1,'01/09/2017',NULL,45000  UNION
        SELECT 2,'01/05/2017',1000,NULL UNION
        SELECT 2,'01/06/2017',1000,NULL 


        SELECT t1.ID,convert(varchar,t2.TransDate,103) 'date', 
        t2.Credit, 
        t2.Debit, 
        SUM(COALESCE(t1.credit, 0) - COALESCE(t1.debit, 0)) AS Balance
        FROM #Test t1 
        INNER JOIN #Test t2
            ON t1.TransDate <= t2.TransDate

        GROUP BY t1.ID,t2.TransDate  , t2.Credit, t2.Debit

输出:

1   01/01/2017  10000.00    NULL    10000.00
1   05/01/2017  200.00      NULL    50200.00
1   05/01/2017  1000.00     NULL    50200.00
1   05/01/2017  40000.00    NULL    50200.00
1   06/01/2017  1000.00     NULL    50200.00
1   09/01/2017  NULL      45000.00   5200.00
2   05/01/2017  200.00      NULL     1000.00
2   05/01/2017  1000.00     NULL     1000.00
2   05/01/2017  40000.00    NULL     1000.00
2   06/01/2017  1000.00      NULL    2000.00
2   09/01/2017  NULL      45000.00   2000.00

要求输出:

输出:

1   01/01/2017  10000.00    NULL    10000.00
1   05/01/2017  200.00      NULL    10200.00
1   05/01/2017  1000.00     NULL    11200.00
1   05/01/2017  40000.00    NULL    50200.00
1   06/01/2017  1000.00     NULL    51200.00
1   09/01/2017  NULL      45000.00   5200.00
2   05/01/2017  200.00      NULL      200.00
2   05/01/2017  1000.00     NULL     1200.00
2   05/01/2017  40000.00    NULL     41200.00
2   06/01/2017  1000.00      NULL    42200.00

但是如果相同的转换日查询的多个条目不起作用(参见第2,第3和第4行结果)..它应该不相同..请任何人帮我获得正确的结果?

2 个答案:

答案 0 :(得分:1)

使用window function。如果日期列包含datetime ...

,这将有效
select id, 
       TransDate, 
       credit, 
       debit, 
       sum(coalesce(credit,0)-coalesce(debit,0)) 
           over (partition by id order by TransDate) as RunningBalance
from #Test

如果日期列仅为日期,请使用:

with t1 as
(
select id, 
       TransDate, 
       credit, 
       debit, 
       row_number() over(partition by id, Transdate order by credit) as t_ord
from #Test
)
select id, 
       TransDate, 
       credit, 
       debit,
       sum(coalesce(credit,0)-coalesce(debit,0)) 
           over (partition by id order by TransDate, t_ord) as RunningBalance
from t1

答案 1 :(得分:1)

首先按日期获取所需列和grop的日期:

select A.TransDate, SUM(COALESCE(credit, 0)) as credit, SUM(COALESCE(debit, 0)) as debit, SUM(COALESCE(credit, 0) - COALESCE(debit, 0)) AS Balance from
(
SELECT convert(varchar,t2.TransDate, 103) as TransDate,  t2.Credit,     t2.Debit  
FROM Test t1 
INNER JOIN Test t2
    ON t1.TransDate <= t2.TransDate
) as  A group by A.TransDate

=================或者===============

使用临时表:

select A.TransDate, SUM(COALESCE(credit, 0)) as credit, SUM(COALESCE(debit, 0)) as debit, SUM(COALESCE(credit, 0) - COALESCE(debit, 0)) AS Balance from
(
SELECT convert(varchar,t2.TransDate, 103) as TransDate,  t2.Credit,     t2.Debit  
FROM Test #t1 
INNER JOIN #Test t2
    ON t1.TransDate <= t2.TransDate
) as  A group by A.TransDate