0

お問い合わせテーブルがあり、フィールドは以下のとおりです

sno     confname         region       enquiry   status update    closed      approval
 1     Conf 2020     Madhya Pradesh     test           test      closed      reconsider
 2     Conf 2013     Maharashtra       test 123        abcde                reconsider
 3     Conf 2013     Maharashtra        test12         test      closed      approved
 4     Conf 2013     Maharashtra        test124       test1      closed      approved 
 5     Conf 3        Karnataka          test124       test1                  
 6     Conf 3        Karnataka          test876       test45       
 7     Conf 3        Karnataka          test365       test45      closed     approved

そして、私は出力が必要です

Region          Conf Name    Total No of Enq   No of Enq Closed    No of Enq Approved 
Maharashtra     Conf 2013                3                2                        2
Karnataka       Conf 3                   3                1                        1
Madhya Pradesh  Conf 2020                1                1  

そして私のSQLクエリは

SELECT confname,
       region,
       clsed ,
       apprstatus ,
       Count(region) AS totenq,
       Count(clsed) AS totenqclsd ,
       COUNT(apprstatus) AS totenqapproved
FROM [enquiries_dtls]
GROUP BY region,
         confname,
         clsed,
         apprstatus
4

1 に答える 1

0

集計関数をCASE式で使用して、結果を取得できます。

select region,
  confname,
  count(*) TotalEnquiry,
  sum(case when closed is not null then 1 else 0 end) TotalClosed,
  sum(case when approval = 'approved' then 1 else 0 end) TotalApproved
from enquiries_dtls
group by region, confname

デモで SQL Fiddle を参照してください

編集、これらの列に基づいて別の値を計算する必要がある場合は、サブクエリを使用します。

select region,
  confname,
  TotalEnquiry,
  TotalClosed, 
  TotalApproved,
  TotalEnquiry - TotalApproved 
from
(
  select region,
    confname,
    count(*) TotalEnquiry,
    sum(case when closed is not null then 1 else 0 end) TotalClosed,
    sum(case when approval = 'approved' then 1 else 0 end) TotalApproved
  from enquiries_dtls
  group by region, confname
) src

デモで SQL Fiddle を参照してください

于 2013-03-21T10:53:17.137 に答える