I have this query that I need to graph. The graph will show Total patients, patient Longest wait (minutes) and patient Median wait for each location. I need to include Medians in my query, and that is where I am stuck. Example Raw Data: Query I have so far w/o Medians: Output: I need help getting a last colum…
Tag: sql-server
SQL Query for multiple Pivot on same column
As per my requirement, I need to apply pivot twice on same column to get different values as column header. Please see below how the data is present in table and expected results. Table Question Response TranslatedResponse activityid createdon Reason Testing testaus 1 01-01-2022 EMail abc@gmail.com abc@gmail.…
T-SQL :: TRUNCATE or DELETE all tables in schema
I need to TRUNCATE or DELETE all tables in schema. I found this code: but on AdventureWorks it gives me: So I found this alternative code: But the result is the same: How to TRUNCATE or DELETE all tables in schema? Answer You simply need to wrap your “delete from all the tables” script with a R…
How to combine information from a main table and a log table in SQL Server
I currently have 2 tables in my database that i need to combine information from. One of them is a log table, that registers the status change of all products in the system. Structure example: ChangeDate Product LastStatus NewStatus 2021-10-01 A New Aging 2021-11-02 A Aging Ressuply 2021-11-25 A Ressuply OFF …
Count all the entity between periods – SQL Server
I want to count all the entities that created between August 1 until May 31 for each year. (academic year) I am successful counting it monthly, but it’s not good enough: Can someone help me with this? Thanks. Answer you can try this : Explanation : when the month is small than 6 then it’s part of …
Data query to keep the common data
I have a table Table 1 : It the final table contains all data . ID and IDS are composite key table 2 : Its a pre final table which will upcoming data with incomplete information EXPECTED RESULT : Some IDs (ID+IDS) are in Table 1 and some are in Table 2 . I need to compare the data of
How to improve Clustered Index scan to Clustered Index seek?
I have two tables with primary clustered keys by id. Also I have stored procedure The Execution plan shows me a ‘Clustered Index Scan’ that I want to avoid. Is there any way I can convert it to a ‘Clustered Index Seek’? Execution Plan screen <db fiddle> Answer As I mentioned in t…
Timeserial not recognized built in function in MS SQL
I am trying to implement an SQL query that gets records between today’s fixed timing 18:00 and yesterday’s fixed timing 18:00 based on a Date time column that I have in my table. I tried this query But, it’s throwing an error Timeserial is not a recognized built-in function name. Any ideas p…
Select max entries in a range in SQL server
I have a salary column where I have to select between range of 10000 and 20000 and also the top rows of max salary. Column I have: Rows I want to select I can use top n rows but that will apply on to this column. What I want to do is, say if this column has 2 rows that
Remove minutes and seconds and then convert to UTC timezone
I have column with time stamp data like this 2021-12-09 08:01:00.520 I want to remove the minutes and second and then convert the date to UTC timezone in one line. I tried this code I get this result 2021-12-09 08:00:00.000 and I tried this code I get this result 2021-12-09 16:01:00.520 What I expected is 202…