Solved

Looping issues.

Posted on 2013-05-12
2
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Express connecting form remote error 26 7 65
Restrict result set 1 49
INSERT DATE FROM STRING COLUMN 18 67
Applying Roles in Common Scenarios 3 44
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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 …

752 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