按范围计数仅返回一行

时间:2013-02-11 22:51:15

标签: mysql sql

我试着在不同的CALL上使用输出。 我想要的输出是

Row   |   Col   |  Data  |  Seq

<70%  | Voters  |  5519  |  8
<80%  | Voters  |  4352  |  8
<90%  | Voters  |  3251  |  8
100%  | Voters  |  2511  |  8

我的代码是

SELECT CASE
        WHEN (poll_propensity >= 0.70 AND perm_absentee = 'N' AND mail_propensity = 0)  THEN '< 70%' 
        WHEN (poll_propensity >= 0.80 AND perm_absentee = 'N' AND mail_propensity = 0)  THEN '< 80%' 
        WHEN (poll_propensity >= 0.90 AND perm_absentee = 'N' AND mail_propensity = 0)  THEN '< 90%'
        WHEN (poll_propensity >= 1 AND perm_absentee = 'N' AND mail_propensity = 0)     THEN '100%' 
       END AS Row,            

'Voters' AS Col,
COUNT(voter_id) AS Data,
8 AS Seq

FROM (  SELECT p.voter_id, p.perm_absentee, vp.poll_propensity, vp.mail_propensity 
        FROM VoterProfile p
             INNER JOIN DistrictPrecinct d ON d.precinct_id=p.precinct_code 
             INNER JOIN VoterPropensity vp ON p.voter_id=vp.voter_id        
        WHERE d.district_id= 3 AND vp.election_type= 'T' ) as derived 
GROUP BY Row

但是我得到的输出就像

Row   |   Col   |  Data  |  Seq

<70%  | Voters  |  5519  |  8

如果有人可以提供帮助,那么只有一个会成功

2 个答案:

答案 0 :(得分:2)

简单地更改WHEN语句的顺序,因为1&gt; .7你需要以.7开头的.7然后下到.7。

SELECT CASE
        WHEN (poll_propensity >= 1 AND perm_absentee = 'N' AND mail_propensity = 0)     THEN '100%' 
        WHEN (poll_propensity >= 0.90 AND perm_absentee = 'N' AND mail_propensity = 0)  THEN '< 90%'
        WHEN (poll_propensity >= 0.80 AND perm_absentee = 'N' AND mail_propensity = 0)  THEN '< 80%' 
        WHEN (poll_propensity >= 0.70 AND perm_absentee = 'N' AND mail_propensity = 0)  THEN '< 70%' 
       END AS Row,            

'Voters' AS Col,
COUNT(voter_id) AS Data,
8 AS Seq

FROM (  SELECT p.voter_id, p.perm_absentee, vp.poll_propensity, vp.mail_propensity 
        FROM VoterProfile p
             INNER JOIN DistrictPrecinct d ON d.precinct_id=p.precinct_code 
             INNER JOIN VoterPropensity vp ON p.voter_id=vp.voter_id        
        WHERE d.district_id= 3 AND vp.election_type= 'T' ) as derived 
GROUP BY Row

或者,您可以将操作从&gt; =更改为&lt;并保持相同的顺序;但是我不确定那会给你你寻找的相同子集。

答案 1 :(得分:0)

看起来你想要从这样的语句返回结果集:

SELECT r.Row
     , 'Voters' AS Col
     , CASE r.Row
         WHEN '< 70%' THEN IFNULL(d.c70,0)
         WHEN '< 80%' THEN IFNULL(d.c80,0)
         WHEN '< 90%' THEN IFNULL(d.c90,0)
         WHEN '100%' THEN IFNULL(d.c100,0)
       END AS Data
     , 8 AS Seq
  FROM ( SELECT '< 70%' AS Row
         UNION ALL SELECT '< 80%' 
         UNION ALL SELECT '< 90%'
         UNION ALL SELECT '100%' 
       ) r
 CROSS
  JOIN ( SELECT SUM(vp.poll_propensity >= 0.70 AND vp.perm_absentee = 'N' AND vp.mail_propensity = 0) AS c70
              , SUM(vp.poll_propensity >= 0.80 AND vp.perm_absentee = 'N' AND vp.mail_propensity = 0) AS c80
              , SUM(vp.poll_propensity >= 0.90 AND vp.perm_absentee = 'N' AND vp.mail_propensity = 0) AS c90
              , SUM(vp.poll_propensity >= 1    AND vp.perm_absentee = 'N' AND vp.mail_propensity = 0) AS c100
              , COUNT(p.voter_id) AS count_voter_id
           FROM VoterProfile p
           JOIN DistrictPrecinct d ON d.precinct_id=p.precinct_code
           JOIN VoterPropensity vp ON p.voter_id=vp.voter_id
          WHERE d.district_id = 3
            AND vp.election_type = 'T'
       ) d
 ORDER BY r.Row

(您使用比较>= 0.70然后使用'< 70%'标签的标签有点奇怪。看起来您可能希望进行<= 0.70比较。

根据您的数据,您似乎希望聚合计数。 poll_propensity为.85的投票人应包括在多个计数中。 (你没有具体说明,但这就是你的预期输出对我的意思。)

如果情况并非如此,如果你想在给定百分比之间选民只有选民,我只会调整条件测试以检查具体范围,例如:

SUM(vp.poll_propensity >= 0.80 AND vp.poll_propensity < 0.90 
     AND vp.perm_absentee = 'N' AND vp.mail_propensity = 0) AS c80