我想根据商店数量选择具有不同商店位置的行,示例如下所示:
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是什么?
答案 0 :(得分:2)
您可以使用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;