我有2臺(MS ACCESS):訪問SQL:如何根據「更重要」條件指定要返回的記錄?
表 「訂單」
OrderID Product Product_Group Client Client_Group Revenue
1 Cars Vehicles Men People 10 000
2 Houses NC_Assets Women People 15 000
3 Houses NC_Assets Partnersh Companies 12 000
4 Cars Vehicles Corps Companies 3 000
表 「Gouping」
Product Product_Group Client Client_Group Tax rate
Cars Companies Taxable 30%
Vehicles Companies Taxable 15%
Houses People Taxable 13%
Houses Women Taxable 15%
我想加入這些表,看看哪些訂單將落入哪個應稅集團。正如你可以看到一些產品/客戶的映射方式不同於它們的組 - >如果是這種情況,查詢應該只返回一對這個對的記錄,並排除包含它們的組的任何配對。在僞代碼中:
If there's product-client grouping, return this record Else
If there's product-client grouping ---//----- else
If there's product group - client ----///-----else
If there's product group-client group ---///----
End if * 4
按照該順序。
現在我的查詢(僞):
SELECT [Orders].*, [Grouping].* FROM [Orders] LEFT JOIN [Grouping] ON
(([Orders].Product = [Grouping].Product OR [Orders].Product_Group = [Grouping].Product_Group) AND
([Orders].Client = [Grouping].Client OR [Orders].Client_Group = [Grouping].Client_Group))
同時返回汽車,公司和車輛-公司。我不知道如何設置它以僅從每個組合獲得最細粒度的記錄。聯盟?不存在?
任何幫助表示讚賞。
使用UNIONs。你可以在工會中添加一個'ImportanceRank'列或者其他東西,但是隻要按照SELECT TOP 1的順序將它們列出就可以了。 – Malk 2014-11-03 17:33:09
Malk,這聽起來很有趣 - 你能給我一個如何coul我做的例子(SQL中的newb) – Zenon 2014-11-03 19:34:00