Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My Pivot wont work

Status
Not open for further replies.

hwkranger

MIS
Nov 6, 2002
717
US
Says there's an error near the 'for' i'm tired and I can't see the issue.......

Code:
Select
 Date, StoreID, LaneID, DriveThruCarID, DriveOn, DriveOff, OrderNumber, OrderTotal,
 [OrderBoardDetector], [ServiceWindowDetector], [GreetDetector], [PaymentWindowDetector]

FROM
 (
			SELECT
			 Date,
			 DTC.StoreID,
			 SE.LaneID,
			 ServiceEventSource,
			 ServiceEventType,
			 MaxEventTime =MAX(SE.TimeStamp),
			 MinEventTime =Min(SE.TimeStamp),
			 DTCSE.DriveThruCarID,
			 --(DateDiff(s,Convert(datetime,Min(SE.TimeStamp),108),Convert(datetime,Max(SE.TimeStamp),108))),
			 --datediff(s,MIN(SE.TimeStamp), MAX(SE.TimeStamp)),
			 DriveOn,
			 DriveOff,
			 DTO.OrderNumber,
			 DTO.OrderTotal,
	         Goal=(SELECT TOP 1 TimeGoalSeconds FROM HAT_CENTRAL.dbo.fnc_BI_FetchGoal(Min(SE.TimeStamp), ServiceEventSource, '61D426D2-324E-4419-88B3-8EE24421C0F5'))
             
			From
			 QTime_ServiceEvent SE 
			  INNER JOIN QTime_DriveThruCarServiceEvent DTCSE
			 ON
			  SE.StoreID = DTCSE.StoreID
				AND
			  SE.ServiceEventID = DTCSE.ServiceEventID
			  Inner Join QTime_DriveThruCar DTC
			 ON
				DTC.StoreID = DTCSE.StoreID
				 and
				DTC.DriveThruCarID = DTCSE.DriveThruCarID
			  Inner Join Dates D
			 ON
			  DTC.DateID = D.DateID

			  inner join QTime_DriveThruCarOrder DTCO
			   ON
				DTC.StoreID = DTCO.StoreID
				 and
				DTC.DriveThruCarID = DTCO.DriveThruCarID

			  inner join QTime_DriveThruOrder DTO
			   ON 
				DTCO.StoreID = DTO.StoreID
				 and
				DTCO.OrderID = DTO.OrderID
			Group By
			 DTCSE.DriveThruCarID,
			 ServiceEventType,
			 ServiceEventSource,
			 DriveOn,
			 DriveOff,
			 SE.LaneID,
			 D.Date, 
			 DTC.StoreID, 
			 OrderNumber,
			 DTO.OrderTotal
	) SUB

PIVOT
(
  MaxEventTime
 for ServiceEventSource in (OrderBoardDetector, ServiceWindowDetector, GreetDetector, PaymentWindowDetector)
) A
 
I figured it out. I anyone cares -- you have to do an aggregate to pivot, and it has to be the name of the alias used in the main function....

Code:
Select
 Date, StoreID, LaneID, DriveThruCarID, DriveOn, DriveOff, OrderNumber, OrderTotal,
 [OrderBoardDetector], [ServiceWindowDetector], [GreetDetector], [PaymentWindowDetector]

FROM
 (
			SELECT
			 Date,
			 DTC.StoreID,
			 SE.LaneID,
			 ServiceEventSource,
			 ServiceEventType,
			 MaxEventTime =MAX(SE.TimeStamp),
			 MinEventTime =Min(SE.TimeStamp),
			 EventTime = (DateDiff(s,Convert(datetime,Min(SE.TimeStamp),108),Convert(datetime,Max(SE.TimeStamp),108))),
			 DTCSE.DriveThruCarID,
			 --(DateDiff(s,Convert(datetime,Min(SE.TimeStamp),108),Convert(datetime,Max(SE.TimeStamp),108))),
			 --datediff(s,MIN(SE.TimeStamp), MAX(SE.TimeStamp)),
			 DriveOn,
			 DriveOff,
			 DTO.OrderNumber,
			 DTO.OrderTotal,
	         Goal=(SELECT TOP 1 TimeGoalSeconds FROM HAT_CENTRAL.dbo.fnc_BI_FetchGoal(Min(SE.TimeStamp), ServiceEventSource, '61D426D2-324E-4419-88B3-8EE24421C0F5'))
             
			From
			 QTime_ServiceEvent SE 
			  INNER JOIN QTime_DriveThruCarServiceEvent DTCSE
			 ON
			  SE.StoreID = DTCSE.StoreID
				AND
			  SE.ServiceEventID = DTCSE.ServiceEventID
			  Inner Join QTime_DriveThruCar DTC
			 ON
				DTC.StoreID = DTCSE.StoreID
				 and
				DTC.DriveThruCarID = DTCSE.DriveThruCarID
			  Inner Join Dates D
			 ON
			  DTC.DateID = D.DateID

			  inner join QTime_DriveThruCarOrder DTCO
			   ON
				DTC.StoreID = DTCO.StoreID
				 and
				DTC.DriveThruCarID = DTCO.DriveThruCarID

			  inner join QTime_DriveThruOrder DTO
			   ON 
				DTCO.StoreID = DTO.StoreID
				 and
				DTCO.OrderID = DTO.OrderID
			Group By
			 DTCSE.DriveThruCarID,
			 ServiceEventType,
			 ServiceEventSource,
			 DriveOn,
			 DriveOff,
			 SE.LaneID,
			 D.Date, 
			 DTC.StoreID, 
			 OrderNumber,
			 DTO.OrderTotal
	) AS SUB

PIVOT
(
 MAX(EventTime) FOR ServiceEventSource in (OrderBoardDetector, ServiceWindowDetector, GreetDetector, PaymentWindowDetector)
) as A

Thanks again..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top