Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a Table with columns ProductId, DateofPurchase, Quantity. 

ProductId DateofPurchase Quantity
1245       10/03/2018      50
4577       23/03/2018      100

I have a parameter in where clause which is dateofpurchase based on this i get the quanity as below

Select * from table where dateofpurchase='23/03/2018' and the result is 100
But I want the result as follows.
Quantity should be decided in which week it belongs to.

ProductId Week1  Week2  Week3  Week4
4577         -      -    100    -

Here the weeks are decided by 
1-7 is week1,8-15 week2,16-23 week3, 24-30 week4

Please Suggest.

What I have tried:

Based on date parameter the week should be displayed
Posted
Updated 31-Jul-18 3:21am

1 solution

The first thing you need to do is work out what the day bit of the date is ... for that you can use DATEPART[^]

For example:
SQL
SELECT *, datepart(D, DateOfPurchase) AS [day] from [table]
which gives you
ProductId	DateOfPurchase	Quantity	day
1245		2018-03-10	50		10
4577		2018-03-23	100		23
You can then use that [day] in a test using CASE[^] with BETWEEN[^]

For example (incomplete)
SQL
Select  
	Week1 = case when datepart(D, DateOfPurchase) BETWEEN 1 AND 7 THEN Quantity
		ELSE NULL END,
	Week2 = case when datepart(D, DateOfPurchase) BETWEEN 8 AND 15 THEN Quantity
		ELSE NULL END, ...
 
Share this answer
 
Comments
Member 13867163 1-Aug-18 0:12am    
Great Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900