我正在尝试将一个查询放在一起,以便可以在数据库中找到既有NY许可/许可证又有有限许可/许可证的人,但是这些行是查询中的单独行。我只想要没有NYS许可证的产品,而且数量有限。
这是我到目前为止所拥有的:
p.Last_Name
,p.First_Name
,p.Middle_Initial
,p.person_id
,i.Document
,i.Expiration_date
,i.Updated_Date
--,i.IssueDate
--,i.Historical
--,i.Comments
--,f.Current_status
,i.State
from dbo.PersonDoc_ID_Numbers i
inner join "DB"."dbo"."People" p on p.person_id = i.person_id
inner join person_facilities f on p.person_id = f.person_id
where
(Document like '%limited%' and i.State like '%NY%' and ((i.Expiration_date > getdate()) or (i.Expiration_date is null))) or
(Document like '%NY%' and i.State like '%NY%' and ((i.Expiration_date > getdate()) or (i.Expiration_date is null)))
and f.current_status in ('Active')
and i.Historical != 1
order by p.Last_Name
所以这样的回报是给某人的,但是我不希望那些拥有纽约执照且数量有限的人。我只想要那些没有纽约证件的有限人:
Last_Name First_Name Middle_Initial person_id Document Expiration_date Updated_Date
GG FF L. 123 Limited Permit 2019-09-23 2018-10-04 456
GG FF L. 123 NY State License NULL 2018-11-13
Smith Tony L. 456 Limited Permit 2019-09-23 2018-10-04 456
Snoopy Dog L. 789 NY State License NULL 2018-11-13
所以对于FF GG来说,我对她的名字归来不感兴趣,因为她拥有纽约州执照和有限的名字。我只想要那些有限的人。但是我希望它能返回托尼·史密斯,就是这样。
*Update as requested:
dbo.PersonDoc_ID_Numbers
person_id Expiration_date State license_NO Updated_Date Historical Document
1 2019-9-22 NY 2 2018-10-04 0 Limited Perm *
1 null NY no 2019-4-17 0 NY State
2 null NY 3 2011-06-30 0 Limited Lic
2 null NY 4 2011-06-14 0 NY State
3 2018-5-04 NY 5 2018-05-01 0 Limited Perm*
4 2019-08-29 NY 6 2018-01-12 0 Limited Lic *
5 null null 7 2016-4-20 0 Jujufication
5 null NY 8 2018-4-05 0 NY State
6 2016-07-15 OH 9 2018-01-09 0 Snoopyism
dbo.Person_Facilities
person_id Current_status
1 Active
2 Active
3 Active
4 Active
5 Active
6 Active
dbo.Person
person_id First Last
1 GG FF
2 MM PP
3 Tony Smith
4 Snoopy Dog
5 Sarah Gd
6 Bethany Yoda
我在id表中的期望值旁边放置一个*。请注意,事实证明FF GG没有许可证号,因此她必须位于终端机中。
答案 0 :(得分:0)
假设您当前的查询很好,您可以使用COUNT(*) OVER(PARTITION BY p.person_id) AS noOfRows
确定查询将生成多少行,然后select
仅将带有查询的一行的条目沿着这些行:< / p>
WITH myCte AS (
SELECT
p.Last_Name
,p.First_Name
,p.Middle_Initial
,p.person_id
,i.Document
,i.Expiration_date
,i.Updated_Date
,i.IssueDate
,i.Historical
,i.Comments
,f.Current_status
,i.State as DocumentState
,COUNT(*) OVER(PARTITION BY p.person_id) AS noOfRows
from dbo.PersonDoc_ID_Numbers i
inner join "DB"."dbo"."People" p on p.person_id = i.person_id
left join person_facilities f on p.person_id = f.person_id
where
(Document like '%limited%' and i.State like '%NY%' and ((i.Expiration_date > getdate()) or (i.Expiration_date is null))) or
(Document like '%NY%' and i.State like '%NY%' and ((i.Expiration_date > getdate()) or (i.Expiration_date is null)))
and f.current_status in ('Active')
and i.Historical != 1
)
SELECT Last_Name
,First_Name
,Middle_Initial
,person_id
,Document
,Expiration_date
,Updated_Date
,IssueDate
,Historical
,Comments
,Current_status
,DocumentState
FROM myCte
WHERE noOfRows = 1
AND Document like '%limited%'
这是工作中的简化示例:SQLFiddle
编辑:
鉴于您提供的示例数据,这是我建议的解决方案:
WITH myCte AS (
SELECT
p.Last,
p.First,
p.person_id,
i.Document,
i.Expiration_date,
i.Updated_Date,
i.Historical,
f.Current_status,
i.State as DocumentState,
COUNT(*) OVER(PARTITION BY p.person_id) AS noOfRows
FROM dbo.PersonDoc_ID_Numbers i
JOIN dbo.Person p ON p.person_id = i.person_id
JOIN person_facilities f ON p.person_id = f.person_id
WHERE
(Document like '%limited%' AND i.State like '%NY%' AND ((i.Expiration_date > getdate()) OR (i.Expiration_date is null))) or
(Document like '%NY%' AND i.State like '%NY%' AND ((i.Expiration_date > getdate()) OR (i.Expiration_date is null)))
AND f.current_status in ('Active')
AND i.Historical != 1
)
SELECT Last
,First
,person_id
,Document
,Expiration_date
,Updated_Date
,Historical
,Current_status
,DocumentState
FROM myCte
WHERE noOfRows = 1
AND Document like '%limited%'
这是使用提供的数据的新示例:SQLFiddle
答案 1 :(得分:0)
您可以在下面尝试查询。
WITH cte AS (
SELECT p.Last_Name
, p.First_Name
, p.Middle_Initial
, p.person_id
, i.Document
, i.Expiration_date
, i.Updated_Date
FROM dbo.PersonDoc_ID_Numbers i
JOIN "DB"."dbo"."People" p ON p.person_id = i.person_id
JOIN person_facilities f ON p.person_id = f.person_id
WHERE /*Document LIKE '%limited%'
AND i.State LIKE '%NY%'
AND*/ (i.Expiration_date > GETDATE() OR i.Expiration_date IS NULL)
AND f.current_status IN ('Active')
AND i.Historical != 1
)
SELECT *
FROM cte c
WHERE Document LIKE '%limited%' AND i.State LIKE '%NY%'
AND NOT EXISTS (
SELECT 1
FROM cte
WHERE person_id = p.person_id AND Document NOT LIKE '%limited%' AND i.State LIKE '%NY%'
)
ORDER BY Last_Name