I am writing a SP in T-SQL to bring back 'first pass yield' metrics from test data stored in database tables on SQL Server 2008 R2. I have the SP written to return the basic data for graphing in the application, but I would like to add tooltips to provide detail for a particular time period when the user hovers over the segment of the graph.
Note - I am not asking how to do the UI part here, just how to get the data. UI stuff I'll deal with later...
Here's a simplified schema for storing the original data:
CREATE TABLE [dbo].[TestRecords](
[TestRecordID] [int] NOT NULL,
[HostName] [varchar](25) NOT NULL,
[UnitSerial] [varchar](20) NOT NULL,
[PassFailStatus] [bit] NOT NULL,
[AssyLineID] [int] NOT NULL,
[TestDateTime] [datetime] NOT NULL)
The idea is to return the total number of Units built correctly the first time divided by the total number of Units built -- this is the first pass yield number. We want to do that for any number of Assembly Lines (AssyLineID) over a somewhat arbitrary time period.
'Arbitrary' in this case means hourly for a given day or daily over a longer time period...
The resulting dataset is a table of records like this:
CREATE TABLE [dbo].[FpyValues](
[FpyValueID] [int] IDENTITY(1,1) NOT NULL,
[SessionID] [int] NOT NULL,
[DateTime] [datetime] NOT NULL,
[AssyLineID] [int] NOT NULL,
[Fpy] [float] NOT NULL,
[TotalUnits] [int] NOT NULL,
[FailedUnits] [int] NOT NULL)
So far so good, but the FPY value returned doesn't hold much information. For interesting events (relatively low or high FPY) the quality team would like to know what kinds of units they were building and what numbers were used to get the FPY -- without consulting yet another report. I could go back to the database when the tooltip is to be displayed, but the data won't be the same. The original dataset takes into account a unit that failed in an earlier time period and doesn't (erroneously) count it as a good unit in the current time period.
Here's a somewhat simplified version of my SP for getting hourly FPY for a given day:
ALTER PROCEDURE [dbo].[GetHourlyFpy]
@ProdLineList VARCHAR(100),
@ReportDate DATETIME
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Fpy FLOAT, @Total FLOAT, @Failed FLOAT
DECLARE @SessionID INT;
DECLARE @TempList TABLE
(
LineID INT
);
DECLARE @LineID VARCHAR(10);
DECLARE @LineName VARCHAR(16);
DECLARE @FailedUnits TABLE
(
UnitSerial VARCHAR(12)
);
DECLARE @Start INT, @End INT, @Current INT;
DECLARE @StartTime DATETIME, @EndTime DATETIME;
-- unpack incoming comma-separated list of Production Line IDs into temp table
-- get session ID to identify results for this session
-- get the start and end hour values (@Start and @End)
-- Get the Date part of the incoming DATETIME value (time = 00:00:00.000)
-- loop through all production lines, creating result records as we go
WHILE EXISTS(SELECT * FROM @TempList)
BEGIN
SELECT TOP 1 @LineID = LineID FROM @TempList;
-- clear the failed units table
DELETE FROM @FailedUnits;
-- set the start time for reporting
SET @StartTime = (SELECT DATEADD(Hh, @Start, @ReportDate));
-- loop through all 1-hour periods for the day
SET @Current = @Start;
WHILE @Current < @End
BEGIN
SET @EndTime = (SELECT DATEADD(Hh, 1, @StartTime));
SET @Total = (SELECT COUNT(DISTINCT tr.UnitSerial)
FROM TestRecords
WHERE @StartTime <= tr.TestDateTime
AND tr.TestDateTime < @EndTime
AND tr.AssyLineID = @LineID
AND (NOT EXISTS
(SELECT UnitSerial FROM @FailedUnits f WHERE tr.UnitSerial = f.UnitSerial)));
SET @Failed = (SELECT COUNT(DISTINCT tr.UnitSerial)
FROM TestRecords tr
WHERE @StartTime <= tr.TestDateTime
AND tr.TestDateTime < @EndTime
AND tr.PassFailStatus = 0
AND tr.AssyLineID = @LineID
AND (NOT EXISTS
(SELECT UnitSerial FROM @FailedUnits f WHERE tr.UnitSerial = f.UnitSerial)));
-- populate the failed units list as needed
INSERT INTO @FailedUnits
SELECT DISTINCT tr.UnitSerial
FROM dbo.TestRecords tr
LEFT OUTER JOIN
@FailedUnits f ON tr.UnitSerial = f.UnitSerial
WHERE @StartTime <= tr.TestDateTime
AND tr.TestDateTime < @EndTime
AND tr.PassFailStatus = 0
AND tr.AssyLineID = @LineID
AND f.UnitSerial IS NULL;
IF (0 = @Total)
SET @Fpy = 0;
ELSE
SET @Fpy = (@Total - @Failed) / @Total;
INSERT INTO dbo.FpyValues (SessionID, [DateTime], ProductionLine, Fpy, TotalUnits, FailedUnits)
VALUES(@SessionID, @StartTime, @LineID, @Fpy, @Total, @Failed);
SET @StartTime = (SELECT DATEADD(Hh, 1, @StartTime));
SET @Current = @Current + 1;
END
-- we're done with this production line
DELETE FROM @TempList WHERE LineID = @LineID;
END
RETURN @SessionID;
END
I need a way to populate a table with detail for every assembly line for each time period as follows:
CREATE TABLE [dbo].[FpyUnits](
[FpyUnitID] [int] IDENTITY(1,1) NOT NULL,
[FpyValueID] [int] NOT NULL,
[DateTime] [datetime] NOT NULL,
[AssyLineID] [int] NOT NULL,
[UnitType] [varchar](25) NOT NULL,
[TotalUnits] [int] NOT NULL,
[FailedUnits] [int] NOT NULL)
Note I need to create and save the master/parent record to disk prior to saving the detail records, so I have the foreign key value (FpyValueID).
One way I can imagine is to change how I calculate the original data and SUM data from the detail records to calculate the overall FPY values. I can also see where I might need to use the GROUP BY directive to get the detail values.
Does anyone have suggestions for how to construct the SQL queries to pull this data back without adding more looping? This is really long already, so I'll quit here. If you need more info, please ask...
Thanks in advance for any ideas / assistance, Dave
This question is too long and contains too much unnecessary information. Rather than just delete it, I am choosing to post what I did to resolve the problem, just in case someone has enough spare time on their hands to actually read both the question and the answer...
This SP will be called from a WCF service, and the resulting dataset returned to the client from the service. Thus, there is no need to do everything all at once. What I plan to do is use memory table created in the first pass in a second pass that will create the detail records. The session ID is returned to the WCF service, which then reads the record set, returns the data to the client and deletes the work records in the database.
Crude but effective. If I come up with a slicker way of doing this I will come back and post it.
Have fun!!