我有一个Query,它返回了我需要的大部分内容,但我需要一些帮助,我认为是一个WHERE语句。
SELECT
C.NameOf AS 'Client',
CV2.DescriptionOf,
VC.Limit1 AS '1st Limit',
VC.Limit2 AS '2nd Limit'
FROM Policy AS P
--And a Bunch of other Joins
WHERE CV2.DescriptionOf IN ('Underinsured/Uninsured Motorists BI',
'Bodily Injury Liability','Combined Single Limit Liability',
'Property Damage - Single Limit')
并返回以下客户信息(显示1个客户端)
Client DescriptionOf 1stLimit 2ndLimit
John Doe Uninsured Motorists 25000 25000
John Doe Property Damage 50000
John Doe Bodily Injury 25000 25000
我需要的是一个WHERE子句,它将返回描述为“Uninsured Motorists”的行和,1stLimit大于其他2行的1stLimit字段。 和2ndLimit字段相同。所以基本上它会返回以下内容......
Client DescriptionOf 1stLimit 2ndLimit
John Doe Uninsured Motorists 25000 25000
John Doe Property Damage 50000
有什么想法吗?
答案 0 :(得分:0)
好的,我正在修改我的答案,这将看看Uninsured Motorists VC.Limit1是否小于其他任何计划(CV2.DescriptionOf)。注意:这是猜测因为我必须在一堆其他连接部分做出很多假设。
SELECT
C.NameOf AS 'Client',
CV2.DescriptionOf,
VC.Limit1 AS '1st Limit',
VC.Limit2 AS '2nd Limit'
FROM Policy AS P
--And a Bunch of other Joins
inner join (select max(VC.Limit1) as MaxLimit, C.NameOf as ClientName
from VC --And a Bunch of other Joins
where CV2.DescriptionOf != 'Uninsured Motorists'
group by C.NameOf) m
on m.ClientName = C.NameOf
WHERE CV2.DescriptionOf = 'Uninsured Motorists'
and VC.Limit1 < MaxLimit
答案 1 :(得分:0)
这可以使用CTE和联合
; with
CTE as (
SELECT C.NameOf AS 'Client',
CV2.DescriptionOf as 'Description',
VC.Limit1 AS 'FirstLimit',
VC.Limit2 AS 'SecondLimit'
FROM Policy AS P
--And a Bunch of other Joins
WHERE CV2.DescriptionOf IN ('Underinsured/Uninsured Motorists BI', 'Bodily Injury Liability', 'Combined Single Limit Liability', 'Property Damage - Single Limit')
)
select *
from CTE p
where FirstLimit = (
select MAX(FirstLimit)
from CTE
where Client = p.Client
)
union
select *
from CTE
where Description = 'Uninsured Motorists'