使用join和group by子句查询的%计算

时间:2013-03-14 01:00:24

标签: sql sql-server sql-server-2008

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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',