Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to determine the earliest "schedule_start_datetime" for a group of SKUs via a script load

In the example below, does anyone know how to determine the earliest "schedule_start_datetime" for a group of SKUs via a script load (not a chart). I would like to create a new column "MinSchedStartDateTime" when I load this SQL data into a resident table. Thanks!

Schedule_Create_DateUpdate_DateBranch_PlantProduction_LineSKUSchedule_Start_Datetime
6/4/2010 0:006/7/2010 0:007103011100711790181486/4/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711790116996/4/2010 5:12
6/4/2010 0:006/7/2010 0:007103011100711790116996/7/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711790016906/8/2010 8:00
6/4/2010 0:006/7/2010 0:007103011100711790016906/11/2010 12:01
6/4/2010 0:006/7/2010 0:007103011100711790016906/14/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711790116996/16/2010 21:49
6/4/2010 0:006/7/2010 0:007103011100711790181486/19/2010 18:06
6/4/2010 0:006/7/2010 0:007103011100711790181486/21/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711790181486/28/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711790181487/5/2010 12:01
6/4/2010 0:006/7/2010 0:007103011100711794300187/6/2010 8:28
6/4/2010 0:006/7/2010 0:007103011100711792802247/11/2010 21:25
6/4/2010 0:006/7/2010 0:007103011100711792802247/12/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711790181487/13/2010 13:48
6/4/2010 0:006/7/2010 0:007103011100711790181487/19/2010 0:00
6/4/2010 0:006/7/2010 0:007103011100711794300187/26/2010 10:12
6/4/2010 0:006/7/2010 0:007103011100711794300187/28/2010 12:01
6/4/2010 0:006/7/2010 0:007103011100711790181487/29/2010 17:53


5 Replies
Not applicable
Author

Should be this:


Left Join (TheTableName)
LOAD SKU, min(Schedule_Start_Datetime) as MinScheduleStartDatetime
Resident TheTableName
Group by SKU;


Not applicable
Author

That won't work because there are mulitple groups of the same SKU. Notice the data is ordered by Schedule_Start_datetime. So I need to calculated the first start date time, when a "group" of SKU starts. So for SKU ending in 8148, I need to calculated 4 different min start dates.

Not applicable
Author

Create a new field called SKU_Group which is

right(SKU,4) as SKU_GROUP

Then you can follow Nick's example replacing references to SKU with SKU_GROUP

Not applicable
Author

Thank's for the query, but I don't think I'm understanding what needs to be done here. Here is the detail of whay I am trying to do, and I am focused on step 3

-- Step 1 Calculate minimum Update_Date for each Schedule_Create_Date and Branch_Plant
SELECT
,[Schedule_Create_Date]
,[Update_Date]
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]
,[SKU]
,[Scheduled_Cases]
,min([Update_Date]) AS Update_Date_Min
INTO #Temp1
FROM [Fg_DataMart].[dbo].[Daily_Production_Schedule_History]
WHERE SKU <> 'Shutdown'
GROUP BY
,[Schedule_Create_Date]
,[Update_Date]
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]
,[SKU]
,[Scheduled_Cases]
ORDER BY
[Schedule_Create_Date]
,[Update_Date] ASC
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]

-- Step 2 Filter out all records that do not meet the criteria below.
SELECT
,[Schedule_Create_Date]
,[Update_Date_Min] AS Update_Date
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]
,[SKU]
,[Scheduled_Cases]
INTO #Temp2 FROM #Temp1 WHERE Update_Date_Min = Update_Date
ORDER BY
[Schedule_Create_Date]
,[Update_Date_Min]
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]

-- Step 3 Find the minimum Schedule_Start_Datetime for each group of SKUs returned from the following query
SELECT
*
-- ,???? AS SKU_Run_Schedule_Start_Datetime_Min
INTO #Temp3
FROM #Temp2
ORDER BY
[Schedule_Create_Date]
,[Update_Date_Min]
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]

-- Step 4 Calculate Production Run ID
SELECT
*
,Schedule_Create_Date + '_' + SKU_Run_Schedule_Start_Datetime_Min AS Production_Run_ID
FROM #Temp3
ORDER BY
[Schedule_Create_Date]
,[Update_Date_Min]
,[Branch_Plant]
,[Production_Line]
,[Schedule_Start_Datetime]

Not applicable
Author

Forget about your DB SQL. We are talking here about QV script.

You need to define a group field and fill it by appropriate data for your grouping calculation. And after that, you need to run a grouping statement. It could be something like this:

<pre>Left Join (TheTableName)
LOAD SKU, Left(SKU, Len(SKU) - 4) as SKU_Group
Resident TheTableName;

Left Join (TheTableName)
LOAD SKU_Group, min(Schedule_Start_Datetime) as MinScheduleStartDatetime
Resident TheTableName
Group by SKU_Group;


You need to know what the grouping logic is for your task.