Solved

SQL Help - loop through dates

Posted on 2013-06-11
2
256 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 40

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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