【问题标题】:Select change column value if in list如果在列表中,请选择更改列值
【发布时间】:2016-09-13 21:04:15
【问题描述】:

我正在尝试查询我的表以计算投票数,如果投票方法在列表 ['C', 'M', 'S', 'L', 'T', 'V', 'B', 'E'] 中,则将其计为 1 并将voting_method 替换为“L”。

现在我有以下查询,它返回正确的结果但不处理重复项。

select `election_lbl`, `voting_method`, count(*) as numVotes 
from `gen2014` group by `election_lbl`, `voting_method` order by `election_lbl` asc 

election_lbl voting_method numVotes
2014-09-04   M                    1
2014-09-05   M                    2
2014-09-05   S                    1
2014-09-08   C                   16
2014-09-08   M                    5
2014-09-08   S                    9
2014-09-09   10                   5
2014-09-09   C                   46
2014-09-09   M                    4
2014-09-09   S                    5
2014-09-10   C                   92
2014-0g-10   M                    3
2014-09-10   S                    7
2014-09-11   C                   96
2014-09-11   M                    3
2014-09-11   S                    2
2014-09-12   C                  104
2014-09-12   M                   10
2014-09-12   S                    3
2014-09-15   C                  243
2014-09-15   M                   18
2014-09-15   S                    3
2014-09-16   10                   1
2014-09-16   C                  161
2014-09-16   M                    4
2014-09-16   S                    3
2014-09-17   C                  157
2014-09-17   M                    5
2014-09-17   S                   12

您可以看到,对于 2014-09-05,我有两个 vote_method M 和 S 都在列表中。如果值在列表中,我希望理想的结果是删除重复的日期字段。所以应该是 2014-09-05 'L' 3。我不希望那个日期的投票消失,所以结果应该算作一个。

将查询更改为此,但 mysql 说语法错误。

select `election_lbl`, `voting_method`, count(*) as numVotes from `gen2014`
(case `voting_method` when in ('C', 'M', 'S', 'L', 'T', 'V', 'B', 'E') 
then 'L' END) group by `election_lbl`, `voting_method` order by `election_lbl` asc

表架构

CREATE TABLE `gen2014` (
 `voting_method` varchar(255) DEFAULT NULL,
 `election_lbl` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

【问题讨论】:

  • in 基本上是一个运算符,正确的语法应该是case when voting method in .... then ...;尽管我想不出任何原因,查询应该如此复杂(除非您也希望 numVotes 用于不在列表中的方法)。
  • 我实际上确实需要不在列表中的方法的 numVotes。目标是将列表中的那些算作“L”,其余的保持原样。

标签: mysql select group-by case


【解决方案1】:
select x.`election_lbl`, x.`voting_method`, count(*) as numVotes 
from (
       select `election_lbl`, 
       CASE when `voting_method` in ('C', 'M', 'S', 'L', 'T', 'V', 'B', 'E') 
            then 'L' 
            else `voting_method` 
       END as `voting_method` 
       from `gen2014`) x
group by x.`election_lbl`, x.`voting_method` 
order by x.`election_lbl` asc

【讨论】:

  • 。收到此错误。每个派生表都必须有自己的别名。
  • 我已经添加了别名。
【解决方案2】:

如果您只想在每个日期使用这些方法获得总票数,列为方法“L”,则不要在group by 中包含方法,并让SELECT 选择'L' as voting_method

select `election_lbl`, 'L' AS `voting_method`, count(*) as numVotes 
from `gen2014` 
where voting_method IN ('C', 'M', 'S', 'L', 'T', 'V', 'B', 'E')
group by `election_lbl`
order by `election_lbl` asc 

【讨论】:

  • 我也需要不在列表中的方法的 numVotes。目标是将列表中的那些算作“L”,其余的保持原样。
  • @YasinYaqoobi 看起来草莓的答案就是这样。
  • 谢谢。也许你可以解决这个问题? stackoverflow.com/questions/37104350/…
【解决方案3】:
SELECT election_lbl
     , CASE WHEN voting_method IN ('C','M','S','L','T','V','B','E')
            THEN 'L' 
            ELSE voting_method END my_voting_method
     , COUNT(*) 
  FROM my_table 
 GROUP 
    BY my_voting_method    -- or vice
     , election_lbl;       -- versa

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多