Admitted SQL newbie here but I’m working on a query where the request just got more complicated. Long short of it is that I have a table with a few columns. One of which is a date column.
I’m writing a query to return a set of columns along with adding a new column based on the date range (<=90 Days, 91-180, 180+). This is what I have so far. It works for the first 90 but I’m having a hard timing nesting the other select commands to get the other data sets.
Select Key, Name, 'C90' as Code
From LD_List
WHERE DATEDIFF(DAY,[LAST_PURCHASE_DATE],GETDATE()) <= 90
I appreciate the help.
If you are using TSQL, SQL Server or MYSQL you may use,
CASE WHEN: Key is a resreved keyword. So use squre brackets. In my sample I have used key as id.Query: SQL Server version:
Results:
EXTRA 🙂
Now if you need a pivot like the following: Check this * SQLFIDDLE Demonstration out
Results: