我正在使用 SQLite。我想讓 2 列中的每個資料行都是唯一的。
表中的示例資料order_transaction:
| 日期交易 | 顧客 |
|---|---|
| 2022-05-11 | 迪奧 |
| 2022-05-11 | 迪奧 |
| 2022-05-11 | 承太郎 |
當我使用distinct查詢時:
SELECT DISTINCT date_transaction, customer FROM order_transaction;
輸出是:
| 日期交易 | 顧客 |
|---|---|
| 2022-05-11 | 迪奧 |
| 2022-05-11 | 承太郎 |
好的,所以我得到customer了唯一的行,但是我怎樣才能得到這樣的date_transaction唯一行?
| 日期交易 | 顧客 |
|---|---|
| 2022-05-11 | 迪奧 |
| 承太郎 |
什么查詢可以獲得該輸出?
我是 SQLite 的新手。對不起 :)
uj5u.com熱心網友回復:
使用CASE運算式和ROW_NUMBER()視窗函式:
SELECT CASE WHEN ROW_NUMBER() OVER (PARTITION BY t.date_transaction ORDER BY t.customer) = 1 THEN t.date_transaction END date_transaction,
t.customer
FROM order_transaction t
GROUP BY t.date_transaction, t.customer
ORDER BY t.date_transaction, ROW_NUMBER() OVER (PARTITION BY t.date_transaction ORDER BY t.customer);
請參閱演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/476319.html
標籤:sqlite 案子 sql-order-by 清楚的 窗函数
上一篇:使用包含滯后值的SQL計算比率
