T-SQL查询:在'Union ALL'语句中包含逻辑

时间:2013-07-09 20:11:30

标签: sql sql-server tsql union case

Mark Ba​​nnister在这个论坛上非常友好地帮助我创建原始查询。我正在尝试修改它以包含case语句中列出的项的条件逻辑。查询的目标可能包含某些软件项的一个或另一个版本。查询将返回任一项为合规。是否可以在联合所有部分中包含逻辑,还是有另一种方法可以做到这一点?

SELECT MIN(CASE 
WHEN DN.Name = 'Adobe Reader XI (11.0.01)' THEN CASE ARP.Version0 WHEN '11.0.01' THEN 'Adobe Reader Compliant' ELSE 'Adobe Reader not Compliant' END 
WHEN DN.Name = 'ActivClient x86' THEN CASE ARP.Version0 WHEN '6.2' THEN 'Active Client Compliant' ELSE 'Active Client not Compliant' END 
WHEN DN.Name = 'Adobe Flash Player 11 ActiveX' THEN CASE ARP.Version0 WHEN '11.7.700.202' THEN 'Adobe Flash Player Compliant' ELSE 'Adobe Flash Player not Compliant' END 
WHEN DN.Name = 'Adobe Shockwave Player 12.0' THEN CASE ARP.Version0 WHEN '12.0.2.122' THEN 'Adobe Shockwave Compliant' ELSE 'Adobe Shockwave not Compliant' END END) as Complaint
From (Select 'Adobe Reader XI (11.0.01)' NAME  UNION ALL SELECT 'ActivClient x86' UNION ALL SELECT 'Adobe Flash Player 11 ActiveX' UNION ALL SELECT 'Adobe Shockwave Player 12.0') as DN
CROSS JOIN dbo.v_R_System AS SYS 
LEFT JOIN dbo.v_Add_Remove_Programs AS ARP
ON ARP.ResourceID = SYS.ResourceID AND ARP.DisplayName0 = DN.Name
WHERE (SYS.Netbios_Name0 = @System_Name)
GROUP BY SYS.Netbios_Name0, DN.Name

例如更改为:

 WHEN DN.Name = 'ActivClient x86' or DN.Name = 'ActivClient x64' THEN CASE ARP.Version0 WHEN '6.2' THEN 'Active Client Compliant' ELSE 'Active Client not Compliant' END 

1 个答案:

答案 0 :(得分:0)

您还可以使用其他表单将逻辑放在现有CASE语句中。例如,您可以替换:

CASE ARP.Version0 WHEN '11.0.01' THEN 'Adobe Reader Compliant' 
                                 ELSE 'Adobe Reader not Compliant' END 

使用

CASE WHEN  ARP.Version0 IN ('11.0.01', '11.0.02') THEN 'Adobe Reader Compliant' 
                                                  ELSE 'Adobe Reader not Compliant' END