根据最大日期和分组更新列

时间:2012-02-07 16:59:47

标签: sql sql-server sql-server-2005 tsql

我有三个表订单,orders_delivered,orders_delivered_sta 并且三个表中的数据看起来像

表订单

orders_id 
 10
 11                      
 12
 13

table orders_delivered

orders_delivered_id    orders_id
       10                 1000                                  
       10                 1001                                  
       11                 1002                            
       12                 1003
       12                 1004
       13                 1005 
       13                 1006
       13                 1007

表orders_delivered_sta

  orders_delivered_sta_id   orders_delivered_id   date     now_ind
            1                  1000             02/11/2011   0
            2                  1000             01/10/2006   0
            3                  1000             09/13/2011   0
            4                  1001             01/19/2010   0
            5                  1001             02/21/2011   0
            6                  1002             02/11/2009   0
            7                  1002             08/27/2010   0
            8                  1003             07/15/2012   0
            9                  1004             03/09/2007   0
            10                 1010             10/01/2010   0
            11                 1011             03/27/2011   0
            12                 1012             07/25/2010   0
            13                 1013             09/18/2004   0

所以我需要更新orders_delivered_sta表,以便now_ind对于一个orders_delivered_id的最大日期应为1 对于一个orders_delivered_id 1000,最大日期是09/13/2011,对于这组orders_delivered_id和日期(1000,09 / 13/2011),now_ind应该是1,如果列orders_delivered_id只有一个id那么应该是改为1

order_delivered_sta表中有一些数据不在订单中,而orders_delivered表中则不需要更改。在oreders_delivered表中的orders_delivered_id只需要更改

因此所需的输出应该是

表orders_delivered_sta

 orders_delivered_sta_id   orders_delivered_id   date     now_ind
            1                  1000             02/11/2011   0
            2                  1000             01/10/2006   0
            3                  1000             09/13/2011   1
            4                  1001             01/19/2010   0
            5                  1001             02/21/2011   1
            6                  1002             02/11/2009   0
            7                  1002             08/27/2010   1
            8                  1003             07/15/2012   1
            9                  1004             03/09/2007   1
           10                 1010             10/01/2010   0
           11                 1011             03/27/2011   0
           12                 1012             07/25/2010   0
           13                 1013             09/18/2004   0

表格结构:

create table orders
(
 order_id int primary key  
)

insert into orders select 10
insert into orders select 11                      
insert into orders select 12
insert into orders select 13

create table orders_delivered
( 
  orders_delivered_id int primary key, 
   orders_id int FOREIGN KEY(orders_id)REFERENCES orders (orders_id)
)
insert into orders_delivered select   1000,10                                  
insert into orders_delivered select   1001,10                                  
insert into orders_delivered select   1002,11                            
insert into orders_delivered select   1003,12
insert into orders_delivered select   1004,12
insert into orders_delivered select   1005,13 
insert into orders_delivered select   1006,13
insert into orders_delivered select   1007,13

create table orders_delivered_sta
( 
  orders_delivered_sta_id int primary key, 
   orders_delivered_id int FOREIGN KEY(orders_delivered_id)REFERENCES orders_delivered (orders_delivered_id), 
   date char(10),
  now_ind int
)

insert into orders_delivered_sta select 1,1000,'02/11/2011', 0
insert into orders_delivered_sta select 2,1000,'01/10/2006', 0
insert into orders_delivered_sta select 3,1000,'09/13/2011', 0
insert into orders_delivered_sta select 4,1001,'01/19/2010', 0
insert into orders_delivered_sta select 5,1001,'02/21/2011', 0
insert into orders_delivered_sta select 6,1002,'02/11/2009', 0
insert into orders_delivered_sta select 7,1002,'08/27/2010', 0
insert into orders_delivered_sta select 8,1003,'07/15/2012', 0
insert into orders_delivered_sta select 9,1004,'03/09/2007', 0
insert into orders_delivered_sta select 10,1010,'10/01/2010', 0
insert into orders_delivered_sta select 11,1011,'03/27/2011', 0
insert into orders_delivered_sta select 12,1012,'07/25/2010', 0
insert into orders_delivered_sta select 13,1013,'09/18/2004', 0

3 个答案:

答案 0 :(得分:3)

您可以使用CTE和窗口MAX()

;
WITH max_dates AS (
  SELECT
    *,
    max_date = MAX(date) OVER (PARTITION BY orders_delivered_id)
  FROM orders_delivered_sta
  WHERE orders_delivered_id IN (SELECT orders_delivered_id FROM orders_delivered)
)
UPDATE max_dates
SET now_ind = 1
WHERE date = max_date

参考文献:

答案 1 :(得分:0)

试试这个:

UPDATE    orders_delivered_sta
SET       now_ind = 1
WHERE     orders_delivered_sta_id IN(
          SELECT     orders_delivered_sta_id
          FROM       (
                      SELECT     orders_delivered_sta_id,
                                 ROW_NUMBER() OVER(PARTITION BY orders_delivered_id ORDER BY date DESC) AS num
                      FROM       orders_delivered_sta) AS T
          WHERE      T.num = 1)

答案 2 :(得分:0)

这是MySQL中的查询,但是将其转换为SQL-Server应该是直截了当的,因为我使用的是纯SQL。请注意我已将日期更改为其他形式(YYYY-MM-DD),以避免从字符串到目前为止的铸件。

update t3
set t3.now_ind = 1
where t3.orders_delivered_sta_id in (
    select distinct t1.orders_delivered_sta_id from t1
    left join (
        select t2.orders_delivered_id, max(t2.adate) as MaxDate from t2
        group by t2.orders_delivered_id
    ) t2 on (t1.orders_delivered_id = t2.orders_delivered_id) and (t1.adate = t2.MaxDate)
    where t2.orders_delivered_id is not null
) and exists (
    select * from o1
    join od1 on (o1.order_id = od1.orders_delivered_id)
    where (t3.orders_delivered_id = od1.orders_id)
)

这是example

希望这有帮助

PS:你确实需要这3张牌......下次我会更好地阅读问题:)