当前位置: 代码迷 >> Sql Server >> 查询~解决思路
  详细解决方案

查询~解决思路

热度:71   发布时间:2016-04-27 13:01:28.0
查询~
SQL code
SELECT DISTINCT a.Project_Yjdw as 移交单位, COUNT(a.Project_Id) AS 工程数量, SUM(a.Project_Mj) AS 面积, SUM(b.Docu_PageCount) AS 大图数量FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProGROUP BY a.Project_Id, a.Project_Yjdw, a.Project_Mj, b.Docu_PageCount
我希望在统计在图数量的时候给加个条件
b.Docu_PageCount=2 该怎么加~

------解决方案--------------------
SQL code
SELECT DISTINCT a.Project_Yjdw as 移交单位, COUNT(a.Project_Id) AS 工程数量, SUM(a.Project_Mj) AS 面积, SUM(b.Docu_PageCount) AS 大图数量FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProwhere b.Docu_PageCount=2GROUP BY a.Project_Id, a.Project_Yjdw, a.Project_Mj, b.Docu_PageCount
------解决方案--------------------
SQL code
SELECT DISTINCT a.Project_Yjdw as 移交单位, COUNT(a.Project_Id) AS 工程数量, SUM(a.Project_Mj) AS 面积, SUM(b.Docu_PageCount) AS 大图数量FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProGROUP BY a.Project_Id, a.Project_Yjdw, a.Project_Mj, b.Docu_PageCounthaving b.Docu_PageCount=2
------解决方案--------------------
那你先把子查询写出来,再把子查询当做表去关联
------解决方案--------------------
加個HAVING SUM(b.Docu_PageCount)=2
------解决方案--------------------
SQL code
--若是b.Docu_PageCount=2,将其加在WHERE后SELECT DISTINCT a.Project_Yjdw as 移交单位, COUNT(a.Project_Id) AS 工程数量, SUM(a.Project_Mj) AS 面积, SUM(b.Docu_PageCount) AS 大图数量FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProwhere b.Docu_PageCount=2GROUP BY a.Project_Id, a.Project_Yjdw, a.Project_Mj, b.Docu_PageCount--若是sum(b.Docu_PageCount)=2,将其加在having后SELECT DISTINCT a.Project_Yjdw as 移交单位, COUNT(a.Project_Id) AS 工程数量, SUM(a.Project_Mj) AS 面积, SUM(b.Docu_PageCount) AS 大图数量FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProGROUP BY a.Project_Id, a.Project_Yjdw, a.Project_Mj, b.Docu_PageCounthaving sum(b.Docu_PageCount)=2
  相关解决方案