繁体   English   中英

根据列和不同列的值选择行

[英]select rows based on value of column and distinct column

我想根据商店数量选择具有不同商店位置的行,示例如下所示:

id   | item_name   | number_of_store| store_location|
+----+---------+-------------------+-------------+
|  3 | margarine | 2              | QLD         |
|  4 | margarine | 2              | NSW         |
|  5 | wine      | 3              | QLD         |
|  6 | wine      | 3              | NSW         |
|  7 | wine      | 3              | NSW         |
|  8 | laptop    | 1              | QLD         |
+----+---------+-------------------+-------------+

预期结果如下所示:

id   | item_name   | number_of_store| store_location|
+----+---------+-------------------+-------------+
|  3 | margarine | 2              | QLD         |
|  4 | margarine | 2              | NSW         |
|  5 | wine      | 3              | QLD         |
|  6 | wine      | 3              | NSW         |
|  8 | laptop    | 1              | QLD         |
+----+---------+-------------------+-------------+

我不在乎哪个id列值返回。 所需的SQL是什么?

您可以使用GROUP BY

SELECT id, item_name ,number_of_store, store_location
FROM tab
GROUP BY  item_name ,number_of_store, store_location;
-- will work if ONLY_FULL_GROUP_BY is disabled

否则,您需要ANY_VALUEMIN等聚合函数:

SELECT ANY_VALUE(id) AS id, item_name ,number_of_store, store_location
FROM tab
GROUP BY  item_name ,number_of_store, store_location;

DB-Fiddle.com演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM