将项目分组为2个数字

时间:2013-02-24 16:10:15

标签: mysql

我有一个如下所示的查询:

select 
price, 
item_id,
sum(price),
count(item_id)
from transactions
group by
(price <= 20),
(price between 21 and 30),
(price between 31 and 40),
(price between 41 and 50),
(price > 50)

在我写这篇文章之前,我从未做过这样的小组我只是猜测查询是否有效,而且确实如此。但我的问题是,它真的让我得到了我想要的东西吗?

我想要按以下方式分组所有交易:

  • 费用低于或等于20美元的物品
  • 价格在21美元至30美元之间的商品
  • 价格在31美元至40美元之间的物品
  • 价格介于41美元至50美元之间的商品
  • 费用超过50美元的物品

那么,那个查询是在做我要问的吗?

3 个答案:

答案 0 :(得分:1)

SELECT 
  price, 
  item_id,
  sum(price),
  count(item_id),
  IF(price<=20,0,IF(price<=30,1,IF(price<=40,2,IF(price<=50,3,4)))) AS pricegroup
FROM transactions
GROUP BY pricegroup

甚至

SELECT 
  price, 
  item_id,
  sum(price),
  count(item_id)
FROM transactions
GROUP BY 
  IF(price<=20,0,IF(price<=30,1,IF(price<=40,2,IF(price<=50,3,4))))

答案 1 :(得分:1)

在标准SQL(和MySQL)中执行此操作的方法是使用case语句。另外,我将定义放在这样的子查询中:

select pricegrp, sum(price), count(item_id)
from (select t.*,
             (case when price <= 20 then '00-20' 
                   when price between 21 and 30 then '21-30'
                   when price between 31 and 40 then '31-40'
                   when price between 41 and 50 then '41-50'
                   when price > 50 then '50+'
             end) as pricegrp
      from transactions t
     ) t
group by pricegrp

另外,您是否也希望按item_id进行分组?或者你只是试图返回一个任意项目?根据您的需要,我将从item_id子句中删除select。这似乎没必要。

您的查询实际上在MySQL中运行,从它运行的意义上说。它将为您想要的每个组生成一行,因此从这个意义上说它“有效”。但是,在每个组中,它将选择任意价格和item_id。这些未在group by子句中明确提及,因此您使用的是名为隐藏列的MySQL(mis)功能。查询的不同运行或对数据的轻微更改或对查询的轻微更改可以更改为每个组返回的price和item_id的值。

我强烈建议您确实命名该组。这使得查询和输出更加清晰。

另外,我建议您养成将select中的所有列放在group by子句中的习惯。在某些情况下隐藏列实际上是有用的,但我认为,一般来说,你应该谨慎地依赖它们。

如果价格没有存储为整数,那么正确的逻辑是:

select pricegrp, sum(price), count(item_id)
from (select t.*,
             (case when price <= 20 then '00-20' 
                   when price <= 30 then '21-30'
                   when price <= 40 then '31-40'
                   when price <= 50 then '41-50'
                   when price > 50 then '50+'
             end) as pricegrp
      from transactions t
     ) t
group by pricegrp

答案 2 :(得分:0)

SELECT  price, 
        item_id,
        SUM(CASE WHEN price <= 20 THEN price ELSE 0 END) `(price <= 20) SUM`,
        SUM(CASE WHEN price <= 20 THEN 1 ELSE 0 END) `(price <= 20) COUNT`,
        SUM(CASE WHEN price between 21 and 30 THEN price ELSE 0 END) `(price <= 20) SUM`,
        SUM(CASE WHEN price between 21 and 30 THEN 1 ELSE 0 END) `(price <= 20) COUNT`,
        SUM(CASE WHEN price between 31 and 40 THEN price ELSE 0 END) `price between 31 and 40 SUM`,
        SUM(CASE WHEN price between 31 and 40 THEN 1 ELSE 0 END) `price between 31 and 40 COUNT`,
        SUM(CASE WHEN price between 41 and 50 THEN price ELSE 0 END) `price between 41 and 50 SUM`,
        SUM(CASE WHEN price between 41 and 50 THEN 1 ELSE 0 END) `price between 41 and 50 COUNT`,
        SUM(CASE WHEN price > 50 THEN price ELSE 0 END) `price > 50 SUM`,
        SUM(CASE WHEN price > 50 THEN 1 ELSE 0 END) `price > 50 COUNT`
FROM    transactions
GROUP   BY price, item_id