2017-11-02 11 views
0

私は既に動作しているクエリに加えて、各カテゴリに1つの余分な列を追加したいと思っています。 。誰でも助けてくれますか?あなたが派生テーブルとして、クロス総数のための既存のクエリに参加SQLクエリ:パーセンテージの追加

select 
     count(case when Lunchstatus = 'P' then 1 else null end) as Paid 
     , count(case when LunchStatus = 'R' then 1 else null end) as Reduced 
     , count(case when LunchStatus = 'F' then 1 else null end) as Free 
     , count(case when LunchStatus = 'fdc' then 1 else null end) as CertifiedFree 
     , count(case when LunchStatus = 'P' then 1 
        when LunchStatus = 'fdc' then 1 
        when LunchStatus = 'R' then 1 
        when LunchStatus = 'F' then 1 
        else null 
        end) as Total 
    from students 
    where enroll_status = 0 
     and schoolid = %param1% 
+0

余分な列を自由にカテゴリごとに、縮小、あなたがachiveしたいものを支払いました..より多くのデータを追加しようとする –

+1

あなたが得る出力を表示し、あなたが望む出力を表示します – RiggsFolly

答えて

0

トリート、その後、あなたの割合を計算します。

select 
     d.* 
    , d.Paid * 100.0/cj.totcount as paid_pct 

    ... more like that 

from (
     select 
       count(case when Lunchstatus = 'P' then 1 else null end) as Paid 
      , count(case when LunchStatus = 'R' then 1 else null end) as Reduced 
      , count(case when LunchStatus = 'F' then 1 else null end) as Free 
      , count(case when LunchStatus = 'fdc' then 1 else null end) as CertifiedFree 
      , count(case when LunchStatus = 'P' then 1 
         when LunchStatus = 'fdc' then 1 
         when LunchStatus = 'R' then 1 
         when LunchStatus = 'F' then 1 
         else null 
         end) as Total 
     from students 
     where enroll_status = 0 
     and schoolid = %param1% 
    ) d 
CROSS JOIN (
     select count(*) as totcount 
     from students 
     where enroll_status = 0 
     and schoolid = %param1% 
     ) cj 
+0

あなたの質問は今解決されましたか?あなたはまだこの答えについて質問がありますか? [help/accepting](https://stackoverflow.com/help/someone-answers)の詳細については、[** Click The Tick **](https://ibb.co/ikqyO6) –

関連する問題