我有一张有10列的表格。我必须为该表插入数据。我有一些插入语句。第一个插入语句插入来自一个源的前3行的数据。现在,我想使用来自不同源的另一个insert语句为同一行中的下一列插入数据。这些插入查询每天针对表Order_Warehouse_Status运行,因此我们每日交易将有1行。
实施例。表Order_Warehouse_Status有10列,如
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
在第一个查询下面插入前3列中的数据。第二个查询应该在同一行中插入下一列的数据。怎么做到这一点?
- 第一次查询
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
- 第二次查询
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
提前致谢
答案 0 :(得分:2)
首先查询将与插入相同,第二个查询可以通过检查日期是否为今天的日期来执行带有where条件的Update语句...
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())
无需更新'date'列,因为它已经插入到第一个查询中。如果内部select语句只返回一行,这将有效。请检查
答案 1 :(得分:0)
您的第一个和第二个查询都只返回一行 - 当前日期的一些摘要。
在单个查询中计算此摘要并将INSERT
完整结果作为一行而不是先插入部分结果,然后查找为了更新找到的行而插入的行,这有点合乎逻辑。结果的第二部分。
WITH
CTE_Printed
AS
(
SELECT
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
)
,CTE_Picking_Scheduled
AS
(
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
)
INSERT INTO Order_Warehouse_Status
([Date],
Printed_PPS_Shipment,
Printed_Shipment_Lines,
Printed_Unit,
Picking_Scheduled_Orders,
Picking_Scheduled_Lines,
Picking_Scheduled_Units)
SELECT
GETDATE() AS [Date],
Printed_PPS_Shipment,
Printed_Shipment_Lines,
Printed_Unit,
Picking_Scheduled_Orders,
Picking_Scheduled_Lines,
Picking_Scheduled_Units
FROM
CTE_Printed CROSS JOIN CTE_Picking_Scheduled
;
请注意,我在此处使用CROSS JOIN
加入了两个查询的结果,没有任何条件。在这种情况下,它将高效且正确地工作,因为两个查询中的每一个都只返回一行。
如果您遇到某种情况,最终会出现多行,那么您需要确定使用某种条件连接两个结果的方法。