Solved

Looping issues.

Posted on 2013-05-12
2
170 Views
Last Modified: 2013-05-12
Hi,

Thanks to some of the people here I have managed to get a query almost working to perfection although I'm having a few issues with getting a loop to work correctly.   Essentially there are two inserts as part of the loop the first identifies a customer with an incident and then adds a time interval and finds the next incident after that time interval which is working like a charm however I now need to cross reference another table to find the last customer interaction before the interval.  so All I did was add the following code into my loop (full code at the bottom)

INSERT INTO tbOutputPrev
SELECT top 1 IncidentDateTime AS IncidentTime, atvcCustId AS CustomerID, atvcCustFullName AS CustomerName, IncidentLevel AS Result, atvcLocationName AS Location
FROM tbIncidents2
WHERE atvcCustId = @CustID
AND IncidentDateTime < @PrevDate
ORDER by IncidentDateTime ASC

Open in new window


So I'm taking setting the @PrevDate  with a new date each loop but it seems to only pull back one single customer ID/Timedate combo but also does it 300 times (which is the number of times I have the loop running for the first part)

Any ideas?

Full code
Line 86 is where the above code starts
BEGIN

IF OBJECT_ID('tbIncidents') IS NOT NULL Drop TABLE tbIncidents
IF OBJECT_ID('tbIncidents2') IS NOT NULL Drop TABLE tbIncidents2
IF OBJECT_ID('tbTotal') IS NOT NULL Drop TABLE tbTotal
IF OBJECT_ID('tbOutput') IS NOT NULL Drop TABLE tbOutput
IF OBJECT_ID('tbOutputPrev') IS NOT NULL Drop TABLE tbOutputPrev


DECLARE @StartDate datetime
DECLARE @EndDate  datetime
DECLARE @Report int


SET @Report = (SELECT MAX(pki4Logon) FROM tbTempReport)
SET @StartDate = (SELECT atdtDateFrom FROM tbTempReport WHERE pki4Logon = @Report)
SET @EndDate = (SELECT atdtDateTo FROM tbTempReport WHERE pki4Logon = @Report)



CREATE TABLE tbIncidents (IncidentDateTime datetime, atvcCustId Nvarchar(20), atvcCustFullName NvarChar(50),IncidentLevel decimal(3,1), atvcLocationName varchar(255))
CREATE TABLE tbIncidents2 (IncidentDateTime datetime, atvcCustId Nvarchar(20), atvcCustFullName NvarChar(50),IncidentLevel decimal(3,1), atvcLocationName varchar(255))
CREATE TABLE tbTotal (IncidentDateTime datetime, atvcCustId Nvarchar(20), atvcCustFullName NvarChar(50),IncidentLevel decimal(3,1), atvcLocationName varchar(255))
CREATE TABLE tbOutput (IncidentDateTime datetime, atvcCustId varchar(20),atvcCustFullName NvarChar(50), IncidentLevel Decimal(3,1), atvcLocationName varchar(50)) 
CREATE TABLE tbOutputPrev (IncidentDateTime datetime, atvcCustId varchar(20),atvcCustFullName NvarChar(50), IncidentLevel Decimal(3,1), atvcLocationName varchar(50)) 

INSERT INTO  tbIncidents
SELECT     IncidentDateTime AS IncidentTime, atvcCustId AS CustomerID, atvcCustFullName AS CustomerName, IncidentLevel AS Result, atvcLocationName AS Location
FROM         tbCustomerData
WHERE     (IncidentType = 'PAT') AND (ProductType = '0') AND (IncidentDateTime >= @StartDate) AND (IncidentDateTime <= @EndDate)AND (IncidentLevel < '4')
ORDER BY atvcCustId, IncidentDateTime

INSERT INTO  tbIncidents2
SELECT     IncidentDateTime AS IncidentTime, atvcCustId AS CustomerID, atvcCustFullName AS CustomerName, IncidentLevel AS Result, atvcLocationName AS Location
FROM         tbCustomerData
WHERE     (IncidentType = 'PAT') AND (ProductType = '0') AND (IncidentDateTime >= @StartDate) AND (IncidentDateTime <= @EndDate)
ORDER BY atvcCustId, IncidentDateTime

INSERT INTO  tbTotal
SELECT     IncidentDateTime AS IncidentTime, atvcCustId AS CustomerID, atvcCustFullName AS CustomerName, IncidentLevel AS Result, atvcLocationName AS Location
FROM         tbCustomerData
WHERE     (IncidentType = 'PAT') AND (ProductType = '0') AND (IncidentDateTime >= @StartDate) AND (IncidentDateTime <= @EndDate)

END



DECLARE @Report1 int
DECLARE @SLA int
DECLARE @StartDate1 datetime
DECLARE @PrevDate datetime
DECLARE @EndDate1 datetime 
DECLARE @Counter int	
DECLARE @RowNum int
DECLARE @CustID varchar(15)
DECLARE CustList cursor for

		
select DISTINCT atvcCustId from tbIncidents
				OPEN CustList
	FETCH NEXT FROM CustList 
	INTO @CustId
	set @RowNum = 0 
	WHILE @@FETCH_STATUS = 0
BEGIN
	SET @RowNum = @RowNum + 1

--To Do
--Counter to be total test number?
SET @Report1 = (SELECT MAX(pki4Logon) FROM tbTempReport)
SET @StartDate1 = (SELECT atdtDateFrom FROM tbTempReport WHERE pki4Logon = @Report1)
SET @PrevDate = @StartDate1
SET @EndDate1 = (SELECT atdtDateTo FROM tbTempReport WHERE pki4Logon = @Report1)
SET @SLA = (SELECT atvcValue FROM tbTempReportCriteriaValues WHERE pki4Logon = @Report1)*60+1
SET @Counter = 300
WHILE @Counter > 0

BEGIN

INSERT INTO tbOutput
Select top 1 *
FROM tbIncidents
WHERE atvcCustId = @CustID
AND IncidentDateTime > @StartDate1  AND IncidentDateTime < @EndDate1

INSERT INTO tbOutputPrev
SELECT top 1 IncidentDateTime AS IncidentTime, atvcCustId AS CustomerID, atvcCustFullName AS CustomerName, IncidentLevel AS Result, atvcLocationName AS Location
FROM tbIncidents2
WHERE atvcCustId = @CustID
AND IncidentDateTime < @PrevDate
ORDER by IncidentDateTime DESC

SET @StartDate1 = DateAdd(mi, + 241, isnull((select max(IncidentDateTime) from tbOutput WHERE atvcCustId = @CustID),'19000101'))
SET @PrevDate = @StartDate1
SET @Counter = @Counter -1
END



SELECT *, convert(VARCHAR(19),IncidentDateTime) AS NamedDate
FROM tbOutput
order by atvcCustId, IncidentDateTime

	  FETCH NEXT FROM CustList 
	    INTO @CustId
	END
	CLOSE CustList
	DEALLOCATE CustList

Open in new window

0
Comment
Question by:Litation
  • 2
2 Comments
 

Author Comment

by:Litation
ID: 39159905
Well I think I've worked out what my issue is its line 96
SET @PrevDate = @StartDate1

What I need to do is make that date the date/time of the test the loop finds NOT make it the same as the @StartDate1 as thats just he starting point where it looks for the next incident.
0
 

Accepted Solution

by:
Litation earned 0 total points
ID: 39159931
Solved it by adding an Id column to tbOutput and then adding @Value variable to get the Id and then getting the @PrevDate from that.

If someone has a more elegant solution let me know....this feels like a duct tape a chewing gum solution.


SET @Value = (SELECT MAX(id)FROM tbOutput)
SET @PrevDate = (SELECT atdtTestDateTime FROM tbOutput WHERE id = @Value)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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