且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server完全联接查询

更新时间:2023-01-29 14:12:16

我认为您需要在查询之前设置日期格式. SET DATEFORMAT dmy;
http://msdn.microsoft.com/en-us/library/ms189491.aspx [ ^ ]


I think you require to set date format before your query. SET DATEFORMAT dmy;
http://msdn.microsoft.com/en-us/library/ms189491.aspx[^]


select datename(month,dob) 'Month', sum(case when typeofpost='manager'THEN 1 ELSE 0 END) 'Man',sum(case when typeofpost='sales'THEN 1 ELSE 0 END) 'Sal'
FROM tablename
group by datename(month,dob)


我制作了表格并将样本数据插入SSMS中并运行了该查询.执行后,我得到了您想要的输出:

I made the table and inserted sample data in SSMS and ran this query. After execution I got your desired output :

select Red month, man, sal from
(select DATENAME(month,dob) as Red,count(TypeOfPost) man
from tablename
where TypeOfPost='Manager'
group by DATENAME(month,dob) ) a
full join
(select DATENAME(month,dob) as Green,count(TypeOfPost) sal
from tablename
where TypeOfPost='Sales'
group by DATENAME(month,dob) ) b on B.Green = A.Red



输出为:



output was :

month                          man         sal
------------------------------ ----------- -----------
January                        1           1
June                           1           NULL
November                       1           1



我认为结果就是您想要的结果:-?
可以吗?



I think the result is the one you wanted :-?
Is it ok ?


select datename(month,dob) 'Month', sum(case when typeofpost='manager'THEN 1 ELSE 0 END) 'Man',sum(case when typeofpost='sales'THEN 1 ELSE 0 END) 'Sal'
FROM tablename
group by datename(month,dob);




感谢prerak patel ..
干杯




Thanks to prerak patel..
Cheers