这是以下论坛帖子的后续内容: Case statement with contains
我很抱歉我正在写一篇新文章,但我意识到我在第一篇论坛帖子中遗漏了很多重要信息,如果我加上这个,这会让事情变得混乱,以为我会重新开始。
(我正在使用SSMS 2016)
我有以下SQL代码:
select
ContactGroupType.GroupID,FormattedName,GroupName,FormattedName,ContactGroupType.GroupType,[Address]
from ContactGroupContacts
left join ContactGroupType on ContactGroupContacts.GroupID = ContactGroupType.GroupID
left join ContactContacts on ContactGroupContacts.ContactID = ContactContacts.ContactID
left join ContactGroup_Details on ContactGroupType.GroupID = ContactGroupDetails.GroupID
left join Addresses on ContactGroupDetails.AddressID = Addresses.AddressID
left join ContactGroups on ContactGroupType.groupID = ContactGroups.groupID
where
ExpiryDate is null and
[Address] in ('9 fox ave','10 bush')
order by
FormattedAddress,
grouptype asc
使用上面的代码,我得到以下结果:
Group_Ref | GroupName | Contact_Name | GroupType | Address
17766 MR D & N Goodwin Dan Goodwin Current Tenant 10 bush
17766 MR D & N Goodwin Nikki Goodwin Current Tenant 10 bush
17108 MRS A & P Bamer Amber Goodwin Current Tenant 9 fox ave
17108 MRS A & P Bamer Peter Goodwin Current Tenant 9 fox ave
2018 MR O & E Tofu Ola Tofu Former Tenant 9 fox ave
11875 MR D & N Biggs Dan Biggs Former Tenant 9 fox ave
12952 MR R & E Spur Richard Spur Former Tenant 9 fox ave
13193 MS N & E Snalles Nicole Snalles Former Tenant 9 fox ave
我需要编写一个case语句来显示以下内容。因此,只要地址只有一个或多个当前租户,我就需要显示一个' 1'。如果地址有一个或多个当前和以前的租户,我需要显示一个' 2'。请看下面:
Group_Ref | GroupName | Contact_Name | GroupType | Address | Case
17766 MR D & N Goodwin Dan Goodwin Current Tenant 10 bush 1
17766 MR D & N Goodwin Nikki Goodwin Current Tenant 10 bush 1
17108 MRS A & P Bamer Amber Goodwin Current Tenant 9 fox ave 2
17108 MRS A & P Bamer Peter Goodwin Current Tenant 9 fox ave 2
2018 MR O & E Tofu Ola Tofu Former Tenant 9 fox ave 2
11875 MR D & N Biggs Dan Biggs Former Tenant 9 fox ave 2
12952 MR R & E Spur Richard Spur Former Tenant 9 fox ave 2
13193 MS N & E Snalles Nicole Snalles Former Tenant 9 fox ave 2
答案 0 :(得分:0)
这适用于SQL Server,它应该在MySql和Oracle中都有效:
CREATE TABLE TEST_TABLE (GroupType VARCHAR(20), Address VARCHAR(20));
INSERT INTO TEST_TABLE (GroupType, Address) VALUES ('Current Tenant', '10 bush');
INSERT INTO TEST_TABLE (GroupType, Address) VALUES ('Current Tenant', '10 bush');
INSERT INTO TEST_TABLE (GroupType, Address) VALUES ('Current Tenant', '9 fox');
INSERT INTO TEST_TABLE (GroupType, Address) VALUES ('Former Tenant', '9 fox');
SELECT A.GroupType
, A.Address
, CASE WHEN (SELECT COUNT(DISTINCT B.GroupType) FROM TEST_TABLE B WHERE B.Address = A.Address GROUP BY B.Address) = 1 THEN '1'
ELSE '2' END
FROM TEST_TABLE A
GROUP BY GroupType, Address;
SELECT
ContactGroupType.GroupID
, FormattedName
, GroupName
, FormattedName
, ContactGroupType.GroupType
, A.Address
, CASE WHEN (SELECT COUNT(DISTINCT CGT.GroupType)
FROM ContactGroupType CGT
LEFT JOIN ContactGroupDetails CGD
ON CGD.GroupID = CGT.GroupID
LEFT JOIN Addresses B
ON B.AddressID = CGD.AddressID
WHERE B.Address = A.Address
GROUP BY B.Address) = 1 THEN '1'
ELSE '2' END
FROM ContactGroupContacts
LEFT JOIN ContactGroupType
ON ContactGroupContacts.GroupID = ContactGroupType.GroupID
LEFT JOIN ContactContacts
ON ContactGroupContacts.ContactID = ContactContacts.ContactID
LEFT JOIN ContactGroup_Details
ON ContactGroupType.GroupID = ContactGroupDetails.GroupID
LEFT JOIN Addresses A
ON ContactGroupDetails.AddressID = A.AddressID
LEFT JOIN ContactGroups
ON ContactGroupType.groupID = ContactGroups.groupID
WHERE
ExpiryDate IS NULL AND
A.Address IN ('9 fox ave','10 bush')
ORDER BY
FormattedAddress
, grouptype asc