如果另一個表中存在活動,我想獲得活動的默認率或覆寫率。我該如何為此撰寫 SQL?
我有這個查詢,但它產生一個錯誤“每個派生表都必須有自己的別名”。
select A.id, rate from (
select
A.id, coalesce(B.rate, A.rate) as rate
from
A
left join B on B.id = A.id
);
考慮以下資料
table user
user_id name
1 johnny
2 sam
table activity_types
activity_type_id description rate
1 cook steak $12.00
2 flip burgers $9.00
3 wait tables $8.00
4 wash dishes $8.00
table personal_override_rates
user_id activity_type_id rate
1 1 $18
table activities
activity_id user_id activity_type_id qty
1 1 1 1
2 1 2 1
3 2 1 1
4 2 2 1
期望的結果:
johnny cook steak 1 $18.00
johnny flip burgers 1 $9.00
sam cook steak 1 $12.00
sam flip burgers 1 $9.00
uj5u.com熱心網友回復:
您得到的錯誤是因為您有一個沒有別名的派生表。您必須提供一個,然后使用別名選擇 id 和 rate。
像這樣:
select derived_table.id, derived_table.rate from (
select
A.id, coalesce(B.rate, A.rate) as rate
from
A
left join B on B.id = A.id
) as derived_table;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528918.html
上一篇:輸出一個數字—姓名(first_name)與至少一個其他學生的姓名匹配的學生人數
下一篇:獲取sql中行的差異總和
