Skip to content

Tag: sql-server

Create A shift summary, with a shift spanning over 2 days

I have a data set like the sample below. I need to query and summarize the number of transactions per shift. The problem is that the shift cycle does not align with the normal hours of a day. Eg. The shift for day 2020-01-01 will start at 06:00 AM on 2020-01-01 and end at 06:00 AM on 2020-01-02 Below are

SQL unpivoting columns

How to unpivot table: to this: What I have so far is : but this is giving me count for all months. I need this with month number and count value for that month. Values 1-9 could be any numbers. Answer Got it working with

Remove same permutations from different multiple columns

Sample Data: Excepted ouput: The Logic : Remove the same val permutations and retain the min val1 data. e.g: Remove 1,1 because they’re same. Remove 2,1 because 1,2 exist…etc Data SQL Script : SQL Server 2012 | db<>fiddle Answer Here is one way to apply the two conditions:

Specify a specific year to be updated

I’m dealing with an old database that contains records of 365 days of the past 3 years. I’m trying to get the 2016 records ‘for example’ and change the year to 2020 and then add my new record to the table so it would contain now records of 4 years. this is what I did so far : I want