简体   繁体   中英

Over (partition by) troubles

Can anybody help with Over and Partition in my case statement

I'm trying to show Y for Stvs.visitorID with no stse.SessionID 's.

My 1st line works fine but Can cant get the 2nd line to work.

с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'

Every line in CASE statement is evaluated separately. Therefore you do not need OVER clause at all.

Also consider using ELSE 'Y' , if you want to guarantee no NULLs .

My version:

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'

Try the follwing.....

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'

OR Use any of the WINDOWING FUNCTIONS instead of COUNT(),SUM(),MAX() FROM the follwing link Over Clause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM