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
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 ステートメントは、毎日必要なデータと合計を提供します。2 つ目は、前の合計に必要なデータを提供します。SQL サーバーの使用

4

3 に答える 3

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;
于 2013-07-10T13:18:08.280 に答える
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;
于 2013-07-10T13:18:21.660 に答える