Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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_Date | Update_Date | Branch_Plant | Production_Line | SKU | Schedule_Start_Datetime |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 6/4/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179011699 | 6/4/2010 5:12 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179011699 | 6/7/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179001690 | 6/8/2010 8:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179001690 | 6/11/2010 12:01 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179001690 | 6/14/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179011699 | 6/16/2010 21:49 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 6/19/2010 18:06 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 6/21/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 6/28/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 7/5/2010 12:01 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179430018 | 7/6/2010 8:28 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179280224 | 7/11/2010 21:25 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179280224 | 7/12/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 7/13/2010 13:48 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 7/19/2010 0:00 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179430018 | 7/26/2010 10:12 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179430018 | 7/28/2010 12:01 |
6/4/2010 0:00 | 6/7/2010 0:00 | 710301 | 1 | 10071179018148 | 7/29/2010 17:53 |
Should be this:
Left Join (TheTableName)
LOAD SKU, min(Schedule_Start_Datetime) as MinScheduleStartDatetime
Resident TheTableName
Group by SKU;
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.
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
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]
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;