如何解决SQL Server 中 union all 的替代方法
我想在不使用 union all 的情况下编写以下查询。这两个查询的唯一区别是针对 'Event'
和 'Time1'
列:
解决方法
你可以这样使用:
Select L.id,L.StudentID,L.Name,L.Owner,O.Stage,O.probab,Multiplier.Event as Event,Multiplier.Time1
From Lead L
CROSS APPLY (
VALUES ('Lead',case when L.converted='true' and convert(varchar,L.ldate,23)<>convert(varchar,O.odate,23) then O.date else L.date end),('Contact Attempt',case when L.Attempted is null then O.Attempted else L.ContactAttempted end)
) AS Multiplier(Event,Time1)
left join Opp O
ON O.LeadID=L.LeadID
left join Acc A
ON A.id=O.AccountId
left join zip Z
ON z.CODE = left(L.postalcode,5)
where (L.ldate is not null or o.NewAssigned is not null)
顺便说一下,您应该始终定义 varchar
的长度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。