使用最新订单附加过去订单的SQL查询

时间:2016-03-29 19:46:30

标签: sql-server datetime max

我有一张订单表,其中包含过去的会员资料和当前数据。我想以单行查看此数据。我有一个过去数据的临时表,但不完全确定如何编写此查询以获取同一行中的当前数据。我知道它与MAX(order no)有关。以下是在临时表中获取过去成员资格数据的查询

set transaction isolation level read uncommitted

declare 
@ship_master_customer_id varchar (10),  @cycle_begin_date datetime,  @cycle_end_date datetime,  @OrderNo varchar(10), @Description Char(100)

create table #t1(ShipMasterCustomerID varchar(10),  OrderNo varchar (10), cycle_begin_date datetime,  cycle_end_date datetime,  Description Char(100))

Insert into #t1 

Select SHIP_MASTER_CUSTOMER_ID, ORDER_NO, CYCLE_BEGIN_DATE,CYCLE_END_DATE, DESCRIPTION FROM [ORDER_DETAIL]

where SHIP_MASTER_CUSTOMER_ID = '11115555' and 
CYCLE_END_DATE = '2/29/2016'

Select * from #t1
Drop table #t1



Here is my script. 

    declare 
    @ship_master_customer_id varchar (10),  @cycle_begin_date datetime,  @cycle_end_date datetime,  @OrderNo varchar(10), @Description Char(100)

    create table #t2(ShipMasterCustomerID varchar(10),  OrderNo varchar (10), cycle_begin_date datetime,  cycle_end_date datetime,  Description Char(100))

    Insert into #t2 (shipmastercustomerid, orderno, cycle_begin_date, cycle_end_date, DESCRIPTION)

    VALUES (1111555,9004731815, 2015/01/01, 2015/31/12,'Annual Mem'), 
    (1111555, 9005148308, 2016/01/01, 2016/31/12,'Annual Mem'), 
    (1111222, 9005027152, 2015/01/03, 2016/29/02,'Annual Mem'), 
    (1111222, 9005440369, 2016/01/03, 2017/31/03,'Annual Mem'),
    (2223333, 9005027152, 2014/01/01, 2016/31/12,'Annual Mem'), 
    (2223333, 9005442116, 2016/01/01, 2017/31/12,'Annual Mem')

Select * from #t2

Drop table #t2

enter image description here

Sample Data

1 个答案:

答案 0 :(得分:0)

您不需要临时表。您可以查询同一个表两次,为其指定别名,然后使用别名为列名添加前缀。既然你没有给我们一个完整的架构或小提琴我用模板表模拟你的数据库,但本质就在这里。但是你有一些注意事项没有提及。您是否确保每位客户都拥有历史和当前记录?如果没有,由于INNER JOIN,它们不会出现在下面的查询中。您可以将其更改为OUTER连接,但是当客户没有新记录时,您将在这些列中看到NULL值。我的观点是,这里有龙......这绝不是一个完整或无助的解决方案,只是在正确的方向上推动。

DECLARE @ORDER_DETAIL AS TABLE(
    ShipMasterCustomerId varchar(20),
    OrderNo varchar(20),
    cycle_begin_date date,
    cycle_end_date date,
    Description varchar(100)
)

INSERT @ORDER_DETAIL SELECT '11115555', '9005337015', '02/26/15', '2/29/16', 'Membership 26-Feb-2015 to 29-Feb-2016'
INSERT @ORDER_DETAIL SELECT '11115555', '9005743023', '02/28/17', '2/28/17', 'Membership 01-Mar-2016 to 28-Feb-2017'

SELECT 
    hist.ShipMasterCustomerId, 
    hist.OrderNo, 
    hist.cycle_begin_date,
    hist.CYCLE_END_DATE, 
    hist.[Description],
    curr.ShipMasterCustomerId, 
    curr.OrderNo, 
    curr.cycle_begin_date,
    curr.CYCLE_END_DATE, 
    curr.[Description]
FROM 
    @ORDER_DETAIL AS hist 
    INNER JOIN @ORDER_DETAIL AS curr ON (
            (curr.ShipMasterCustomerId = hist.ShipMasterCustomerId) AND (curr.cycle_end_date = 
            (SELECT MAX(cycle_end_date) FROM @ORDER_DETAIL WHERE ShipMasterCustomerId = hist.ShipMasterCustomerId))
            )
WHERE 
    (hist.ShipMasterCustomerId = '11115555') 
    AND 
    (hist.cycle_end_date = '2/29/2016')