Hi We get requests by selecting a certain frequency. Based on the frequency selected by the user we generate records for that request until an expiration date. We currently support monthly and quarterly frequency for which we have a working query . We are currently planning to give users option of selecting weekly and bi weekly frequency for which I need to write a query but having problems.
we are currently using the below query that will generate records monthly based on the number of months that i calculate between two date fields for a givenRequest Id. The date fields that I use are Received Date Fieldand Expiration Date Field.
How can i use the same query to generate records for weekly and bi weekly based on theReceived Date Field and Expiration Date Field that i use in the subtraction for calculating the number of months.
Also when inserting monthly records I have been adding a month as we always give an additional report after the expiration date . SET NOCOUNT ON GO declare
@num_of_times int declare @count int declare @frequency varchar(10) declare @num_of_times1 int DECLARE @oldrequestid varchar(50),@newrequestid varchar(50) DECLARE db_cursor CURSOR FOR SELECT
Requestid from Request_Customer where requestid in (149016) OPEN db_cursor FETCH NEXT FROM db_cursor INTO @oldrequestid WHILE @@FETCH_STATUS = 0 BEGIN --do
work here SET
@num_of_times = NULL select @num_of_times=datediff(month, receiveddate,expirationdate) from Request_Customer where requestid in (@oldrequestid) SET @num_of_times1 = @num_of_times+1 set @count=0 WHILE @count < @num_of_times1 BEGIN update
table_keys set key_id = key_id + 1 where table_name = 'adhoc' Select @newrequestid = key_id from table_keys where table_name = 'adhoc' INSERT INTO [dbo].[renoffcyc] ([roc_id] ,[prodmth] ,[opa_id] ,[freqcd] ,[entereddt] ,[rptdesc] ,[groupname] ,[origrequestid] ) SELECT @newrequestid ,convert(varchar(30),DATEADD (month , @count+1 ,RR.Receiveddate)) ,SR.Assignto ,RR.defineschedule ,convert(varchar(30),DATEADD (month , @count+1 ,RR.Receiveddate)) ,SR.rptdesc ,RR.CTNAME ,@oldrequestid FROM dbo].[Request_Customer] RR INNER JOIN SELECTED_CUSTOMER SR ON RR.requestid = SR.requestid where RR.requestid = @oldrequestid set @count=@count+1 END FETCH NEXT FROM db_cursor INTO @oldrequestid END -- Cursor loop CLOSE db_cursor
Example:
So for example a request comes in Jan 2014 with frequency selected as monthly and the expiration date is Dec 2014. we
have to send the user monthly reports until Jan 2015 for which my monthly logic submits requests and one of our team member uses that automatic request generated by the query to process them monthly.
Sample Data Original Request Requestid Receiveddate frequency assignedanalystid requesttypeid CustomerName CompletedDate ExpirationDate 123456 12/31/2013 Monthly 123 34 Testing Company 1/15/2014 12/31/2014
Monthly Offcycles for original request 123456 Roc_id ProductionMonth AssignedAnalystid freqcd entereddt rptdesc groupname origrequestid 123457 2/1/2014 123 Monthly 2/1/2014 34 Testing Company 123456 123458 3/1/2014 123 Monthly 3/1/2014 34 Testing Company 123456 123459 4/1/2014 123 Monthly 4/1/2014 34 Testing Company 123456 123460 5/1/2014 123 Monthly 5/1/2014 34 Testing Company 123456 123461 6/1/2014 123 Monthly 6/1/2014 34 Testing Company 123456 123462 7/1/2014 123 Monthly 7/1/2014 34 Testing Company 123456 123463 8/1/2014 123 Monthly 8/1/2014 34 Testing Company 123456 123464 9/1/2014 123 Monthly 9/1/2014 34 Testing Company 123456 123465 10/1/2014 123 Monthly 10/1/2014 34 Testing Company 123456 123466 11/1/2014 123 Monthly 11/1/2014 34 Testing Company 123456 123467 12/1/2014 123 Monthly 12/1/2014 34 Testing Company 123456 123468 1/1/2015 123 Monthly 1/1/2015 34 Testing Company 123456
Below is sample data for original request and the off cycles that I would like help to generate weekly offcycles and Bi weekly Offcycles
Original Request Requestid Receiveddate frequency assignedanalystid requesttypeid CustomerName CompletedDate ExpirationDate 123654 12/31/2013 Weekly 123 34 Testing Company 1/15/2014 12/31/2014
Weekly Offcycles for original request 123456 Roc_id ProductionMonth AssignedAnalystid freqcd entereddt rptdesc groupname origrequestid 123655 1/22/2014 123 Weekly 1/22/2014 34 Testing Company 123654 123656 1/29/014 123 Weekly 1/29/014 34 Testing Company 123654 123657 2/4/2014 123 Weekly 2/4/2014 34 Testing Company 123654 123658 2/11/2014 123 Weekly 2/11/2014 34 Testing Company 123654 123659 2/18/2014 123 Weekly 2/18/2014 34 Testing Company 123654 123660 2/25/2014 123 Weekly 2/25/2014 34 Testing Company 123654 123661 3/4/2014 123 Weekly 3/4/2014 34 Testing Company 123654 123662 3/10/014 123 Weekly 3/10/014 34 Testing Company 123654 123663 3/17/2014 123 Weekly 3/17/2014 34 Testing Company 123654 123664 3/24/2014 123 Weekly 3/24/2014 34 Testing Company 123654 123665 3/31/2014 123 Weekly 3/31/2014 34 Testing Company 123654 123666 4/7/2014 123 Weekly 4/7/2014 34 Testing Company 123654 123667 4/11/2014 123 Weekly 4/11/2014 34 Testing Company 123654 123668 4/18/2014 123 Weekly 4/18/2014 34 Testing Company 123654 123669 4/25/2014 123 Weekly 4/25/2014 34 Testing Company 123654<//span> _ __ __
Note: In the first sample data the monthly records can be generated using the query I have based on received date and expiration date and I need help to generate weekly . I have only listed sample data until end of April 2014.<//span> I would like help on generating records weekly and biweekly for a certain request based on the received date feild and expiration date feild of that request. |