How can i create duplicate records based the value in another table

人走茶凉 提交于 2021-01-27 12:39:33

问题


I have two tables in my database, Work_Order table which is the source table where work order information's stored i also have Work_Schedule table which contains work schedules which tell peoples in the production floor what to build, when and how much to build.

Work_Order Table looks like

Work order  ItemCode    Size          Qty    Qty_per_HR 
41051        600111    14L-16.1        55          10

I want duplicate the above work order line in work order table above based on the Qty per hour and automatically create a work scheduler as shown below.

where TARGET = Work_Order.Qty/Work_Order.Qty_per_HR

Work_Schedule Table

Id      Start Date/Time        End Date/Time       Work Order       Work Center     TARGET   ACTUAL
1001    2019-07-22 7:00AM      2019-07-22 8:00AM       41051             1         10       
1001    2019-07-22 8:00AM      2019-07-22 9:00AM       41051             1         10    
1001    2019-07-22 9:00AM      2019-07-22 10:00AM      41051             1         10    
1001    22019-07-22 10:15AM    2019-07-22 11:00AM      41051             1         10    
1001    22019-07-22 11:00AM    2019-07-22 12:00PM      41051             1         10  
1001    2019-07-22 1:30PM      2019-07-22 2:30PM       41051             1         5      

My plan is to use AfterInsert trigger as soon as the user a work order create the duplicates.

Schedule windows


回答1:


This seems like a natural for a recursive CTE:

with cte as (
      select convert(datetime, '2019-07-22 7:00AM') as dt, workorder, 1 as workcenter, qtyperh as target,
             itemcode, size, (qty - qtyperh) as qty, qtyperh
      from t
      union all
      select dateadd(hour, 1, dt), workorder, workcenter,
             (case when qty > qtyperh then qtyperh else qty end) as target,
             itemcode, size, (qty - qtyperh), qtyperh
      from cte
      where qty > 0
     )
select cte.*,
       dateadd(second, 60 * 60 * target / qtyperh, dt) as end_dt
from cte
order by workorder, dt;

Here is a db<>fiddle.




回答2:


Is that what are you after?

CREATE TABLE T(
  WorkOrder INT,
  ItemCode INT,
  Size VARCHAR(25),
  Qty INT,
  QtyPerH INT
);

INSERT INTO T VALUES
(41051,        600111,    '14L-16.1',        55,          10),
(41052,        600112,    '14L-16.2',        55,          5);

SELECT T.*
FROM T CROSS APPLY
(
  SELECT 1 N
  FROM master..spt_values
  WHERE [Type] = 'P'
        AND
        [Number] < (T.Qty / T.QtyPerH)
) TT;

Returns:

+-----------+----------+----------+-----+---------+
| WorkOrder | ItemCode |   Size   | Qty | QtyPerH |
+-----------+----------+----------+-----+---------+
|     41051 |   600111 | 14L-16.1 |  55 |      10 |
|     41051 |   600111 | 14L-16.1 |  55 |      10 |
|     41051 |   600111 | 14L-16.1 |  55 |      10 |
|     41051 |   600111 | 14L-16.1 |  55 |      10 |
|     41051 |   600111 | 14L-16.1 |  55 |      10 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
|     41052 |   600112 | 14L-16.2 |  55 |       5 |
+-----------+----------+----------+-----+---------+

Demo



来源:https://stackoverflow.com/questions/57333762/how-can-i-create-duplicate-records-based-the-value-in-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!