Solved

SQL Help - loop through dates

Posted on 2013-06-11
2
258 Views
Last Modified: 2013-06-17
Trying to create a query which will retrieve information and store in a temp table so I can report on the values.  The report needs to show the information by each Friday.  I am creating a temp table which adds each Friday by date

Like this.

reportdate                      source      item_no      quantity      trx_dt      promise_dt
2013-06-14 00:00:00.000      NULL      NULL      NULL      NULL      NULL
2013-06-21 00:00:00.000      NULL      NULL      NULL      NULL      NULL
2013-06-28 00:00:00.000      NULL      NULL      NULL      NULL      NULL

However, my other data doesn't fall into each Friday order.

source      item_no                                        quantity      doc_type      trx_dt
P      ITK2236                             60.0000      O      2013-06-14 00:00:00.000
O      ITK2236                             14.0000      A      2013-06-14 00:00:00.000
P      ITK2236                             60.0000      O      2013-06-17 00:00:00.000
P      ITK2236                             60.0000      O      2013-06-19 00:00:00.000
O      ITK2236                             15.0000      A      2013-06-21 00:00:00.000
O      ITK2236                             27.0000      A      2013-07-26 00:00:00.000


How can I loop, cursor, whatever through these records and sum the values of the quantity by the trx_dt by the appropriate Friday.  This will only run by 1 item_no at a time, but there could be muliple records per trx_dt.

Any help would be appreciated?
0
Comment
Question by:red_75116
2 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39239758
try this. add relevant columns wherever you want.
select DATEADD(dd,4,DATEADD(wk, DATEDIFF(wk,0,trx_dt), 0)) reportdate,
       NULL source,
	   item_no,
	   sum(quantity) quantity
  from your_table
 group by DATEADD(dd,4,DATEADD(wk, DATEDIFF(wk,0,trx_dt), 0)),item_no

Open in new window

0
 

Author Closing Comment

by:red_75116
ID: 39253154
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …

713 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