我如何将这两个sql查询合并为一个?

时间:2013-07-10 13:11:19

标签: sql sql-server

    Select Name,
sum(case when DayAci = 'Sunday' then 1 else 0 END) as Sunday,
sum(case when DayAci = 'Monday' then 1 else 0 END) as Monday,
sum(case when DayAci = 'Tuesday' then 1 else 0 END) as Tuesday,
sum(case when DayAci = 'Wednesday' then 1 else 0 END) as Wednesday,
sum(case when DayAci = 'Thursday' then 1 else 0 END) as Thursday,
sum(case when DayAci = 'Friday' then 1 else 0 END) as Friday,
sum(case when DayAci = 'Saturday' then 1 else 0 END) as Saturday,
count(*) as Total
from Caraccident
where Accident = 'Near-miss'
group by Name;

Select Name,
count(*) as Total 
From CaraccidentPrevious
where Accident = 'Near-miss'
group by Name;

在此类表格中显示信息

Name | Sunday | Monday | Tuesday | ..... | Total | Previous Total

 Joe     0        2         1                3         5

第一个SQL语句为我提供了每天所需的数据和总计。 第二个给出了我以前的总数所需的数据。使用SQL服务器

3 个答案:

答案 0 :(得分:1)

  Select Name,
    sum(case when DayAci = 'Sunday' then 1 else 0 END) as Sunday,
    sum(case when DayAci = 'Monday' then 1 else 0 END) as Monday,
    sum(case when DayAci = 'Tuesday' then 1 else 0 END) as Tuesday,
    sum(case when DayAci = 'Wednesday' then 1 else 0 END) as Wednesday,
    sum(case when DayAci = 'Thursday' then 1 else 0 END) as Thursday,
    sum(case when DayAci = 'Friday' then 1 else 0 END) as Friday,
    sum(case when DayAci = 'Saturday' then 1 else 0 END) as Saturday,
    count(*) as Total
    (SELECT count(*) from CaraccidentPrevious
      WHERE Accident = 'Near-miss' 
        AND  name = CarAccident.name 
        AND Accident = 'Near-miss' )                  AS PreviousTotal
    group by Name;

答案 1 :(得分:1)

您可以使用子查询将结果连接在一起:

Select Name, sum(case when DayAci = 'Sunday' then 1 else 0 END) as Sunday,
       sum(case when DayAci = 'Monday' then 1 else 0 END) as Monday,
       sum(case when DayAci = 'Tuesday' then 1 else 0 END) as Tuesday,
       sum(case when DayAci = 'Wednesday' then 1 else 0 END) as Wednesday,
       sum(case when DayAci = 'Thursday' then 1 else 0 END) as Thursday,
       sum(case when DayAci = 'Friday' then 1 else 0 END) as Friday,
       sum(case when DayAci = 'Saturday' then 1 else 0 END) as Saturday,
       count(*) as Total, max(cap.prevTotal) as prevTotal
 from Caraccident ca left outer join
      (select cap.name, count(*) as prevTotal
       from CaraccidentPrev cap
       where cap.Accident = 'Near-miss'
       group by cap.name
      ) cap
      on cap.name = ca.name
 where Accident = 'Near-miss'
 group by Name;

答案 2 :(得分:0)

Select CC.Name,
sum(case when CC.DayAci = 'Sunday' then 1 else 0 END) as Sunday,
sum(case when CC.DayAci = 'Monday' then 1 else 0 END) as Monday,
sum(case when CC.DayAci = 'Tuesday' then 1 else 0 END) as Tuesday,
sum(case when CC.DayAci = 'Wednesday' then 1 else 0 END) as Wednesday,
sum(case when CC.DayAci = 'Thursday' then 1 else 0 END) as Thursday,
sum(case when CC.DayAci = 'Friday' then 1 else 0 END) as Friday,
sum(case when CC.DayAci = 'Saturday' then 1 else 0 END) as Saturday,
count(CC.*) as Total,
max(T.Total1)
from Caraccident CC
inner join (Select Name,count(*) as Total1 
            From CaraccidentPrevious
            where Accident = 'Near-miss'
            group by Name;
           )T
on T.name=CC.name
where Accident = 'Near-miss'
group by Name;