question

skasthuri0717 avatar image
skasthuri0717 asked

last week range orders quantity

Hi, I am new to SQL training, I got a problem here. I have attached the data of two tables orders and order_items. where orders = o_id, c_id, o_date; order_items = o_id, it_id, it_qty; How can I solve the problem of getting total orders of last week when I run the query in this week(on any day). for example: I need the result of last week orders (i.e 8-feb-2-15 to 14-feb-2015) when i run the query today or tomorrow or any day of this week. I need a solution when I run the query on any day of this week, it should show me last weeks total orders, it should be dynamically. for example when I run the query next week of this month, it should display this week total orders and so on...., please help me with solution. thank you.
datetimedate-functionsweeks
data.jpg (20.8 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
Here are a [couple of examples][1] showing how to get this done. Also check out the [answer on this question][2]. One of those ought to work for you. [1]: http://www.sqlservercentral.com/Forums/Topic673397-338-1.aspx [2]: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/36af23fb-b6d3-459d-b07f-8f8c65731cdf/calendar-week-previous-week-last-year-week?forum=transactsql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.