2017-07-25 4 views
0

ピボットテーブルを取得して、行の末尾に総計を表示しようとしています。これは可能ですか?しかし、私はそれを働かせることができません。私はカウントとデータがありますが、私は各行の合計を取得することはできません。これを達成する別の方法がありますか?どんな支援も大歓迎です。ピボットテーブルの総数を取得する方法

select sch.name, s.grade, s.schid, count(s.schid), 
     (select count(ss.schid) from students ss 
     where ss.enroll_status=0 
      and ss.schid = s.schid 
     group by ss.schid) as Total 
from students s 
inner join schools sch on 
s.schid = sch.schloc 
where s.enroll_status=0 
group by sch.name,s.grade, s.schid 
order by sch.name,s.schid, s.grade; 

これは、これはそれがピボットテーブルに表示する必要がある方法です、それは

|School   |Grade |Loc  |Count |Total | 
|Amery Middle |7  |2740  |233 | 813 | 
|Amery Middle |8  |2740  |218 | 813 | 
|Porter Elem  |3  |12830 |2  | 68 | 
|Porter Elem  |4  |12830 |2  | 68 | 

を表示する方法です:カウント数を示す列の等級と行の学校、その後、総計全体学校の総入学のため

|    |count|count|count|count|count |count|Grand Total| 
|School   |3rd |4th |5th |6th |7th |8th |   | 
|Amery Middle |2 |2 |0 |0 |0  |0 |868  | 
|Porter Elem  |0 |0 |0 |0 |233 |218 |813  | 

は、提供された支援をいただき、ありがとうございます。

答えて

0

あなたはこのようにそれを行うことができます。これは、総計列が(つまり、すべての生徒が学年になっていること)全学年の生徒の合計であり、すべてのグレードが持っていることを前提とし

SELECT NAME, 
     "Count 3rd", 
     "Count 4th", 
     "Count 5th", 
     "Count 6th", 
     "Count 7th", 
     "Count 8th", 
     "Count 3rd" + 
     "Count 4th" + 
     "Count 5th" + 
     "Count 6th" + 
     "Count 7th" + 
     "Count 8th" "Grand Total" 
FROM (SELECT sch.name, 
       s.grade, 
       s.schid 
     FROM students s 
       INNER JOIN schools sch ON s.schid = sch.schloc 
     WHERE s.enroll_status = 0) 
PIVOT (COUNT(*) FOR (grade) IN (3 AS "Count 3rd", 
           4 AS "Count 4th", 
           5 AS "Count 5th", 
           6 AS "Count 6th", 
           7 AS "Count 7th", 
           8 AS "Count 8th")); 

を選択されました。

+0

ありがとうございます...これは魅力的でした! – teelee

+0

あなたのアシスタントをありがとう...別のものを選択する方法があるかどうか知っていますか?いずれかのクエリで「DISTINCT」を選択すると、すべてのデータが削除され、各グレードの列に「1」が表示されます。私はちょっとカウントを投げているいくつかの重複を得ています。何か助けてくれてありがとう! – teelee

+0

あなたはおそらくこれを新しい質問として尋ね、データと期待される出力を完成させるべきです(この質問でやったように、btw!)。 – Boneist

関連する問題