Note that there are some explanatory texts on larger screens.

plurals
  1. PO% calculation in query with join and group by clause
    primarykey
    data
    text
    <p>I have a query which counts pickups, and divides it by client ethnicity and category. It works, but I need calculate one column <code>'Pickups as % of Active'</code> which is total count of active clients in client table (Active client is StatusID in (1,2,6) in Clients table, all other statuses count as nonactive) My query looks like </p> <pre><code>SELECT max(a.AgencyName)as Agency, COUNT(PickupID)as Pickups, ( COUNT(PickupID)/(select COUNT( c.ClientID) AS ActiveClients FROM Clients c WHERE c.StatusID in (1,2,6) GROUP BY c.AgencyID)) as 'Pickups as % of Active', --SUM(CASE WHEN c.StatusID in (1,2,6) THEN 1 ELSE 0 END)/ COUNT(PickupID) 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 Clients c ON c.ClientID = p.ClientID where PickupDate between '2013-01-01' and '2013-01-31' group by a.AgencyID WITH ROLLUP order by 1 </code></pre> <p>end it throws error </p> <pre><code>Msg 512, Level 16, State 1, Line 5 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, &lt;, &lt;= , &gt;, &gt;= or when the subquery is used as an expression. Warning: Null value is eliminated by an aggregate or other SET operation. </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.
 

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