table 中的多个字段只有 return 个异常
Only return exceptions from multiple fields in a table
我有 table(交易)存储零售交易。我希望能够找到多个字段的独特组合。我有一个 table 存储交易类型(在线或商店)和客户名称、周数和产品类型。我需要找到共享相同客户、周和产品的在线或商店交易,但前提是没有来自其他交易类型的相应交易。请参阅下面的示例 ...
+------------------+----------+------+--------------+
| Transaction_Type | Customer | Week | Product_Type |
+------------------+----------+------+--------------+
| Online | 123 | 1 | Clothing |
+------------------+----------+------+--------------+
| Store | 123 | 1 | Homeware |
+------------------+----------+------+--------------+
| Online | 123 | 1 | Homeware |
+------------------+----------+------+--------------+
| Online | 123 | 2 | Clothing |
+------------------+----------+------+--------------+
| Store | 123 | 2 | Clothing |
+------------------+----------+------+--------------+
| Online | 123 | 2 | Sporting |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Clothing |
+------------------+----------+------+--------------+
| Store | 345 | 2 | Homeware |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Homeware |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Clothing |
+------------------+----------+------+--------------+
| Store | 345 | 2 | Homeware |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Sporting |
+------------------+----------+------+--------------+
由此我想看到以下返回...
+------------------+----------+------+--------------+
| Transaction_Type | Customer | Week | Product_Type |
+------------------+----------+------+--------------+
| Online | 123 | 1 | Clothing |
+------------------+----------+------+--------------+
| Online | 123 | 2 | Clothing |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Clothing |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Clothing |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Clothing |
+------------------+----------+------+--------------+
| Store | 345 | 2 | Homeware |
+------------------+----------+------+--------------+
| Online | 345 | 2 | Sporting |
+------------------+----------+------+--------------+
其他交易被排除在外,因为在线和商店具有相同的客户、周和 product_type 值。
我目前的代码是这样的...
select * from transactions
group by customer, week, product_type
having count(customer, week, product_type) <> 2
但我想我应该使用分区,但我不知道如何使用。
谢谢
您可以使用 not exists
:
select t.*
from transactions t
where not exists (
select 1
from transactions t1
where
t1.customer = t.customer
and t1.week = t.week
and t1.product = t.product
and t1.transaction_type <> t.transaction_type
)
我有 table(交易)存储零售交易。我希望能够找到多个字段的独特组合。我有一个 table 存储交易类型(在线或商店)和客户名称、周数和产品类型。我需要找到共享相同客户、周和产品的在线或商店交易,但前提是没有来自其他交易类型的相应交易。请参阅下面的示例 ...
+------------------+----------+------+--------------+ | Transaction_Type | Customer | Week | Product_Type | +------------------+----------+------+--------------+ | Online | 123 | 1 | Clothing | +------------------+----------+------+--------------+ | Store | 123 | 1 | Homeware | +------------------+----------+------+--------------+ | Online | 123 | 1 | Homeware | +------------------+----------+------+--------------+ | Online | 123 | 2 | Clothing | +------------------+----------+------+--------------+ | Store | 123 | 2 | Clothing | +------------------+----------+------+--------------+ | Online | 123 | 2 | Sporting | +------------------+----------+------+--------------+ | Online | 345 | 2 | Clothing | +------------------+----------+------+--------------+ | Store | 345 | 2 | Homeware | +------------------+----------+------+--------------+ | Online | 345 | 2 | Homeware | +------------------+----------+------+--------------+ | Online | 345 | 2 | Clothing | +------------------+----------+------+--------------+ | Store | 345 | 2 | Homeware | +------------------+----------+------+--------------+ | Online | 345 | 2 | Sporting | +------------------+----------+------+--------------+
由此我想看到以下返回...
+------------------+----------+------+--------------+ | Transaction_Type | Customer | Week | Product_Type | +------------------+----------+------+--------------+ | Online | 123 | 1 | Clothing | +------------------+----------+------+--------------+ | Online | 123 | 2 | Clothing | +------------------+----------+------+--------------+ | Online | 345 | 2 | Clothing | +------------------+----------+------+--------------+ | Online | 345 | 2 | Clothing | +------------------+----------+------+--------------+ | Online | 345 | 2 | Clothing | +------------------+----------+------+--------------+ | Store | 345 | 2 | Homeware | +------------------+----------+------+--------------+ | Online | 345 | 2 | Sporting | +------------------+----------+------+--------------+
其他交易被排除在外,因为在线和商店具有相同的客户、周和 product_type 值。
我目前的代码是这样的...
select * from transactions
group by customer, week, product_type
having count(customer, week, product_type) <> 2
但我想我应该使用分区,但我不知道如何使用。
谢谢
您可以使用 not exists
:
select t.*
from transactions t
where not exists (
select 1
from transactions t1
where
t1.customer = t.customer
and t1.week = t.week
and t1.product = t.product
and t1.transaction_type <> t.transaction_type
)