带条件的VB + SQL SUM

时间:2016-06-26 04:43:46

标签: sql-server vb.net datagridview

我试图让这段代码发挥作用。

# pylint: disable=protected-access

我已经尝试了

  SELECT InputDate       
        ,Number       
        ,Name       
        ,Sector       
        ,YukyuDate       
        ,YukyuIn       
        ,YukyuOut       
        ,ISNULL(SUM(YukyuIn), 0)-ISNULL(SUM(YukyuOut), 0)As YukyuLeft
        ,Reason  
    FROM TYukyu 
   WHERE Number = '0011683963'
GROUP BY Number
ORDER BY InputDate

我的sql有这个colums

SUM(YukyuIn-YukyuOut) As YukyuLeft
SUM(YukyuIn)-SUM(YukyuOut) As YukyuLeft

我希望此结果显示在datagridview

    InputDate   |Number|Name |Sector |YukyuDate |YukyuIn|YukyuOut|Reason
-------------------------------------------------------------------------
2016-06-26 11:50|  1   |User1|Sector1|2016-06-26|   10  |        |Test1
2016-06-26 11:51|  1   |User1|Sector1|2016-06-26|       |    1   |Test2
2016-06-26 11:52|  1   |User1|Sector1|2016-06-26|       |    1   |Test3
2016-06-26 11:53|  2   |User2|Sector1|2016-06-26|   10  |    0   |Test4
2016-06-26 11:54|  1   |User1|Sector1|2016-06-26|       |    1   |Test5
2016-06-26 11:55|  3   |User3|Sector1|2016-06-26|       |    1   |Test6

1 个答案:

答案 0 :(得分:0)

试试这个

      SELECT InputDate       
                ,Number       
                ,Name       
                ,Sector       
                ,YukyuDate       
                ,YukyuIn       
                ,YukyuOut       
                ,SUM(isnull(YukyuIn,0)-isnull(YukyuOut,0)) 
over (partition by number order by InputDate) as YukyuLeft
                ,Reason  
            FROM TYukyu 
           WHERE Number = '0011683963'
        ORDER BY InputDate


    '