Style Integration
This procedure is designed to synchronize style information between the SooperWizer
database and the ERP system, inserting any missing data into
the ParentStyleTemplate and StyleTemplate tables.
1. Drop Temporary Table if Exists
Section titled “1. Drop Temporary Table if Exists”DROP TABLE IF EXISTS #StyleInfoFromERP;- Explanation: This line ensures that any previous instance of the temporary table
#StyleInfoFromERPis dropped before creating a new one. It prevents potential conflicts from leftover data.
2. Fetch Style Data from ERP
Section titled “2. Fetch Style Data from ERP”WITH StyleInfoInTarget AS ( SELECT DISTINCT pst.ParentStyleTemplateDescription AS StyleNo, st.StyleTemplateCode AS DocNo, stb.RevNo, stb.LayoutDep, st.ModificationDate FROM [Essentials].[ParentStyleTemplate] pst JOIN [Essentials].[StyleTemplate] st ON st.ParentStyleTemplateID = pst.ParentStyleTemplateID JOIN [Essentials].[StyleBulletin] stb ON stb.StyleTemplateID = st.StyleTemplateID AND stb.IsActive = 1)SELECT DISTINCT sv.* INTO #StyleInfoFromERPFROM [NCPMS].[dbo].[style_vw] svLEFT JOIN StyleInfoInTarget sit ON sit.StyleNo = sv.StyleNo AND sit.DocNo = sv.DocNo AND sit.RevNo = sv.RevNo AND TRIM(sit.LayoutDep) = TRIM(sv.LayoutDep) AND sit.ModificationDate = sv.StyleModificationDateWHERE sit.StyleNo IS NULL AND sit.DocNo IS NULL AND sit.RevNo IS NULL AND sit.LayoutDep IS NULL AND sit.ModificationDate IS NULL AND sv.OperationDesc NOT LIKE '(%';-
Purpose: This CTE (
StyleInfoInTarget) retrieves distinct style information from theSooperWizerdatabase, including:- StyleNo: Parent style description.
- DocNo: Document number (style code).
- RevNo: Revision number.
- LayoutDep: Layout department.
- ModificationDate: Date when the style was last modified.
-
Joins:
- ParentStyleTemplate to StyleTemplate: Joins
ParentStyleTemplateandStyleTemplatetables byParentStyleTemplateID. - StyleTemplate to StyleBulletin: Filters on active style bulletins with a join on
StyleTemplateIDand theIsActive = 1condition.
- ParentStyleTemplate to StyleTemplate: Joins
-
Explanation: The query pulls style data from the
style_vwview in theNCPMSdatabase. It identifies styles that are not already present in the target database (StyleInfoInTarget), using aLEFT JOINto compare:StyleNoDocNoRevNoLayoutDepModificationDate
-
Condition: The query filters out any rows where a match is found in
StyleInfoInTargetby checking that all key fields areNULL. Additionally, rows with operation descriptions containing parentheses are excluded. -
Action: The distinct results are inserted into a temporary table
#StyleInfoFromERPfor further processing.
3. Insert Non-Existing Style Numbers (ParentStyleTemplate)
Section titled “3. Insert Non-Existing Style Numbers (ParentStyleTemplate)”INSERT INTO [Essentials].[ParentStyleTemplate] (ParentStyleTemplateDescription)SELECT sie.StyleNoFROM #StyleInfoFromERP sieLEFT JOIN [Essentials].[ParentStyleTemplate] pst ON pst.ParentStyleTemplateDescription = sie.StyleNoWHERE pst.ParentStyleTemplateDescription IS NULLGROUP BY sie.StyleNo;-
Purpose: This query inserts new styles into the
ParentStyleTemplatetable that do not already exist. -
Explanation:
- It uses a
LEFT JOINto check if theStyleNofrom#StyleInfoFromERPalready exists in theParentStyleTemplatetable. - If the style does not exist (
NULLcondition), the style is inserted.
- It uses a
4. Insert Non-Existing Document Numbers (StyleTemplate)
Section titled “4. Insert Non-Existing Document Numbers (StyleTemplate)”INSERT INTO [Essentials].[StyleTemplate] (StyleTemplateCode, ParentStyleTemplateID, ModificationDate)SELECT sie.DocNo, pst.ParentStyleTemplateID, MAX(sie.StyleModificationDate) AS ModificationDateFROM #StyleInfoFromERP sieJOIN [Essentials].[ParentStyleTemplate] pst ON pst.ParentStyleTemplateDescription = sie.StyleNoLEFT JOIN [Essentials].[StyleTemplate] st ON st.StyleTemplateCode = sie.DocNo AND st.ParentStyleTemplateID = pst.ParentStyleTemplateIDWHERE st.StyleTemplateCode IS NULLGROUP BY sie.DocNo, pst.ParentStyleTemplateID;-
Purpose: This query inserts new document numbers (
DocNo) into theStyleTemplatetable that do not already exist.. -
Explanation:
- A
JOINis performed between#StyleInfoFromERPandParentStyleTemplateto get the correspondingParentStyleTemplateID. - A
LEFT JOINchecks if theDocNoalready exists in theStyleTemplatetable. - If no matching document is found (
NULL), theDocNo,ParentStyleTemplateID, andModificationDateare inserted.
- A
5. Drop Temporary Table if Exists
Section titled “5. Drop Temporary Table if Exists”DROP TABLE IF EXISTS #OperationDataSetFromERP;- Explanation: This line ensures that any previous instance of the temporary table
#OperationDataSetFromERPis dropped before creating a new one, preventing potential conflicts from leftover data.
6. DataSet for Operations
Section titled “6. DataSet for Operations”SELECT dt.*, CASE WHEN Heading = 'FIRST OPERATION' THEN 1 ELSE 0 END AS IsFirst, CASE WHEN QCStation = 'END LINE QC 100%' THEN 1 ELSE 0 END AS IsLast, CASE WHEN QCStation = 'STATION QC 100%' THEN 1 ELSE 0 END AS IsQcOperation, ROW_NUMBER() OVER(PARTITION BY DocNo,RevNo,DepartmentID ORDER BY OperationNo) AS OpSeq,'Bundle' AS ScanType INTO #OperationDataSetFromERPFROM ( -- SubQuery Start SELECT sie.*, st.StyleTemplateID, op.OperationID, d.DepartmentID, LAG(sie.OperationDesc) OVER (PARTITION BY sie.DocNo, sie.RevNo, sie.LayoutDep ORDER BY sie.OperationNo) AS PrevOperationDesc FROM #StyleInfoFromERP sie JOIN [Essentials].[StyleTemplate] st ON st.StyleTemplateCode = sie.DocNo JOIN [Essentials].[Operation] op ON op.OperationName = sie.OperationDesc LEFT JOIN [Essentials].[Department] d ON d.DepartmentName = sie.LayoutDep -- SubQuery End) AS dtWHERE PrevOperationDesc IS NULL OR PrevOperationDesc <> OperationDesc;-
Purpose: This query constructs a temporary dataset (
#OperationDataSetFromERP) for operations, adding columns for first operation flags (IsFirst), last operation flags (IsLast), and quality control flags (IsQcOperation). -
Explanation:
-
The outer query creates flags based on specific criteria:
-
IsFirst: Marks rows as the first operation when theHeadingis “FIRST OPERATION”. -
IsLast: Marks rows as the last operation when theQCStationis “END LINE QC 100%”. -
IsQcOperation: Marks quality control operations when theQCStationis “STATION QC 100%”.
-
-
The
ROW_NUMBER()function assigns a sequence (OpSeq) to operations within eachDocNo,RevNo, andDepartmentID, ordering byOperationNo. -
A constant
'Bundle'is assigned to theScanTypecolumn.
-
-
Subquery Purpose: The subquery selects relevant operation data from the
#StyleInfoFromERPtable, joining theStyleTemplate,Operation, andDepartmenttables to retrieve necessary identifiers and department information.-
A
LAGfunction is used to retrieve the previous operation description (PrevOperationDesc) for each row, partitioned byDocNo,RevNo, andLayoutDep, and ordered byOperationNo. -
The purpose of using
LAGis to eliminate duplicate operations; if the same operation appears in the style with two differentOperationNovalues, the function allows us to select the operation with the lowerOperationNo.
-
-
Condition: The final
WHEREclause filters out rows where thePrevOperationDescis equal to the currentOperationDescto avoid duplications.
7. Non-Existing Operations to Insert into StyleBulletin
Section titled “7. Non-Existing Operations to Insert into StyleBulletin”INSERT INTO [Essentials].[StyleBulletin] ( StyleTemplateID, DocNo, OperationID, OperationDesc, OperationSequence, SMV, RevNo, CompanyID, BrID, WorkOrder, MachineID, MachineAbv, MachineName, QCStation, NoQCS, Heading, Target, Line_no, LayoutType, LayoutPos, LayoutPos2, HeaderSr, OprSr, RGBColor, R, G, B, Mod_Date, Ent_Date, User_Name, Comp_Name, LayoutDep, DepartmentID, IsFirst, IsLast, IsQcOperation, OpSeq, ScanType)-- DataSetSELECT ds.StyleTemplateID, ds.DocNo, ds.OperationID, ds.OperationDesc, ds.OperationNo, ds.SMV, ds.RevNo, ds.CompanyID, ds.BrID, ds.WorkOrder, ds.MachineID, ds.MachineAbv, ds.MachineName, ds.QCStation, ds.NoQCS, ds.Heading, ds.Target, ds.Line_no, ds.LayoutType, ds.LayoutPos, ds.LayoutPos2, ds.HeaderSr, ds.OprSr, ds.RGBColor, ds.R, ds.G, ds.B, ds.Mod_Date, ds.Ent_Date, ds.User_Name, ds.Comp_Name, ds.LayoutDep, ds.DepartmentID, ds.IsFirst, ds.IsLast, ds.IsQcOperation, ds.OpSeq, ds.ScanTypeFROM #OperationDataSetFromERP dsLEFT JOIN ( SELECT DISTINCT StyleTemplateID, DocNo, RevNo, OperationID, OperationDesc, DepartmentID, LayoutDep, Mod_Date FROM [Essentials].[StyleBulletin] WHERE IsActive = 1 AND OperationDesc NOT IN ('Out From Cutting', 'QC Pass') ) stb ON stb.StyleTemplateID = ds.StyleTemplateID AND stb.RevNo = ds.RevNo AND stb.DepartmentID = ds.DepartmentID AND stb.OperationID = ds.OperationIDWHERE stb.StyleTemplateID IS NULLORDER BY ds.StyleTemplateID;-
Purpose: This query inserts non-existing operations into the
StyleBulletintable from the#OperationDataSetFromERPtemporary dataset. -
Explanation:
-
The
INSERT INTOstatement targets theStyleBulletintable, specifying all relevant columns. -
The
SELECTstatement retrieves data from the#OperationDataSetFromERPtemporary table (ds), which contains operations data to be inserted. -
A
LEFT JOINis performed with a subquery (stb) that selects distinct records from theStyleBulletintable whereIsActiveis1and theOperationDescis not in the specified exclusion list ('Out From Cutting', 'QC Pass'). -
The
WHEREclause filters for records where no matchingStyleTemplateIDis found in theStyleBulletintable, indicating that the operation does not already exist. -
The results are ordered by
StyleTemplateIDfor better organization.
-
8. Corrected OperationSequence in StyleBulletin
Section titled “8. Corrected OperationSequence in StyleBulletin”WITH ModifiedStyles AS (SELECT DISTINCT StyleNo, DocNo, RevNo, LayoutDep, StyleModificationDateFROM #StyleInfoFromERP),CorrectOpSeq AS (SELECT stb.DocNo,stb.LayoutDep,stb.RevNo,stb.OperationDesc,stb.OperationSequence,stb.OpSeq, ROW_NUMBER() OVER(PARTITION BY stb.DocNo, stb.RevNo, stb.DepartmentID ORDER BY stb.OperationSequence) AS NewOpSeq, stb.CreatedAt,stb.UpdatedAtFROM [Essentials].[StyleBulletin] stbJOIN ModifiedStyles ms ON ms.DocNo=stb.DocNo AND ms.RevNo=stb.RevNo AND TRIM(ms.LayoutDep)=TRIM(stb.LayoutDep)WHERE IsActive=1 AND OpSeq NOT LIKE '%-%')UPDATE CorrectOpSeqSET OpSeq = NewOpSeq, UpdatedAt=GETDATE()WHERE OpSeq!=NewOpSeq;-
Purpose: This query corrects the
OperationSequencein theStyleBulletintable by assigning new operation sequence numbers (OpSeq) based on their order of appearance. -
Explanation:
-
Common Table Expressions (CTEs):
-
ModifiedStyles: This CTE retrieves distinct style information, including
StyleNo,DocNo,RevNo,LayoutDep, andStyleModificationDate, from the#StyleInfoFromERPtemporary dataset. -
CorrectOpSeq: This CTE generates a new operation sequence (
NewOpSeq) for records in theStyleBulletintable that match the modified styles. It uses theROW_NUMBER()function, partitioned byDocNo,RevNo, andDepartmentID, and orders the results byOperationSequence.
-
-
The
UPDATEstatement updates theOpSeqin theCorrectOpSeqCTE with the newly calculatedNewOpSeqwhere the existingOpSeqdoes not matchNewOpSeq. -
The
UpdatedAtcolumn is also set to the current date and time to reflect when the update occurred.
-
9. Deactivate Old StyleBulletin
Section titled “9. Deactivate Old StyleBulletin”UPDATE stbSET stb.IsActive = 0FROM [Essentials].[StyleBulletin] stbWHERE NOT EXISTS ( SELECT 1 FROM [NCPMS].[dbo].[MchLayoutDetail_vw_union] mld WHERE mld.DocNo = stb.DocNo AND mld.RevNo = stb.RevNo AND mld.LayoutDep = stb.LayoutDep);-
Purpose: This query deactivates old entries in the
StyleBulletintable by setting theIsActiveflag to0for styles that no longer exist in theMchLayoutDetailview InERP. -
Explanation:
-
The
UPDATEstatement targets theStyleBulletintable (stb). -
The
WHERE NOT EXISTSclause checks for the absence of matching records in theMchLayoutDetailview (mld).- It compares
DocNo,RevNo, andLayoutDepbetween the two tables.
- It compares
-
If no matching record is found in
MchLayoutDetail, theIsActivecolumn for that style in theStyleBulletintable is set to0, effectively deactivating it.
-
10. Insert Manual Operations without Duplicates
Section titled “10. Insert Manual Operations without Duplicates”INSERT INTO [Essentials].[StyleBulletin](StyleTemplateID, DocNo, RevNo, LayoutDep, DepartmentID, OperationSequence, OperationDesc, OpSeq, ScanType, OperationID, IsLast)SELECT sb.StyleTemplateID,sb.DocNo,sb.RevNo,op.LayoutDep,op.DepartmentID,op.OperationSequence,op.OperationDesc,op.OpSeq,op.ScanType,o.OperationID,op.IsLastFROM( VALUES ('Out From Cutting', 1, 'CUTTING', -1, -1, 'Bundle', 1),('QC Pass', 5, 'SEWING', -2, -2, 'Bundle', 0)) AS op(OperationDesc, DepartmentID, LayoutDep, OperationSequence, OpSeq, ScanType, IsLast)JOIN [Essentials].[Operation] o ON o.OperationName = op.OperationDescJOIN( SELECT DISTINCT StyleTemplateID, DocNo, RevNo, LayoutDep FROM [Essentials].[StyleBulletin] WHERE IsActive = 1 AND LayoutDep IN ('CUTTING', 'SEWING')) AS sbON sb.LayoutDep = op.LayoutDepLEFT JOIN [Essentials].[StyleBulletin] stbON stb.DocNo = sb.DocNo AND stb.RevNo = sb.RevNo AND stb.LayoutDep = sb.LayoutDep AND stb.OperationDesc = op.OperationDesc AND stb.IsActive = 1WHERE stb.DocNo IS NULL;-
Purpose: This query inserts manual operations into the
StyleBulletintable while ensuring that no duplicates are created. -
Explanation:
-
The
INSERT INTOstatement targets theStyleBulletintable, specifying the columns to be filled. -
The
SELECTstatement retrieves data from the following sources:-
A
VALUESclause defines manual operation entries (like ‘Out From Cutting’ and ‘QC Pass’) along with their associated attributes. -
A
JOINwith theOperationtable retrieves theOperationIDbased on theOperationDesc. -
A subquery (
sb) fetches distinct style entries from theStyleBulletintable where styles are active and belong to specific layout departments (‘CUTTING’ and ‘SEWING’).
-
-
A
LEFT JOINis performed with theStyleBulletintable (stb) to check for existing records matching theDocNo,RevNo,LayoutDep, andOperationDesc. -
The
WHEREclause filters out any entries that already exist in theStyleBulletintable by checking ifstb.DocNoisNULL, ensuring that only new, non-duplicate records are inserted.
-
10. Mark Unique IsFirst Operation For Sewing
Section titled “10. Mark Unique IsFirst Operation For Sewing”WITH StyleForSewingFirst AS (SELECT DISTINCT StyleNo, DocNo, RevNo, LayoutDep, StyleModificationDateFROM #StyleInfoFromERPWHERE LayoutDep='SEWING'),FirstOpMinSeqSewing AS (SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,stb.Heading,stb.IsFirst, MIN(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) AS MinOpSeq, CASE WHEN stb.OpSeq=MIN(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) THEN 1 ELSE 0 END AS ActualFirstOpFROM [Essentials].[StyleBulletin] stbJOIN StyleForSewingFirst ssfON ssf.DocNo=stb.DocNo AND ssf.RevNo=stb.RevNoAND TRIM(ssf.LayoutDep)=TRIM(stb.LayoutDep)WHERE stb.Heading = 'FIRST OPERATION' AND stb.IsActive = 1)UPDATE stbSET stb.IsFirst = CASE WHEN stb.OpSeq = c.MinOpSeq THEN 1 ELSE 0 END,stb.UpdatedAt=GETDATE()/**SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,c.MinOpSeq,stb.Heading,stb.IsFirst,c.ActualFirstOp**/FROM [Essentials].[StyleBulletin] stbJOIN FirstOpMinSeqSewing c ON stb.StyleBulletinID = c.StyleBulletinID;-
Purpose: This query identifies and marks the first operation in the
StyleBulletintable for sewing operations while eliminating duplicate entries for theIsFirstoperation flag. -
Explanation:
-
Common Table Expressions (CTEs):
-
StyleForSewingFirst: This CTE retrieves distinct style information (including
StyleNo,DocNo,RevNo,LayoutDep, andStyleModificationDate) for styles where theLayoutDepis ‘SEWING’. -
FirstOpMinSeqSewing: This CTE calculates the minimum operation sequence (
MinOpSeq) for sewing operations. It also determines whether each operation is the actual first operation (ActualFirstOp) by comparing the current operation sequence (OpSeq) with the minimum sequence.-
The
MINfunction is used with theOVERclause to get the minimum operation sequence partitioned byDocNo,RevNo, andLayoutDep, ordered byOperationSequence. -
A case statement marks
ActualFirstOpas 1 if the currentOpSeqequals the minimum operation sequence.
-
-
-
UPDATE Statement:
-
The
UPDATEstatement modifies theIsFirstflag in theStyleBulletintable for sewing operations. -
It sets
IsFirstto 1 for the operation with the minimum sequence (MinOpSeq) and updates theUpdatedAtcolumn to the current date and time.
-
-
-
Notes: The commented-out
SELECTstatement can be used to view the results before performing the update, helping to validate which records are being modified.
11. Mark Unique IsLast Operation For Sewing
Section titled “11. Mark Unique IsLast Operation For Sewing”WITH StyleForSewingLast AS (SELECT DISTINCT StyleNo, DocNo, RevNo, LayoutDep, StyleModificationDateFROM #StyleInfoFromERPWHERE LayoutDep='SEWING'),LastOpMaxSeqSewing AS (SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,stb.QCStation,stb.IsLast, MAX(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) AS MaxOpSeq, CASE WHEN stb.OpSeq=MAX(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) THEN 1 ELSE 0 END AS ActualLastOpFROM [Essentials].[StyleBulletin] stbJOIN StyleForSewingLast sslON ssl.DocNo=stb.DocNo AND ssl.RevNo=stb.RevNoAND TRIM(ssl.LayoutDep)=TRIM(stb.LayoutDep)WHERE stb.QCStation = 'END LINE QC 100%' AND stb.IsActive=1)UPDATE stbSET stb.IsLast = CASE WHEN stb.OpSeq = c.MaxOpSeq THEN 1 ELSE 0 END/**SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,c.MaxOpSeq,stb.QCStation,stb.IsLast,c.ActualLastOp**/FROM [Essentials].[StyleBulletin] stbJOIN LastOpMaxSeqSewing c ON stb.StyleBulletinID = c.StyleBulletinID;-
Purpose: This query identifies and marks the last operation in the
StyleBulletintable for sewing operations while eliminating duplicate entries for theIsLastoperation flag. -
Explanation:
-
Common Table Expressions (CTEs):
-
StyleForSewingLast: This CTE retrieves distinct style information (including
StyleNo,DocNo,RevNo,LayoutDep, andStyleModificationDate) for styles where theLayoutDepis ‘SEWING’. -
LastOpMaxSeqSewing: This CTE calculates the maximum operation sequence (
MaxOpSeq) for sewing operations. It also determines whether each operation is the actual last operation (ActualLastOp) by comparing the current operation sequence (OpSeq) with the maximum sequence.-
The
MAXfunction is used with theOVERclause to get the maximum operation sequence partitioned byDocNo,RevNo, andLayoutDep, ordered byOperationSequence. -
A case statement marks
ActualLastOpas 1 if the currentOpSeqequals the maximum operation sequence.
-
-
-
UPDATE Statement:
-
The
UPDATEstatement modifies theIsLastflag in theStyleBulletintable for sewing operations. -
It sets
IsLastto 1 for the operation with the maximum sequence (MaxOpSeq).
-
-
-
Notes: The commented-out
SELECTstatement can be used to view the results before performing the update, helping to validate which records are being modified.
12. Mark Unique IsFirst Operation For All Departments Except Sewing
Section titled “12. Mark Unique IsFirst Operation For All Departments Except Sewing”WITH StyleForFirstExcludeSewing AS (SELECT DISTINCT StyleNo, DocNo, RevNo, LayoutDep, StyleModificationDateFROM #StyleInfoFromERPWHERE LayoutDep!='SEWING'),FirstOpMinSeqExcludeSewing AS (SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,stb.Heading,stb.IsFirst, MIN(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) AS MinOpSeq, CASE WHEN stb.OpSeq=MIN(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) THEN 1 ELSE 0 END AS ActualFirstOpFROM [Essentials].[StyleBulletin] stbJOIN StyleForFirstExcludeSewing ssf ON ssf.DocNo=stb.DocNo AND ssf.RevNo=stb.RevNo AND TRIM(ssf.LayoutDep)=TRIM(stb.LayoutDep)WHERE stb.IsActive=1 AND stb.LayoutDep != 'SEWING' AND stb.OpSeq!=-1)UPDATE stbSET stb.IsFirst = CASE WHEN stb.OpSeq = c.MinOpSeq THEN 1 ELSE 0 END/**SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,c.MinOpSeq,stb.Heading,stb.IsFirst,c.ActualFirstOp**/FROM [Essentials].[StyleBulletin] stbJOIN FirstOpMinSeqExcludeSewing c ON stb.StyleBulletinID = c.StyleBulletinID;-
Purpose: This query identifies the first operation in the
StyleBulletintable for all departments except sewing, ensuring that only relevant operations are marked as the first operation. -
Explanation:
-
Common Table Expressions (CTEs):
-
StyleForFirstExcludeSewing: This CTE selects distinct style information (including
StyleNo,DocNo,RevNo,LayoutDep, andStyleModificationDate) from the#StyleInfoFromERPtemporary dataset, filtering out any entries withLayoutDepequal to ‘SEWING’. -
FirstOpMinSeqExcludeSewing: This CTE retrieves information from the
StyleBulletintable and calculates the minimum operation sequence (MinOpSeq) for each style, while also determining if the current operation is the first operation (ActualFirstOp).-
It uses the
MINfunction with theOVERclause to calculate the minimumOpSeqfor each group of operations partitioned byDocNo,RevNo, andLayoutDep, ordered byOperationSequence. -
The
CASEstatement assignsActualFirstOpas 1 if the currentOpSeqequals the minimum operation sequence.
-
-
-
UPDATE Statement:
-
The
UPDATEstatement modifies theIsFirstflag in theStyleBulletintable. -
It sets
IsFirstto 1 if the current operation’sOpSeqmatches the calculatedMinOpSeq, effectively marking the first operation for each style. -
The
FROMclause joins the originalStyleBulletintable with theFirstOpMinSeqExcludeSewingCTE based onStyleBulletinID.
-
-
-
Notes:
- The commented-out
SELECTstatement provides a way to preview the results, showing details of the operations being updated, includingStyleBulletinID,MinOpSeq, and the current state ofIsFirst. - This query aims to ensure that only one operation per style is marked as the first operation, avoiding duplication and confusion.
- The commented-out
13. Mark Unique IsFirst Operation For All Departments Except Sewing
Section titled “13. Mark Unique IsFirst Operation For All Departments Except Sewing”WITH StyleForLastExcludeSewCut AS (SELECT DISTINCT StyleNo, DocNo, RevNo, LayoutDep, StyleModificationDateFROM #StyleInfoFromERPWHERE LayoutDep NOT IN ('SEWING','CUTTING')),LastOpMaxSeqExcludeSewCut AS (SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,stb.QCStation,stb.IsLast, MAX(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) AS MaxOpSeq, CASE WHEN stb.OpSeq=MAX(stb.OpSeq) OVER (PARTITION BY stb.DocNo,stb.RevNo,stb.LayoutDep ORDER BY stb.OperationSequence ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) THEN 1 ELSE 0 END AS ActualLastOpFROM [Essentials].[StyleBulletin] stbJOIN StyleForLastExcludeSewCut ssl ON ssl.DocNo=stb.DocNo AND ssl.RevNo=stb.RevNo AND TRIM(ssl.LayoutDep)=TRIM(stb.LayoutDep)WHERE stb.IsActive=1 AND stb.LayoutDep NOT IN ('SEWING','CUTTING'))UPDATE stbSET stb.IsLast = CASE WHEN stb.OpSeq = c.MaxOpSeq THEN 1 ELSE 0 END/**SELECT stb.StyleBulletinID,stb.StyleTemplateID,stb.DocNo,stb.RevNo,stb.LayoutDep,stb.OperationDesc,stb.OperationSequence,stb.OpSeq,c.MaxOpSeq,stb.Heading,stb.IsLast,c.ActualLastOp**/FROM [Essentials].[StyleBulletin] stbJOIN LastOpMaxSeqExcludeSewCut c ON stb.StyleBulletinID = c.StyleBulletinID;-
Purpose: This query identifies the last operation in the
StyleBulletintable for all departments except sewing and cutting, ensuring that only relevant operations are marked as the last operation. -
Explanation:
-
Common Table Expressions (CTEs):
-
StyleForLastExcludeSewCut: This CTE retrieves distinct style information (including
StyleNo,DocNo,RevNo,LayoutDep, andStyleModificationDate) from the#StyleInfoFromERPtemporary dataset, filtering out any entries withLayoutDepequal to ‘SEWING’ or ‘CUTTING’. -
LastOpMaxSeqExcludeSewCut: This CTE retrieves information from the
StyleBulletintable and calculates the maximum operation sequence (MaxOpSeq) for each style while determining if the current operation is the last operation (ActualLastOp).-
It uses the
MAXfunction with theOVERclause to calculate the maximumOpSeqfor each group of operations partitioned byDocNo,RevNo, andLayoutDep, ordered byOperationSequence. -
The
CASEstatement assignsActualLastOpas 1 if the currentOpSeqequals the maximum operation sequence.
-
-
-
UPDATE Statement:
-
The
UPDATEstatement modifies theIsLastflag in theStyleBulletintable. -
It sets
IsLastto 1 if the current operation’sOpSeqmatches the calculatedMaxOpSeq, effectively marking the last operation for each style. -
The
FROMclause joins the originalStyleBulletintable with theLastOpMaxSeqExcludeSewCutCTE based onStyleBulletinID.
-
-
-
Notes:
-
The commented-out
SELECTstatement provides a way to preview the results, showing details of the operations being updated, includingStyleBulletinID,MaxOpSeq, and the current state ofIsLast. -
This query aims to ensure that only one operation per style is marked as the last operation, avoiding duplication and confusion.
-
14. Mark Unique IsFirst Operation For All Departments Where No Operation Marked
Section titled “14. Mark Unique IsFirst Operation For All Departments Where No Operation Marked”UPDATE stbSET stb.IsFirst = CASE WHEN stb.OpSeq = f.MinOpSeq THEN 1 ELSE 0 ENDFROM [Essentials].[StyleBulletin] stbJOIN (SELECT DocNo, LayoutDep, RevNo, MIN(CASE WHEN OpSeq NOT LIKE '%-%' THEN OpSeq ELSE OpSeq END) AS MinOpSeqFROM [Essentials].[StyleBulletin]WHERE IsActive = 1GROUP BY DocNo, LayoutDep, RevNoHAVING SUM(CASE WHEN IsFirst = 1 THEN 1 ELSE 0 END) = 0 -- No IsFirst = 1) AS fON stb.DocNo = f.DocNoAND stb.LayoutDep = f.LayoutDepAND stb.RevNo = f.RevNoAND stb.IsActive=1;-
Purpose: This query updates the
IsFirstflag in theStyleBulletintable to indicate the first operation for each unique combination ofDocNo,LayoutDep, andRevNowhere no operation has already been marked as the first. -
Explanation:
-
UPDATE Statement: The
UPDATEstatement modifies theIsFirstcolumn in theStyleBulletintable (stb). -
JOIN Clause:
-
A derived table (aliased as
f) is created to identify the first operation for each style by selecting the minimumOpSeq(operation sequence) for active styles. -
The
MINfunction is used to find the smallestOpSeqvalue that does not contain a hyphen (%-%). TheCASEstatement ensures that the minimum is calculated correctly.
-
-
Conditions:
-
The
WHEREclause in the subquery filters for active styles (IsActive = 1). -
The
GROUP BYclause groups the results byDocNo,LayoutDep, andRevNo, allowing for the identification of the first operation within each group. -
The
HAVINGclause ensures that the query only includes groups where no operation is currently marked as the first (IsFirst = 1). This prevents the query from incorrectly marking an operation as the first if one already exists.
-
-
ON Clause: The
JOINcondition links the mainStyleBulletintable with the derived tablefbased onDocNo,LayoutDep, andRevNo, ensuring that only those records that need updating are selected. -
SET Clause: The
SETclause updates theIsFirstflag. It assigns1toIsFirstif theOpSeqmatches theMinOpSeqcalculated in the subquery, indicating that this operation is the first for that style. Otherwise, it setsIsFirstto0.
-
-
Notes:
- The query ensures that only one operation per style is marked as the first operation, thereby eliminating duplicates in the
IsFirstdesignation.
- The query ensures that only one operation per style is marked as the first operation, thereby eliminating duplicates in the
15. Mark Unique IsLast Operation For All Departments Where No Operation Marked
Section titled “15. Mark Unique IsLast Operation For All Departments Where No Operation Marked”UPDATE sbSET IsLast = CASE WHEN sb.OpSeq = l.MaxOpSeq THEN 1 ELSE 0 ENDFROM [Essentials].[StyleBulletin] sbJOIN (SELECT DocNo, LayoutDep, RevNo, MAX(OpSeq) AS MaxOpSeqFROM [Essentials].[StyleBulletin]WHERE IsActive = 1GROUP BY DocNo, LayoutDep, RevNoHAVING SUM(CASE WHEN IsLast = 1 THEN 1 ELSE 0 END) = 0 -- No IsLast = 1;) AS lON sb.DocNo = l.DocNoAND sb.LayoutDep = l.LayoutDepAND sb.RevNo = l.RevNoAND IsActive=1;-
Purpose: This query updates the
IsLastflag in theStyleBulletintable to indicate the last operation for each unique combination ofDocNo,LayoutDep, andRevNowhere no operation has already been marked as the last. -
Explanation:
-
UPDATE Statement: The
UPDATEstatement modifies theIsLastcolumn in theStyleBulletintable (sb). -
JOIN Clause:
-
A derived table (aliased as
l) is created to identify the last operation for each style by selecting the maximumOpSeq(operation sequence) for active styles. -
The
MAXfunction is used to find the largestOpSeqvalue, which represents the last operation in the sequence.
-
-
Conditions:
-
The
WHEREclause in the subquery filters for active styles (IsActive = 1). -
The
GROUP BYclause groups the results byDocNo,LayoutDep, andRevNo, allowing for the identification of the last operation within each group. -
The
HAVINGclause ensures that the query only includes groups where no operation is currently marked as the last (IsLast = 1). This prevents the query from incorrectly marking an operation as the last if one already exists.
-
-
ON Clause: The
JOINcondition links the mainStyleBulletintable with the derived tablelbased onDocNo,LayoutDep, andRevNo, ensuring that only those records that need updating are selected. -
SET Clause: The
SETclause updates theIsLastflag. It assigns1toIsLastif theOpSeqmatches theMaxOpSeqcalculated in the subquery, indicating that this operation is the last for that style. Otherwise, it setsIsLastto0.
-
-
Notes:
- The query ensures that only one operation per style is marked as the last operation, thereby eliminating duplicates in the
IsLastdesignation.
- The query ensures that only one operation per style is marked as the last operation, thereby eliminating duplicates in the
16. Update ModificationDate of Existing DocNos
Section titled “16. Update ModificationDate of Existing DocNos”UPDATE stSET st.ModificationDate = source.ModificationDateFROM [Essentials].[StyleTemplate] stJOIN [Essentials].[ParentStyleTemplate] pst ON st.ParentStyleTemplateID = pst.ParentStyleTemplateIDJOIN ( SELECT sie.StyleNo, sie.DocNo, MAX(sie.StyleModificationDate) AS ModificationDate FROM #StyleInfoFromERP sie GROUP BY sie.StyleNo, sie.DocNo) AS sourceON st.StyleTemplateCode = source.DocNoAND pst.ParentStyleTemplateDescription = source.StyleNo;-
Purpose: This query updates the
ModificationDatecolumn in theStyleTemplatetable for entries that match existingDocNovalues based on the most recent modification date from a temporary dataset (#StyleInfoFromERP). -
Explanation:
-
UPDATE Statement: The
UPDATEstatement targets theStyleTemplatetable (aliased asst) to modify theModificationDatecolumn. -
JOIN Clauses:
-
The first
JOINlinks theStyleTemplatetable to theParentStyleTemplatetable (pst) on theParentStyleTemplateID. This ensures that you have access to the parent style’s description. -
The second
JOINconnects theStyleTemplatetable to a derived table (source) created from the#StyleInfoFromERPtemporary table. This derived table aggregates the most recent modification dates for each combination ofStyleNoandDocNo.
-
-
Derived Table (source):
-
The derived table (
source) selects distinctStyleNoandDocNovalues from#StyleInfoFromERP, along with the maximumStyleModificationDatefor each combination. This effectively captures the latest modification date for each style-document pair. -
The
GROUP BYclause groups the results byStyleNoandDocNoto ensure that the aggregation correctly reflects the most recent date for each combination.
-
-
ON Clause: The
ONconditions ensure that:-
The
DocNofrom theStyleTemplatetable matches theDocNofrom thesourcederived table. -
The
ParentStyleTemplateDescriptionfrom theParentStyleTemplatetable matches theStyleNofrom thesourcederived table.
-
-
-
Effect: After executing this query, the
ModificationDatein theStyleTemplatetable will reflect the most recent modification date from the temporary dataset for the corresponding styles and documents.