结束(分区)麻烦

时间:2014-02-28 13:02:53

标签: sql sql-server tsql

任何人都可以在我的案例陈述中使用OverPartition帮助

我正在尝试Y显示Stvs.visitorID而没有stse.SessionID

我的第一行工作正常,但无法让第二行工作。

сase 
    when stse.datetimeselected < max(stse.datetimeselected) Over (partition by Stvs.visitorID) then 'N'
    when stse.SessionID is null Over (partition by Stvs.visitorID) then 'Y'
end as 'Y/N'

2 个答案:

答案 0 :(得分:0)

CASE语句中的每一行都是单独评估的。因此,您根本不需要OVER子句。

如果您不保证不ELSE 'Y',请考虑使用NULLs

我的版本:

Case 
when stse.datetimeselected < max(stse.datetimeselected) Over (partition by Stvs.visitorID) then 'N'
when stse.SessionID is null then 'Y'
END as 'Y/N'

答案 1 :(得分:0)

尝试以下......

Case 
when stse.datetimeselected < max(stse.datetimeselected) Over (partition by Stvs.visitorID) then 'N'
when SUM(stse.SessionID) Over (partition by Stvs.visitorID)  is null then 'Y'
END as 'Y/N'

OR

Case 
when stse.datetimeselected < max(stse.datetimeselected) Over (partition by Stvs.visitorID) then 'N'
when COUNT(stse.SessionID) Over (partition by Stvs.visitorID)  = 0 then 'Y'
END as 'Y/N'

或使用以下链接中的任何WINDOWING FUNCTIONS代替COUNT(),SUM(),MAX() Over Clause