I have a table with 3 columns – user_id, building_id, datetime where each record is an access to a specific building and hence users can accesss a building many times. I need to select each user_id from each day who has accessed a specific building. It would seem like I need to cast the datetime to just a day and do a distinct across user_id, building_id and day. How would I do this in a modern version of SQL Server?
Sql-server – select users who do something on each date in sql server
sql server
Related Question
- Sql-server – How to use MAX() to select a single record but prevent GROUP BY from selecting all distinct values and multiple records
- Sql-server – SQL Server 2016: Adding temporal tables to existing application
- Sql-server – Select Count (case when)!
- Sql-server – On SQL Server, is it possible to restrict certain users from using certain functions, operators or statements
Best Answer
The query to list the users and dates for a given building (defined by
@BuildingID
):Beyond that, the requirement is not clear. In the title you say "on each date" - if you have a set of dates and you want the users who accessed
@BuildingID
on all of those dates, one approach could be:Not sure if you are querying the data more often than you are writing it, but this might be a use case for an indexed view.