Solved

Best practice on "CASE" script

Posted on 2014-03-31
2
211 Views
Last Modified: 2014-03-31
The script i have works but very long and i have run into a "CASE" statement limit so i can not go as far back in history as i would like.  Here is a example of what i am currently using.
SELECT ITEM_NO, 
	CASE 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 7 THEN 'Week_01' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 14 THEN 'Week_02' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 21 THEN 'Week_03' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 28 THEN 'Week_04' 
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 35 THEN 'Week_05'
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 42 THEN 'Week_06'
		WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 49 THEN 'Week_07' 
                WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 56 THEN 'Week_08' 
                WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 63 THEN 'Week_09' 
                WHEN datediff(DD, [BUS_DAT], GETDATE()) <= 70 THEN 'Week_10' 
		ELSE NULL 
	END AS SALE_WEEK, QTY_SOLD
FROM dbo.PS_TKT_HIST_LIN

Open in new window


What this is doing is labeling each line according to what week the sale falls in to by counting back 7 day from the current date to create week one and so on and so forth.  I'm looking to a way to not use so many when clauses and i can look back 3 or 4 years if i want.

Thank you for your help and let me know if you have any further questions.
0
Comment
Question by:Littlet5621
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39967651
SELECT ITEM_NO,
   'Week_' + RIGHT('0' + CAST(DATEDIFF(DAY, BUS_DAT, GETDATE()) / 7 + 1 AS VARCHAR), 2),
   ...
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39967790
Hi,

select dateadd( week, (datediff( week, 0, getdate()), 0 ) should give the date at the start of the week.

You may need to change the 0 to -1 or something as the base datetime. Depends on what you want as the starting day for the week.

HTH
  David
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question