sql - 在 SQL Server 中联合所有的替代方案
问题描述
我想在不使用 union all 的情况下编写以下查询。两个查询的唯一区别是 for'Event'
和'Time1'
columns:
解决方案
你可以使用这样的东西:
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
.
推荐阅读
- vba - Excel VBA:如果单元格着色,则输出单元格行和列标题
- angular - 如何从选择之外操作 mat-select 多个?
- vim - vim 搜索和复制行,包括模式
- javascript - 如何使用 react onScroll
- google-apis-explorer - 使用 https://classroom.googleapis.com/v1/courses?pageToken=SomePageToken 的 HTTP 400 错误请求
- sql - TSQL CASE 意外处理多个 THEN 语句
- python - 读取 json 并仅将某些 json 对象写入 csv
- swift - Swift 4.2 中的 AVAudioSession setCategory 可用性
- react-native - 使用 navigationOptions 根据 React Navigation 中的状态更改底部选项卡栏
- android - 如何解决“无效的密钥库异常”?