Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Add your test to an expression that returns 1 if successful and 0 otherwise. Then add it up and divide it by your total to get the percentage.</p> <p>You can use something like the following:</p> <pre><code>SUM(CASE WHEN c.StatusID in (1,2,6) and p.PickupDate between '2012-01-01' and '2012-01-31' THEN 1 ELSE 0 END) / COUNT(PickupID) as 'Pickups as % of Active' </code></pre> <p>EDIT: OK, I see that I was wrong the first time. You could get a count of active as a subquery instead:</p> <pre><code>SELECT max(a.AgencyName)as Agency, COUNT(PickupID)as Pickups, COUNT(PickupID) / ActiveClients AS 'Pickups as % of Active', count (CASE WHEN p.CategCode = 'NB' THEN p.CategCode END) as NB ,count (CASE WHEN p.CategCode = 'IN' THEN p.CategCode END) as 'IN' ,count (CASE WHEN p.CategCode = 'CH' THEN p.CategCode END) as 'CH' ,count (CASE WHEN p.CategCode = 'PG' THEN p.CategCode END) as 'PG' ,count (CASE WHEN p.CategCode = 'BF' THEN p.CategCode END) as 'BF' ,count (CASE WHEN p.CategCode = 'PP' THEN p.CategCode END) as 'PP' ,count (CASE WHEN p.CategCode = 'SR' THEN p.CategCode END) as 'SR' ,count (CASE WHEN p.EthnCode = 'N' THEN p.EthnCode END) as Nat ,count (CASE WHEN p.EthnCode = 'A' THEN p.EthnCode END) as Asn ,count (CASE WHEN p.EthnCode = 'B' THEN p.EthnCode END) as Blk ,count (CASE WHEN p.EthnCode = 'P' THEN p.EthnCode END) as Pac ,count (CASE WHEN p.EthnCode = 'W' THEN p.EthnCode END) as Wth ,count (CASE WHEN p.EthnCode NOT IN ('N', 'A', 'B', 'P', 'W', '0') THEN p.EthnCode END) as Nat ,count (CASE WHEN p.EthnCode = '0' THEN p.EthnCode END) as Unknown ,count (CASE when PickupHispanic=1 then PickupHispanic end) as Hispanic FROM dbo.Pickup p join Agency a on p.agencyid = a.agencyid join (select c.ClientID, COUNT( c.ClientID) AS ActiveClients FROM dbo.Pickup p JOIN Clients c on c.ClientID = p.ClientID WHERE c.StatusID in (1,2,6) and p.PickupDate between '2012-01-01' and '2012-01-31' GROUP BY c.ClientID ) ActiveClientCounts ON p.ClientID = ActiveClientCounts.ClientID where PickupDate between '2012-01-01' and '2012-01-31' group by a.AgencyID WITH ROLLUP order by 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload