0

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

4

2 に答える 2

0

ここで、私はあなたの最初の要件を撮影しました:

最初に正しく構築されたユニットの総数を、構築されたユニットの総数で割った値を返します。これは、最初のパスの歩留まり数です。

SELECT COUNT(TestRecordID) as UnitsPassed,CAST(CAST(COUNT(TestRecordID) AS DECIMAL(6,3)) / (SELECT COUNT(TestRecordID) FROM TestRecords) AS DECIMAL(6,3)) as FirstPassYield
FROM TestRecords
WHERE PassFailStatus = 1 AND testDateTime > '12/06/2011' AND testDateTime < 12/07/2011'

CASTを機能させるためのより効率的な方法がおそらくあります。

于 2011-12-07T14:06:00.333 に答える
0

この質問は長すぎて、不要な情報が多すぎます。単に削除するのではなく、問題を解決するために行ったことを投稿することを選択しています。これは、誰かが実際に質問と回答の両方を読むのに十分な時間がある場合に備えて...

この SP は WCF サービスから呼び出され、結果のデータセットがサービスからクライアントに返されます。したがって、すべてを一度に行う必要はありません。私が計画しているのは、最初のパスで作成されたメモリ テーブルを、詳細レコードを作成する 2 番目のパスで使用することです。セッション ID は WCF サービスに返され、WCF サービスはレコード セットを読み取り、データをクライアントに返し、データベース内の作業レコードを削除します。

粗雑だが効果的。これを行うためのよりスマートな方法を思いついたら、戻ってきて投稿します。

楽しむ!!

于 2011-12-27T17:39:05.630 に答える