I'm using SQL Server 2016. I'm trying to figure out how to conditionally filter with a having clause.
I want to give me a list of all IDs, date, and username that if the date is before 2018 show me that users with 2-12 IDs, if the date is after 2018 then show me the users with 2-24 IDs. I can only think of unioning the codes but that increases the processing time, is there another way to do this quicker?
Original Code:
select [id], [date], [user name]
from [table]
where [date] < '2018-01-01'
group by [id], [date], [user name]
having count([id]) between 2 and 12) b
UNION Idea:
select [id], [date], [user name]
from [table]
where [date] < '2018-01-01'
group by [id], [date], [user name]
having count([id]) between 2 and 12) b
UNION
select [id], [date], [user name]
from [table]
where [date] >= '2018-01-01'
group by [id], [date], [user name]
having count([id]) between 2 and 24) b
Aucun commentaire:
Enregistrer un commentaire