Sql server 数据年型sql
我试着每年获取数据。我试过这个问题Sql server 数据年型sql,sql-server,count,Sql Server,Count,我试着每年获取数据。我试过这个问题 select year(SStartDate) as joinedyear , count(*) joined_total from Detail where client = 81 Group By YEAR(StartDate) union all select year(SEndDate) as leftyear, count(*) left_total from
select
year(SStartDate) as joinedyear ,
count(*) joined_total
from
Detail
where
client = 81
Group By
YEAR(StartDate)
union all
select
year(SEndDate) as leftyear,
count(*) left_total
from
Detail
where
client = 81
Group By
YEAR(SEndDate)
这显示了正确的数据,但这显示了如下数据
1900 12
2001 1
2012 3
2013 3
2016 45
1900 23
2002 34
2004 34
2015 1
2016 56
我想要这样的数据
joinedyear joined_total leftyear left_total
1900 12 1900 45
2001 1 2002 34
2012 3 2004 34
2013 3 2015 1
2016 45 2016 56
尝试下面的查询..它可以帮助您
select * from (
select year(SStartDate) as joinedyear ,
count(*) joined_total from Detail
where client=81
Group By YEAR(StartDate)
) as a
full outer join
(
select year(SEndDate) as leftyear , count(*) left_total from Detail
where client=81
Group By YEAR(SEndDate)
) as b
on a.joinedyear=b.leftyear
我不想使用union all。尝试使用内部联接来联接表。