Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple Insert query for existing row in sql

I have a table which have 10 columns. I have to insert data for that table. I have some insert statements. first insert statement insert data for first 3 rows from one source. Now I want to insert data for next columns in same row using another insert statements from different source. These insert queries runs daily for table Order_Warehouse_Status, so we will have 1 row for daily transaction.

Ex. Table Order_Warehouse_Status have 10 columns like

Printed_PPS_Shipment,
Printed_Shipment_Lines,
Printed_Unit, 
Picking_Scheduled_Orders, 
Picking_Scheduled_Lines, 
Picking_Scheduled_Units, 
Pick_Complete_Orders, 
Pick_Complete_Lines, 
Pick_Complete_Units

Below 1st query insert data in first 3 columns. 2nd query should insert data for next column in same row. How to achieve this?

--1st Query

insert into Order_Warehouse_Status
(date , Printed_PPS_Shipment,
 Printed_Shipment_Lines,
 Printed_Unit) 
SELECT Getdate(), count(v_c_ship_ship_id) as Printed_PPS_Shipment, 
       count(ship_l_id) as Printed_Shipment_Lines, 
       count(allocated_qty) as Printed_Unit \
FROM [STG_WMS_Status_PPS_Line_QTY] 
where CONVERT(DATE,Inserted_date )=CONVERT(DATE,Getdate()) 
      and shipment_status=2

--2nd query

insert into Order_Warehouse_Status 
(Date, Picking_Scheduled_Orders, 
 Picking_Scheduled_Lines, 
 Picking_Scheduled_Units) 
SELECT Getdate(), count(v_c_ship_ship_id) as Picking_Scheduled_Orders, 
       count(ship_l_id) as Picking_Scheduled_Lines, 
       count(allocated_qty) as Picking_Scheduled_Units 
FROM STG_Closed_Received 
where CONVERT(DATE,Inserted_date )=CONVERT(DATE,Getdate()) 
      and shipment_status=7

Thanks in advance

like image 503
ramu Avatar asked Feb 15 '26 09:02

ramu


1 Answers

Hi first query will be same as insert and the second query you can do an Update statement with a where condition by checking whether the date is today's date...

  Update Order_Warehouse_Status 
  set Picking_scheduled_orders = i.Picking_Scheduled_Orders,
      Picking_Scheduled_Lines =  i.Picking_Scheduled_Lines,
       Picking_Scheduled_Units = i.Picking_Scheduled_Units 
       From(SELECT  count(v_c_ship_ship_id) as Picking_Scheduled_Orders, 
       count(ship_l_id) as Picking_Scheduled_Lines, 
       count(allocated_qty) as Picking_Scheduled_Units 
FROM STG_Closed_Received 
where CONVERT(DATE,Inserted_date )=CONVERT(DATE,Getdate()) 
      and shipment_status=7)i
Where CONVERT (Date,'date column of Order_Warehouse_Status)
          = CONVERT(DATE,Getdate())

No need to update 'date' column because it is already inserted in first query.Hope this will work if the inner select statement will return only one row..just check

like image 51
Sachu Avatar answered Feb 18 '26 01:02

Sachu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!