[英]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_VALUE
或MIN
等聚合函數:
SELECT ANY_VALUE(id) AS id, item_name ,number_of_store, store_location
FROM tab
GROUP BY item_name ,number_of_store, store_location;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.