查询使用每天更新的另一个表中的数据更新表

时间:2014-04-02 18:14:20

标签: sql sql-server

我有一个复杂的查询,它创建了一个运行某些报告所需的数据表。现在我们希望以我们可以运行它的方式更改查询,并使用任何新日期更新创建的表。我认为基本的想法是我需要加入这两个表,并根据记录的日期时间戳比表2中的任何内容更新来运行更新。由于某种原因,它没有认识到表2有一个recordcreatedate字段但是它确实并且标记正确。我将包含整个查询,以便您可以看到我正在使用的内容。

Insert into table_2
select RecordCreateDate,ShipperInf_CoilId, shipperinf_Ordernumber,orderinf_ordernumber,
(select  datediff(second, min(case when PilerInf_CreateDate = '1900-01-01 00:00:00.000'
then shipperinf_createdate else PilerInf_CreateDate end), max(case when 
ShipperInf_CreateDate= '1900-01- 01 00:00:00.000' then PilerInf_CreateDate else 
ShipperInf_CreateDate end)) FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] where
ShipperInf_CoilId = l2.ShipperInf_CoilId) as seconds, 

(select sum( case when cast([ShipperInf_Length]as float ) > 0 then cast([ShipperInf_Length] as
float) * [ShipperInf_ActualPieces] else cast(orderinf_orderedlength as float) * 
[ShipperInf_ActualPieces] end) FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] 
where ShipperInf_CoilId = l2.ShipperInf_CoilId and ShipperInf_ScreenName = 'shipping') as 
totalinch,

((select case when (select  datediff(second, min(case when PilerInf_CreateDate = '1900-01-01 
00:00:00.000' then shipperinf_createdate else PilerInf_CreateDate end), max(case when 
ShipperInf_CreateDate = '1900-01-01 00:00:00.000' then PilerInf_CreateDate else 
ShipperInf_CreateDate end)) FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] where 
ShipperInf_CoilId = l2.ShipperInf_CoilId)  =0 then null else sum( case when cast
([ShipperInf_Length]as float ) > 0 then cast([ShipperInf_Length] as float) * 
[ShipperInf_ActualPieces] else cast(orderinf_orderedlength as float) * [ShipperInf_ActualPieces] 
end)end FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] where ShipperInf_CoilId = 
l2.ShipperInf_CoilId and ShipperInf_ScreenName = 'shipping')/(select  datediff(second, min(case
when PilerInf_CreateDate = '1900-01-01 00:00:00.000' then shipperinf_createdate else 
PilerInf_CreateDate end), max(case when ShipperInf_CreateDate = '1900-01-01 00:00:00.000' then 
PilerInf_CreateDate else ShipperInf_CreateDate end)) FROM [nucor_historical_data].[dbo].
[tblL3SendProductionData] where ShipperInf_CoilId = l2.ShipperInf_CoilId) 
) as inchpersec,
ShipperInf_CoilIdLiftLetter, 
ActualGauge, ActualWidth, ShipperInf_Status, [ShipperInf_ActualPieces],  
[ShipperInf_LiftWeight],cast([OrderInf_MaxLiftWeight] as float) as MaxLiftWeight,  case when cast
([ShipperInf_Length]as float ) > 0 then cast([ShipperInf_Length] as float) *  
[ShipperInf_ActualPieces] else cast(orderinf_orderedlength as float) * [ShipperInf_ActualPieces]
 end as inches,

((case when cast([ShipperInf_Length]as float ) > 0 then cast([ShipperInf_Length] as float) * 
[ShipperInf_ActualPieces] else cast(orderinf_orderedlength as float) * [ShipperInf_ActualPieces]
end)/((select case when (select  datediff(second, min(case when PilerInf_CreateDate = '1900-01- 
01 00:00:00.000' then shipperinf_createdate else PilerInf_CreateDate end), max(case when 
ShipperInf_CreateDate = '1900-01-01 00:00:00.000' then PilerInf_CreateDate else 
ShipperInf_CreateDate end)) FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] where 
ShipperInf_CoilId = l2.ShipperInf_CoilId) =0 then null else sum(case when cast
([ShipperInf_Length]as float ) > 0 then cast([ShipperInf_Length] as float) * 
[ShipperInf_ActualPieces] else cast(orderinf_orderedlength as float) * [ShipperInf_ActualPieces]
end)end 

FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] where ShipperInf_CoilId = 
l2.ShipperInf_CoilId and ShipperInf_ScreenName = 'shipping')

/(select  datediff(second, min(case
when PilerInf_CreateDate = '1900-01-01 00:00:00.000' then shipperinf_createdate else 
PilerInf_CreateDate end), max(case when ShipperInf_CreateDate = '1900-01-01 00:00:00.000' then 
PilerInf_CreateDate else ShipperInf_CreateDate end)) FROM [nucor_historical_data].[dbo].
[tblL3SendProductionData] where ShipperInf_CoilId = l2.ShipperInf_CoilId))) as processingseconds

FROM [nucor_historical_data].[dbo].[tblL3SendProductionData] l2 
where l2.RecordCreateDate > table_2.RecordCreateDate 
group by  RecordCreateDate,ShipperInf_CoilId, shipperinf_ordernumber, 
orderinf_ordernumber,ShipperInf_CoilIdLiftLetter,ActualGauge, ActualWidth, Shipperinf_Status,
[ShipperInf_ActualPieces],ShipperInf_LiftWeight, orderinf_MaxLiftWeight,[ShipperInf_Length],
orderinf_orderedlength

2 个答案:

答案 0 :(得分:0)

删除除

以外的所有内容
Insert into table_2

select 
    RecordCreateDate,
    ShipperInf_CoilId, 
    shipperinf_Ordernumber,
    orderinf_ordernumber,   
    ShipperInf_CoilIdLiftLetter, 
    ActualGauge, 
    ActualWidth, 
    ShipperInf_Status, 
    ShipperInf_ActualPieces,  
    ShipperInf_LiftWeight,
    MaxLiftWeight=cast([OrderInf_MaxLiftWeight] as float),  
    --
    -- !!!! THESE VALUES ARE NOT NEEDED TO TEST WHY YOUR INSERT IS NOT WORKING AS EXPECTED.
    --
    seconds=0,
    totalinch=0,
    inchpersec=0,
    inches=0,
    processingseconds=0
FROM 
    nucor_historical_data.dbo.tblL3SendProductionData AS l2 
where 
    l2.RecordCreateDate > table_2.RecordCreateDate 
group by  
    RecordCreateDate,ShipperInf_CoilId, 
    shipperinf_ordernumber, 
    orderinf_ordernumber,
    ShipperInf_CoilIdLiftLetter,
    ActualGauge, 
    ActualWidth, 
    Shipperinf_Status,
    ShipperInf_ActualPieces,
    ShipperInf_LiftWeight, 
    orderinf_MaxLiftWeight,
    ShipperInf_Length,
    orderinf_orderedlength

这样可以更容易地确定您是否需要从table_2获取MAX(RecordCreateDate)以确定适合插入的内容。

答案 1 :(得分:0)

试试这个:

DECLARE @LastUpdateDate DATETIME
SELECT @LastUpdateDate =MAX(RecordCreateDate) FROM table_2

Insert into table_2 
....
WHERE
    l2.RecordCreateDate > @LastUpdateDate