0

我有一个查询,它计算皮卡,并按客户种族和类别划分。它可以工作,但我需要计算一列'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.
4

2 回答 2

1

将您的测试添加到表达式中,如果成功则返回 1,否则返回 0。然后把它加起来除以你的总数得到百分比。

您可以使用以下内容:

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'

编辑:好的,我第一次看到我错了。您可以将活动计数作为子查询来代替:

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
于 2013-03-14T01:19:04.063 回答
1

问题是 Count 结果是一个整数。当你将一个整数除以一个整数时,它会给你一个整数作为结果。

尝试将你的分母转换为小数,它会给你一个小数作为输出:

 select 100 * COUNT(PickupID)/CAST(COUNT( c.ClientID) AS DECIMAL)

编辑:

好的,您已经更改了几次代码,这很令人困惑。

从我目前所见,您的错误来自Group By c.AgencyID子查询中的子句。这个怎么样:

( COUNT(PickupID)/(select Cast(COUNT( c.ClientID) AS DECIMAL) AS ActiveClients 
    FROM Clients c
    WHERE c.StatusID in (1,2,6) 
    AND c.AgencyID = a.AgencyID )) as 'Pickups as % of Active',

编辑2:

使用十进制结果的强制转换和 2dp 的强制转换:

CAST(( COUNT(PickupID)/(select CAST(COUNT( c.ClientID) AS DECIMAL) AS ActiveClients 
    FROM Clients c
    WHERE c.StatusID in (1,2,6) 
    AND c.AgencyID = a.AgencyID )) as Decimal(10,2)) as 'Pickups as % of Active',
于 2013-03-14T01:23:25.650 回答