我有一个查询,它计算皮卡,并按客户种族和类别划分。它可以工作,但我需要计算一列'Pickups as % of Active'
,它是客户端表中活动客户端的总数(活动客户端是客户端表中 (1,2,6) 中的 StatusID,所有其他状态计为非活动状态)我的查询看起来像
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
结束它抛出错误
Msg 512, Level 16, State 1, Line 5
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Warning: Null value is eliminated by an aggregate or other SET operation.