为什么不执行?
select a.行业 ,b.户数 from 行业顺序 a left join (select YGZ_MC , count(YGZ_MC) as 户数 from temp一般人 where datediff(day, GZZSYXQ ,'2013-8-1') >=0 and datediff(day, HZDJRQ ,'2013-8-1') >0
group by YGZ_MC ) b on a.行业= b.YGZ_MC
行业顺序里面存储 A B C D
如果 where datediff(day, GZZSYXQ ,'2013-8-1') >=0 光有一个条件的话,正常应该输出为
A 10
B 11
C 100
D 6
但是现在 where datediff(day, GZZSYXQ ,'2013-8-1') >=0 and datediff(day, HZDJRQ ,'2013-8-1') >0 跟着两个条件 执行起来 跟行业顺序表就无关 程序结果就变为:
C 10
B 12
A 12
D 12
请问怎么回事
[解决办法]
最后加个"order by a.行业"子句即可.
select a.行业,
b.户数
from 行业顺序 a
left join
(select YGZ_MC,
count(YGZ_MC) as 户数
from temp一般人
where datediff(day,GZZSYXQ ,'2013-8-1')>=0 and datediff(day,HZDJRQ,'2013-8-1')>0
group by YGZ_MC) b on a.行业=b.YGZ_MC
order by a.行业