title: SQL Query - SUM(CASE WHEN x THEN 1 ELSE 0) for multiple columns
body: <p>I'm looking to see if there is a better approach to the query below.  What I'm trying to do is create a summary report, compiling stats by date.</p>
  
  <pre><code> SELECT CAST(Detail.ReceiptDate AS DATE) AS 'DATE'
  , SUM(CASE WHEN Detail.Type = 'TotalMailed' THEN 1 ELSE 0 END) AS 'TOTALMAILED'
  , SUM(CASE WHEN Detail.Type = 'TotalReturnMail' THEN 1 ELSE 0 END) AS 'TOTALUNDELINOTICESRECEIVED'
  , SUM(CASE WHEN Detail.Type = 'TraceReturnedMail' THEN 1 ELSE 0 END) AS 'TRACEUNDELNOTICESRECEIVED'
  FROM
  (
  select SentDate AS 'ReceiptDate', 'TotalMailed' AS 'Type'
  from MailDataExtract
  where sentdate is not null
  UNION ALL
  select MDE.ReturnMailDate AS 'ReceiptDate', 'TotalReturnMail' AS 'Type'
  from MailDataExtract MDE
  where MDE.ReturnMailDate is not null
  UNION ALL
  select MDE.ReturnMailDate AS 'ReceiptDate', 'TraceReturnedMail' AS 'Type'
  from MailDataExtract MDE
      inner join DTSharedData.dbo.ScanData SD ON SD.ScanDataID = MDE.ReturnScanDataID
  where MDE.ReturnMailDate is not null AND SD.ReturnMailTypeID = 1
  ) AS Detail
  GROUP BY CAST(Detail.ReceiptDate AS DATE)
  ORDER BY 1   
  </code></pre>
  
  <p>This is only a sample of the query (which is used in a report) as there are a number of other columns and the logic for the other stats are way more complicated.  Is there a more elegant approach to getting this kind of information/writing this kind of report?</p>