Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 数据年型sql_Sql Server_Count - Fatal编程技术网

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。尝试使用内部联接来联接表。