我如何设计一个 select 语句来 select 一个完全基于条件的值的子集?
How can I design a select statement to select a subset of values exclusively based on a condition?
我正在尝试将 SQL 语句设计为仅在某些情况下 select 值。这是一个示例来说明我的 2 个输入表和预期输出。
表 1:
TransactionID TotalItemsType1 TotalItemsType2
0001 1 8
0002 7 6
1234 5 6
表 2:
TransactionID Cost
0001 5.99
1234 2.25
1234 0.15
0002 9.99
预期结果:
TransactionID Cost TotalItemsType1 TotalItemsType2
0001 5.99 1 8
0002 9.99 7 6
1234 2.25 5 6
1234 0.15 0 0
当表 2 中有多个行用于单个 TransactionID (1234) 时,输出中的 TotalItemsType1/2 列应仅填充具有最高 Cost 值(2.25 与 0.15 相比)的 TransactionID,否则返回 0。如果多个最大值相等(例如 2.25 与 2.25 相比),则只需根据数据库行顺序(第一个)选择一个 TransactionID。
我尝试使用各种连接和 case 表达式来做到这一点,但我还没有找到可行的解决方案。
您可以通过 CTE 将分区行号应用于 Table2,然后仅当行号为 1(否则为 0)时有条件地使用 JOIN 中的值:
WITH rn AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY TransactionID ORDER BY Cost) rn
FROM Table2
)
SELECT rn.TransactionID, rn.Cost, COALESCE(t1.TotalItemsType1, 0) TotalItemsType1,
COALESCE(t1.TotalItemsType2, 0) TotalItemsType2
FROM rn LEFT JOIN Table1 t1
ON rn.TransactionID = t1.TransactionID AND rn.rn = 1
LEFT JOIN
使用行号为1的条件为不为1的Table1生成"empty columns",然后COALESCE
使用这些NULL值生成需要的0 .
with max_cost as ( select transactionid tranid,max(cost) max_cost1 from tran_2 group by transactionid)
select a.transactionid,
b.cost,
decode(b.cost,max_cost1,a.totaltype1,0) type_1,
decode(b.cost,max_cost1,a.totaltype2,0) type_2
from tran_1 a,tran_2 b,max_cost c
where a.transactionid = b.transactionid
and c.tranid = a.transactionid
and c.tranid = b.transactionid
order by a.transactionid
不使用解析函数的解决方案
我使用了包含 transactionid,type1,type2 列的 tran_1 table
和 tran_2 table 包含 transactionid,cost
我正在尝试将 SQL 语句设计为仅在某些情况下 select 值。这是一个示例来说明我的 2 个输入表和预期输出。
表 1:
TransactionID TotalItemsType1 TotalItemsType2
0001 1 8
0002 7 6
1234 5 6
表 2:
TransactionID Cost
0001 5.99
1234 2.25
1234 0.15
0002 9.99
预期结果:
TransactionID Cost TotalItemsType1 TotalItemsType2
0001 5.99 1 8
0002 9.99 7 6
1234 2.25 5 6
1234 0.15 0 0
当表 2 中有多个行用于单个 TransactionID (1234) 时,输出中的 TotalItemsType1/2 列应仅填充具有最高 Cost 值(2.25 与 0.15 相比)的 TransactionID,否则返回 0。如果多个最大值相等(例如 2.25 与 2.25 相比),则只需根据数据库行顺序(第一个)选择一个 TransactionID。
我尝试使用各种连接和 case 表达式来做到这一点,但我还没有找到可行的解决方案。
您可以通过 CTE 将分区行号应用于 Table2,然后仅当行号为 1(否则为 0)时有条件地使用 JOIN 中的值:
WITH rn AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY TransactionID ORDER BY Cost) rn
FROM Table2
)
SELECT rn.TransactionID, rn.Cost, COALESCE(t1.TotalItemsType1, 0) TotalItemsType1,
COALESCE(t1.TotalItemsType2, 0) TotalItemsType2
FROM rn LEFT JOIN Table1 t1
ON rn.TransactionID = t1.TransactionID AND rn.rn = 1
LEFT JOIN
使用行号为1的条件为不为1的Table1生成"empty columns",然后COALESCE
使用这些NULL值生成需要的0 .
with max_cost as ( select transactionid tranid,max(cost) max_cost1 from tran_2 group by transactionid)
select a.transactionid,
b.cost,
decode(b.cost,max_cost1,a.totaltype1,0) type_1,
decode(b.cost,max_cost1,a.totaltype2,0) type_2
from tran_1 a,tran_2 b,max_cost c
where a.transactionid = b.transactionid
and c.tranid = a.transactionid
and c.tranid = b.transactionid
order by a.transactionid
不使用解析函数的解决方案 我使用了包含 transactionid,type1,type2 列的 tran_1 table 和 tran_2 table 包含 transactionid,cost