问题描述
我想在不使用 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
的长度。