Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
groupby之后列上的mysql orderby_Mysql_Group By_Sql Order By - Fatal编程技术网

groupby之后列上的mysql orderby

groupby之后列上的mysql orderby,mysql,group-by,sql-order-by,Mysql,Group By,Sql Order By,我有一个包含以下字段的表:季节、收集、产品密钥、汇总销售。以下查询未给出预期的输出 select t.* from (SELECT * FROM rank_processing.aggregate_season_product order by aggregated_sale) t group by t.collection, t.forecast_name, t.product_key; 样本输入 ss, f1, 1, 11 ss,

我有一个包含以下字段的表:季节、收集、产品密钥、汇总销售。以下查询未给出预期的输出

select
    t.* 
from
    (SELECT * FROM rank_processing.aggregate_season_product 
       order by aggregated_sale) t
group by
    t.collection,
    t.forecast_name,
    t.product_key;
样本输入

ss, f1, 1, 11
ss, f1, 3, 10
ss, f1, 2, 5
ss, f2, 5, 11
ss, f2, 4, 7
预期产量为

ss, f1, 2, 5
ss, f1, 3, 10
ss, f1, 1, 11
ss, f2, 4, 7
ss, f2, 5, 11

请注意,如果没有明确的
orderby
子句,则不需要服务器对结果进行排序,即使存在
groupby

如果要对结果进行排序,只需添加适当的
排序依据
,如下所示:

SELECT t.* 
FROM (SELECT * FROM rank_processing.aggregate_season_product 
    ORDER BY aggregated_sale) t
GROUP BY
    t.collection,
    t.forecast_name,
    t.product_key
ORDER BY
    t.collection,
    t.forecast_name,
    t.product_key

这里的另一个问题是子查询中的orderby是无用的,您甚至应该扩展该子查询以完全消除它。

为什么需要使用子查询?此查询将为您提供结果

SELECT * FROM rank_processing.aggregate_season_product 
group by
  collection,
  forecast_name,
  product_key
order by season, collection, aggregated_sale
我猜,你甚至不需要一个
groupby

SELECT * FROM rank_processing.aggregate_season_product 
order by season, collection, aggregated_sale

MySQL 5.5.32架构设置

CREATE TABLE aggregate_season_product
    (`season` varchar(2), `collection` varchar(2), `forecast_name` varchar(2), `product_key` int, `aggregated_sale` int)
;

INSERT INTO aggregate_season_product
    (`season`, `collection`, `forecast_name`, `product_key`, `aggregated_sale`)
VALUES
    ('ss', 'cc', 'f1', 1, 11),
    ('ss', 'cc', 'f1', 3, 10),
    ('ss', 'cc', 'f1', 2, 5),
    ('ss', 'cc', 'f2', 5, 11),
    ('ss', 'cc', 'f2', 4, 7)
;
select * 
from aggregate_season_product 
where season = 'ss' and collection = 'cc'
ORDER by
    season,
    collection,
    forecast_name,
    aggregated_sale
| SEASON | COLLECTION | FORECAST_NAME | PRODUCT_KEY | AGGREGATED_SALE |
|--------|------------|---------------|-------------|-----------------|
|     ss |         cc |            f1 |           2 |               5 |
|     ss |         cc |            f1 |           3 |              10 |
|     ss |         cc |            f1 |           1 |              11 |
|     ss |         cc |            f2 |           4 |               7 |
|     ss |         cc |            f2 |           5 |              11 |
查询1

CREATE TABLE aggregate_season_product
    (`season` varchar(2), `collection` varchar(2), `forecast_name` varchar(2), `product_key` int, `aggregated_sale` int)
;

INSERT INTO aggregate_season_product
    (`season`, `collection`, `forecast_name`, `product_key`, `aggregated_sale`)
VALUES
    ('ss', 'cc', 'f1', 1, 11),
    ('ss', 'cc', 'f1', 3, 10),
    ('ss', 'cc', 'f1', 2, 5),
    ('ss', 'cc', 'f2', 5, 11),
    ('ss', 'cc', 'f2', 4, 7)
;
select * 
from aggregate_season_product 
where season = 'ss' and collection = 'cc'
ORDER by
    season,
    collection,
    forecast_name,
    aggregated_sale
| SEASON | COLLECTION | FORECAST_NAME | PRODUCT_KEY | AGGREGATED_SALE |
|--------|------------|---------------|-------------|-----------------|
|     ss |         cc |            f1 |           2 |               5 |
|     ss |         cc |            f1 |           3 |              10 |
|     ss |         cc |            f1 |           1 |              11 |
|     ss |         cc |            f2 |           4 |               7 |
|     ss |         cc |            f2 |           5 |              11 |

CREATE TABLE aggregate_season_product
    (`season` varchar(2), `collection` varchar(2), `forecast_name` varchar(2), `product_key` int, `aggregated_sale` int)
;

INSERT INTO aggregate_season_product
    (`season`, `collection`, `forecast_name`, `product_key`, `aggregated_sale`)
VALUES
    ('ss', 'cc', 'f1', 1, 11),
    ('ss', 'cc', 'f1', 3, 10),
    ('ss', 'cc', 'f1', 2, 5),
    ('ss', 'cc', 'f2', 5, 11),
    ('ss', 'cc', 'f2', 4, 7)
;
select * 
from aggregate_season_product 
where season = 'ss' and collection = 'cc'
ORDER by
    season,
    collection,
    forecast_name,
    aggregated_sale
| SEASON | COLLECTION | FORECAST_NAME | PRODUCT_KEY | AGGREGATED_SALE |
|--------|------------|---------------|-------------|-----------------|
|     ss |         cc |            f1 |           2 |               5 |
|     ss |         cc |            f1 |           3 |              10 |
|     ss |         cc |            f1 |           1 |              11 |
|     ss |         cc |            f2 |           4 |               7 |
|     ss |         cc |            f2 |           5 |              11 |

样本输入ss,f1,1,11 ss,f1,3,10 ss,f1,2,5 ss,f2,5,11 ss,f2,4,7预期输出是ss,f1,2,5 ss,f1,3,10 ss,f1,1,11 ss,f2,4,7 ss,f2,5,11我很困惑。如何排序?
groupby
mysql的实现对其内部pupos执行排序,因此嵌套查询中的
orderby
不会改变anything@Joe弗拉姆巴赫:似乎它是按
col1,col2排序的,col4
我想要上述输入的预期输出。:)使用
GROUP BY
或聚合函数中未使用的列将导致不可预测的结果。@user2450833:建议的解决方案在思想上是错误的incorrect@zerkms我不明白;请解释一下。问题是共有4列,其中3列在选择框中按*分组。这将导致不确定的结果。因为这里没有聚合,所以有必要去掉group by。因为没有聚合,所以很容易否决投票并忽略这个问题,因为OP没有考虑到底需要什么。在嵌套查询中排序没有任何意义。@zerkms:这就是为什么我提到应该删除它为什么不显示结果查询?PS:
groupby
在内部执行排序,因此相同列上的
orderby
是多余的。PPS:OP希望通过
收集、预测\名称、聚合\销售
、而不是
收集、预测\名称、产品\密钥
@zerkms:
分组依据
可以进行内部排序,它不必这样做。这就是这个答案的全部要点——如果你想保证订单,你必须为mysql添加
orderby
,根据定义,它总是这样做的。如果愿意,您甚至可以指定
groupbycoldesc