Solved

Looping issues.

Posted on 2013-05-12
2
168 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now