Skip to content

Tag: tsql

SQL consolidate overlapping dates based on criteria

I’m trying to merge overlapping dates between Admit and discharge dates of patients. There are a few edge cases which I couldn’t cover in the query. Input Expected Output Query I used the logic that was here But this doesn’t cover the edge case for ID 2 and 3. Also the subquery is slower whe…

Compare two tables via the three tables SQL

I plan to compare two tables via the three table. my query is as the following if the count is zore, then the tableA and TableC match, otherwise, these two tables do not match It takes a long time to run the query. Do we have a way to compare tableA and tableC fast? Question: How to compare tableA and

Add rows accumulated by current month

Is there a way to translate my hard coded SQL script into a dynamic? My problem with this code is that, it still adding the SEPT-DEC it should be zero since we are not yet covered this month My table Expected Result: Starts from JAN then Added the succeeding covered months. Answer You can UNPIVOT your data, u…

SQL DATEPART(qq, @date) date quarter start October

I have a query that prints the data grouped quarter, the query uses datepart(qq,repo.lesson_date) SQL function. My issue is I want my first quarter to be October-December and not Jan-March like the output I get from the SQL function above. Is there anyone can assist? Answer You may use an additional calculati…

Round to nearest 15 minute interval

I’ve seen this question asked and answered for time that is stored as a date, but I have duration of hours and minutes stored as numeric(4,2). For example 2.12, is 2 hours 12 minutes. I need to round that to the nearest 15 minute interval, doing this (CONVERT([numeric](4,2),round(2.12/(25),(2))*(25))) d…

T SQL Count and Group by calculable column

This code gives the following table cntqueue cntdate cntINKTONERBLACK signalcolumn Queue01 2001-04-04 3 0 Queue01 2001-04-05 1 0 Queue01 2001-04-06 100 1 Is there a way to count ‘1’ in signal column and group 1st column to have? cntqueue NumberOfJumps Queue01 1 Answer You can use a subquery: