Solved

Looping issues.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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