我有2个脚本,如下所示:
第一:
SELECT [Fm].[Id], [Sdp].[FirstName], [Sdp].[LastName], [Sdp].[SSN], [Sdp].[StoreName], [Sdp].[PostalCode], [Fc].[Id], [Sdp].[Address] FROM [SRM].[SiteMembers].[DProfile] AS [Sdp] INNER JOIN [SRM].[SiteMembers].[Member] AS [Sm] ON [Sdp].[Member_Id] = [Sm].[Id] INNER JOIN [FRM].[Members].[Member] AS [Fm] ON [Sm].[UserId] = [Fm].[UserId] INNER JOIN [SRM].[General].[City] AS [Sc] ON [Sdp].[City_Id]=[Sc].[Id] INNER JOIN [FRM].[General].[City] AS [Fc] ON [Fc].[Title]=[Sc].[Title] COLLATE SQL_Latin1_General_CP1_CI_AS WHERE ISNUMERIC([Sdp].[PostalCode])=1;
第二个:
SELECT [Fm].[Id], [Sdp].[FirstName], [Sdp].[LastName], [Sdp].[SSN], [Sdp].[StoreName], 0, [Fc].[Id], [Sdp].[Address] FROM [SRM].[SiteMembers].[DProfile] AS [Sdp] INNER JOIN [SRM].[SiteMembers].[Member] AS [Sm] ON [Sdp].[Member_Id] = [Sm].[Id] INNER JOIN [FRM].[Members].[Member] AS [Fm] ON [Sm].[UserId] = [Fm].[UserId] INNER JOIN [SRM].[General].[City] AS [Sc] ON [Sdp].[City_Id]=[Sc].[Id] INNER JOIN [FRM].[General].[City] AS [Fc] ON [Fc].[Title]=[Sc].[Title] COLLATE SQL_Latin1_General_CP1_CI_AS WHERE ISNUMERIC([Sdp].[PostalCode])=0;
这2个脚本之间的区别在于,第一个脚本选择带有数字邮政编码的列,第二个脚本选择非数字邮政编码的0,所以我如何将这2个脚本合并到一个脚本中,我不是在谈论Union,我很有趣在非数字邮政编码的选择查询中使用某些条件时,请选择0。任何人有任何想法吗?
Union
case在选定的列列表中使用一条语句,然后删除该where子句:
case
where
SELECT [Fm].[Id], [Sdp].[FirstName], [Sdp].[LastName], [Sdp].[SSN], [Sdp].[StoreName], case when isnumeric([Sdp].[PostalCode]) = 1 then [Sdp].[PostalCode] else 0 end, [Fc].[Id], [Sdp].[Address] FROM [SRM].[SiteMembers].[DProfile] AS [Sdp] INNER JOIN [SRM].[SiteMembers].[Member] AS [Sm] ON [Sdp].[Member_Id] = [Sm].[Id] INNER JOIN [FRM].[Members].[Member] AS [Fm] ON [Sm].[UserId] = [Fm].[UserId] INNER JOIN [SRM].[General].[City] AS [Sc] ON [Sdp].[City_Id]=[Sc].[Id] INNER JOIN [FRM].[General].[City] AS [Fc] ON [Fc].[Title]=[Sc].[Title] COLLATE SQL_Latin1_General_CP1_CI_AS