1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| CREATE TABLE SHOPORDER_SCHEDULING ( ID INT PRIMARY KEY, EQUIPMENT_CODE VARCHAR(50), SHOPORDER VARCHAR(50), PLAN_QTY INT ); CREATE TABLE SN_STEP ( ID INT PRIMARY KEY, RESOURCE_NAME VARCHAR(50), SHOPORDER VARCHAR(50), ADOPT INT ); INSERT INTO SHOPORDER_SCHEDULING (ID, EQUIPMENT_CODE, SHOPORDER, PLAN_QTY) VALUES (1, 'EQ001', 'SO1001', 100), (2, 'EQ001', 'SO1002', 150), (3, 'EQ002', 'SO1003', 200), (4, 'EQ002', 'SO1003', 250), (5, 'EQ003', 'SO1001', 300);
INSERT INTO SN_STEP (ID, RESOURCE_NAME, SHOPORDER, ADOPT) VALUES (1, 'EQ001', 'SO1001', 0), (2, 'EQ001', 'SO1001', 1), (3, 'EQ001', 'SO1002', 0), (4, 'EQ002', 'SO1003', 1);
SELECT * FROM SHOPORDER_SCHEDULING SC LEFT JOIN SN_STEP SS ON SC.EQUIPMENT_CODE = SS.RESOURCE_NAME;
SELECT * FROM SHOPORDER_SCHEDULING SC LEFT JOIN SN_STEP SS ON SC.EQUIPMENT_CODE = SS.RESOURCE_NAME AND SC.SHOPORDER = SS.SHOPORDER;
SELECT * FROM SHOPORDER_SCHEDULING SC LEFT JOIN SN_STEP SS ON SC.EQUIPMENT_CODE = SS.RESOURCE_NAME AND SC.SHOPORDER = SS.SHOPORDER WHERE SC.EQUIPMENT_CODE IS NOT NULL AND SS.ADOPT IN (0, 1);
SELECT SC.ID, COUNT(DISTINCT SS.ID) AS OUT_QTY, CONCAT(FORMAT(COUNT(DISTINCT SS.ID) / SC.PLAN_QTY * 100, 2), '%') AS ACHIEVED_RATE FROM SHOPORDER_SCHEDULING SC LEFT JOIN SN_STEP SS ON SC.EQUIPMENT_CODE = SS.RESOURCE_NAME AND SC.SHOPORDER = SS.SHOPORDER WHERE SC.EQUIPMENT_CODE IS NOT NULL AND SS.ADOPT IN (0, 1) GROUP BY SC.ID;
SELECT SC.ID, COUNT(DISTINCT SS.ID) AS OUT_QTY, COUNT(DISTINCT CASE WHEN SS.ADOPT = 0 THEN SS.ID END) AS FALSE_QTY, COUNT(DISTINCT CASE WHEN SS.ADOPT = 1 THEN SS.ID END) AS PASS_QTY, CONCAT(FORMAT(COUNT(DISTINCT CASE WHEN SS.ADOPT = 1 THEN SS.ID END) / SC.PLAN_QTY * 100, 2), '%') AS ACHIEVED_RATE FROM SHOPORDER_SCHEDULING SC LEFT JOIN SN_STEP SS ON SC.EQUIPMENT_CODE = SS.RESOURCE_NAME AND SC.SHOPORDER = SS.SHOPORDER WHERE SC.EQUIPMENT_CODE IS NOT NULL AND SS.ADOPT IN (0, 1) GROUP BY SC.ID, SC.PLAN_QTY;
|