SQL - 为给定列中的值获取另一列的值
SQL - Get the values of another column for values in a given column
我正在处理大量数据 table。如果给定项目有超过 1 个目的地,我想检查单个项目及其目的地和目的地数量。
所以在下图中,在我的结果中,我不应该得到物品 Grapes 和 Rice,因为它们只有一个目的地。但我应该得到 Apple 和 Orange 项目的结果。
我写的查询如下。然而它并没有达到目的。
SELECT PrdTbl.Item,,PrdTbl2.cnt
FROM ProductsTable PrdTbl
JOIN
(
SELECT Item,count(*) cnt FROM ProductsTable
GROUP BY Item
HAVING COUNT(*) > 1
) PrdTbl2 ON PrdTbl.Item = PrdTbl2.Item
ORDER BY PrdTbl.Item;
有人可以提出解决方案吗?提前致谢。
编辑:
感谢您的原始答案。
我认为我的问题需要重新措辞。我也想按目的地过滤。虽然原始条件保持不变,但我需要删除前往同一目的地的项目。所以在这里,我需要的只有Apple及其各自的目的地。橙色应该被删除,因为它去同一个目的地。
如果你只需要看项目,没有必要加入,你可以直接说
select item, count(1)
from ek_test
group by item
having count(1) > 1;
这将向您显示超过 1 条记录的项目
正如@forpas 所说,如果您修复了 select 列表,那么您的查询就会按照您所说的进行:
SELECT PrdTbl.Item,PrdTbl.Destination,PrdTbl2.cnt
FROM ...
您也可以使用分析计数来避免自连接:
SELECT Item, Destination, cnt
FROM (
SELECT Item, Destination, count(*) OVER (PARTITION BY Item) as cnt
FROM ProductsTable
)
WHERE cnt > 1
ORDER BY Item;
ITEM | DESTINATION | CNT
:----- | :---------- | --:
Apple | Paris | 3
Apple | Rome | 3
Apple | London | 3
Orange | Cape Town | 3
Orange | New York | 3
Orange | Cairo | 3
I want to filter by the destination as well. While the original condition remains the same, I need to remove the items which are going to the same destination.
您只需要计算不同的目的地,而不是所有行:
SELECT PrdTbl.Item,PrdTbl.Destination,PrdTbl2.cnt
FROM ProductsTable PrdTbl
JOIN
(
SELECT Item,count(DISTINCT Destination) cnt FROM ProductsTable
GROUP BY Item
HAVING COUNT(DISTINCT Destination) > 1
) PrdTbl2 ON PrdTbl.Item = PrdTbl2.Item
ORDER BY PrdTbl.Item;
或分析:
SELECT Item, Destination, cnt
FROM (
SELECT Item, Destination, count(DISTINCT Destination) OVER (PARTITION BY Item) as cnt
FROM ProductsTable
)
WHERE cnt > 1
ORDER BY Item;
ITEM | DESTINATION | CNT
:---- | :---------- | --:
Apple | Barcelona | 5
Apple | London | 5
Apple | Moscow | 5
Apple | Paris | 5
Apple | Rome | 5
我正在处理大量数据 table。如果给定项目有超过 1 个目的地,我想检查单个项目及其目的地和目的地数量。
所以在下图中,在我的结果中,我不应该得到物品 Grapes 和 Rice,因为它们只有一个目的地。但我应该得到 Apple 和 Orange 项目的结果。
我写的查询如下。然而它并没有达到目的。
SELECT PrdTbl.Item,,PrdTbl2.cnt
FROM ProductsTable PrdTbl
JOIN
(
SELECT Item,count(*) cnt FROM ProductsTable
GROUP BY Item
HAVING COUNT(*) > 1
) PrdTbl2 ON PrdTbl.Item = PrdTbl2.Item
ORDER BY PrdTbl.Item;
有人可以提出解决方案吗?提前致谢。
编辑:
感谢您的原始答案。
我认为我的问题需要重新措辞。我也想按目的地过滤。虽然原始条件保持不变,但我需要删除前往同一目的地的项目。所以在这里,我需要的只有Apple及其各自的目的地。橙色应该被删除,因为它去同一个目的地。
如果你只需要看项目,没有必要加入,你可以直接说
select item, count(1)
from ek_test
group by item
having count(1) > 1;
这将向您显示超过 1 条记录的项目
正如@forpas 所说,如果您修复了 select 列表,那么您的查询就会按照您所说的进行:
SELECT PrdTbl.Item,PrdTbl.Destination,PrdTbl2.cnt
FROM ...
您也可以使用分析计数来避免自连接:
SELECT Item, Destination, cnt
FROM (
SELECT Item, Destination, count(*) OVER (PARTITION BY Item) as cnt
FROM ProductsTable
)
WHERE cnt > 1
ORDER BY Item;
ITEM | DESTINATION | CNT
:----- | :---------- | --:
Apple | Paris | 3
Apple | Rome | 3
Apple | London | 3
Orange | Cape Town | 3
Orange | New York | 3
Orange | Cairo | 3
I want to filter by the destination as well. While the original condition remains the same, I need to remove the items which are going to the same destination.
您只需要计算不同的目的地,而不是所有行:
SELECT PrdTbl.Item,PrdTbl.Destination,PrdTbl2.cnt
FROM ProductsTable PrdTbl
JOIN
(
SELECT Item,count(DISTINCT Destination) cnt FROM ProductsTable
GROUP BY Item
HAVING COUNT(DISTINCT Destination) > 1
) PrdTbl2 ON PrdTbl.Item = PrdTbl2.Item
ORDER BY PrdTbl.Item;
或分析:
SELECT Item, Destination, cnt
FROM (
SELECT Item, Destination, count(DISTINCT Destination) OVER (PARTITION BY Item) as cnt
FROM ProductsTable
)
WHERE cnt > 1
ORDER BY Item;
ITEM | DESTINATION | CNT
:---- | :---------- | --:
Apple | Barcelona | 5
Apple | London | 5
Apple | Moscow | 5
Apple | Paris | 5
Apple | Rome | 5