Sql server TSQL联合,最大(日期)-有解决方案。需要改进吗

Sql server TSQL联合,最大(日期)-有解决方案。需要改进吗,sql-server,tsql,union,max,Sql Server,Tsql,Union,Max,下面的查询可以工作,但必须有更好的方法将表的值设置为两组数据并集的最大日期。以下是我所拥有的: Update stagingTable Set OverrideFlag = ( select total.overrideflag from ( select Customer_SSN as ssn, RequestDateTime as maxdate, overrideflag from tableA where RequestDateTime > '9/

下面的查询可以工作,但必须有更好的方法将表的值设置为两组数据并集的最大日期。以下是我所拥有的:

Update stagingTable
Set OverrideFlag = 
(
select total.overrideflag from
    (
    select Customer_SSN as ssn, RequestDateTime as maxdate, overrideflag
    from tableA
    where RequestDateTime > '9/1/2012'
    union
    select ssn, EntryDate as maxdate, overrideflag
    from tableB
    where EntryDate > '9/1/2012'
    )  total
    join
    (
    select ssn, max(maxdate) as maxdate from
        (
        select Customer_SSN as ssn, RequestDateTime as maxdate
        from tableA
        where RequestDateTime > '9/1/2012'
        union
        select ssn, EntryDate as maxdate
        from tableB
        where EntryDate > '9/1/2012'
        ) maxs
        group by ssn
    ) maxdates  on total.ssn = maxdates.ssn and total.maxdate = maxdates.maxdate        where total.ssn = stagingTable.ssn
)

看起来你做了两次完全相同的事情,所以我不需要定义两次并将其连接回自身,除非其中一个嵌套选择中有不同的内容。实际上,您正在两次编写同一条语句,冗余可能是一个问题,因为其中一个SELECT看起来完全冗余

-- this is a CTE and is better for reuse than a nested select as you can reference it
-- is as a base and reuse that, versus having to write the same statement twice.
;with a as 
    (
     select 
         Customer_SSN as ssn, 
         RequestDateTime as maxdate, 
         OverRideFlag,
         -- EDIT, you should be able to use a 'Windowed function' to get the maxDate
         max(RequestDateTime) over(partition by SSN order by RequestDateTime desc) as maxDate
     from tableA
     where RequestDateTime > '9/1/2012'
     union
     select 
          ssn, 
          EntryDate, 
          OverRideFlag, 
          max(RequestDateTime) over(partition by SSN order by RequestDateTime desc) as maxDate
     from tableB
     where EntryDate > '9/1/2012'
    )
Update stagingTable
Set OverrideFlag = total.overrideflag
from a total
   join stagingTable on total.ssn = stagingTable.ssn  
   -- do not know reference here so you need to add that table as a 'join' 
where total.EntryDate = total.maxDate

我还找到了一种不同的方法来处理临时表。我对这些已经很适应了,但我总是想看到一种不同的方式来做到这一点。不要失望

create table #tOvr(customerID varchar(15), ssn varchar(11), EntryDate datetime, overrideflag varchar(2))
insert into #tOvr
select customer_ID, Customer_SSN, RequestDateTime, overrideflag
from tableA
where RequestDateTime > '9/1/2012'
and Customer_ID in 
    (select contact_ID from stagingTable
    where Location_ID = @Location_ID)
insert into #tOvr
select Customer_ID, ssn, EntryDate, overrideflag
from tableB
where EntryDate > '9/1/2012'
and Customer_ID in 
    (select contact_ID from stagingTable
    where Location_ID = @Location_ID)

Update stagingTable
Set OverrideFlag =
    (select overrideflag from #tOvr
    where EntryDate = (select max(EntryDate) from #tOvr where #tOvr.customerID = stagingTable.contact_ID)
    )

您能提供您试图接收数据/表格的输入和输出示例吗?当然。有两张桌子,表A和表B。TableA有一个customerID varchar15、SSN varchar11、date datetime和overrideflag varchar2。表B也是如此。暂存表中有一个用于overrideflag的varchar2。我需要最新日期的overrideflag值,因此我需要maxmaxdate:选择ssn,maxmaxdate作为MaxDate我很抱歉,我错过了嵌套select的那部分。您应该能够使用“窗口函数”来获取该部件。请看我更新的示例。临时表的一个很酷的地方是,您不必先定义它们,然后再插入它们。我这个懒惰的人经常这样做:从对象中选择*进入临时状态。然后,如果我必须确保它不首先存在,我会在对象_id'tempdb..Temp'不是null的情况下执行此操作。一直偷懒并不是最好的做法,但如果你不挑剔声明类型,那么它可以为你节省大量代码,而且你可以这样假设它们。