当前位置: 代码迷 >> Sql Server >> 求高人解答SQL有关问题
  详细解决方案

求高人解答SQL有关问题

热度:59   发布时间:2016-04-24 09:59:37.0
求高人解答SQL问题
SELECT [One_project],[Two_project],[Three_project],[XM_ID],[TR_Year],SUM (S_TR) AS S_TRCount,SUM (ZY_TR) AS ZY_TRCount,SUM (HY_TR) AS HY_TRCount,SUM (QT_TR) AS QT_TRCount,SUM(TR_SUM) AS sum_TRCount,GROUPING(XM_ID),GROUPING(TR_Year) FROM [input] join Input_User on input.ID = Input_User.inputID  WHERE Input_User.HeadID =" + userID + "and XM_ID IS not NULL GROUP BY One_project,Two_project,Three_project,XM_ID,TR_Year WITH rollup HAVING XM_ID IS NOT NULL


其中Input_User表中有五条数据
,input表里有一条数据
查询的结果是
但是我想要的结果是
求高人帮我解答。


------解决思路----------------------
信息真乱,这点数据看了半天才看懂。
 你这个连接只有input.id=input_user.inputid1对多的连接。在求和肯定是2000了。
你在WHERE 后面价格input_user.id=7 1对1的连接就可以了。

SELECT [One_project],[Two_project],[Three_project],[XM_ID],[TR_Year],
 SUM (S_TR) AS S_TRCount,
 SUM (convert(int,ZY_TR)) AS ZY_TRCount,
 SUM (convert(int,HY_TR)) AS HY_TRCount,
 SUM (convert(int,QT_TR)) AS QT_TRCount,
 SUM(TR_SUM) AS sum_TRCount,GROUPING(XM_ID),GROUPING(TR_Year) 
 FROM [input] join Input_User on input.ID = Input_User.inputID  
 WHERE Input_User.HeadID =2 
 GROUP BY One_project,Two_project,Three_project,
 XM_ID,TR_Year WITH rollup 
 having xm_id is not null
  相关解决方案