我有一个包含Customer_ID,Customer_Name,Address1,Address2,Mail_Address1,Mail_Address2等的表。
在某些情况下,Mail_Address1 ='相同'或者' ....这基本上意味着我需要提取Address1 + Address2,否则忽略并获取Mail_Address1 + Mail_Address2。
我的伪SQL查询如下所示: -
// The query that would give me all the records with the same address as Address1 for Mailing Address //
SELECT Customer_ID, Customer_Name, Address1 + ', ' + Address2 as Address, ....
FROM Customers
Where Customer_ID != 0 AND (Mail_Address1 ='Same' or Mail_Address1 ='')
和
SELECT Customer_ID, Customer_Name, Mail_Address1 + ', ' + Mail_Address2 as Address, ....
FROM Customers
Where Customer_ID != 0 AND Mail_Address1 !='Same' AND Mail_Address1 !=''
如何组合这两个查询,以便我可以根据条件选择一个正确的地址。 (相同或空字符串)。
答案 0 :(得分:0)
您是否考虑过CASE
声明?
SELECT
CASE
WHEN Customer_ID != 0 AND (Mail_Address1 ='Same' OR Mail_Address1 = '')
THEN Address1 + ', ' + Address2
ELSE Mail_Address1 + ', ' + Mail_Address2
END AS address
FROM Customers