?
Solved

Looping issues.

Posted on 2013-05-12
2
Medium Priority
?
177 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

718 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