首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

查询~解决思路

2012-05-05 
查询~SQL codeSELECT DISTINCT a.Project_Yjdw as 移交单位, COUNT(a.Project_Id) AS 工程数量, SUM(a.Pro

查询~

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 

热点排行