2

我试图让输出在不同的 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

如果有人可以提供帮助,那就只有一个通过,那就太好了

4

2 回答 2

2

只需更改 WHEN 语句的顺序,因为 1 > .7 将 1 与 .7 混为一谈,您需要从 .1 开始,然后下降到 .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

或者,您可以将操作从 >= 更改为 < 并保持顺序不变;但我不确定这是否会为您提供您正在寻找的相同子集。

于 2013-02-11T22:54:53.913 回答
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 为 0.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
于 2013-02-11T23:19:35.750 回答