SQLupdate在具有公共部分的多行上

时间:2017-04-24 10:14:13

标签: sql sql-server

我想要更新的桌子上需要帮助

我当前的表running_purchase

Today       My_purchase    My_Product   delivery_day    day_late    late_bool
---------------------------------------------------------------------------
2017-04-19  0000001        123456       2017-04-15      4           1 
2017-04-19  0000001        234567       2017-04-20      0           0
2017-04-19  0000001        345678       2017-04-20      0           0
2017-04-19  0000002        456789       2017-04-18      1           1
2017-04-19  0000002        567891       2017-05-15      0           0
2017-04-19  0000003        678912       2017-04-18      1           1
2017-04-19  0000003        789123       2017-04-24      0           0

我想要的表running_purchase

Today       My_purchase    My_Product   delivery_day    day_late    late_bool
-------------------------------------------------------------------------
2017-04-19  0000001        123456       2017-04-15      4           1 
2017-04-19  0000001        234567       2017-04-20      0           1
2017-04-19  0000001        345678       2017-04-20      0           1
2017-04-19  0000002        456789       2017-04-18      1           1
2017-04-19  0000002        567891       2017-05-15      0           1
2017-04-19  0000003        678912       2017-04-18      1           1
2017-04-19  0000003        789123       2017-04-24      0           1

因为一件产品迟到,所以购买迟到了。

SQL中是否有可以更改的请求?

这是一个例子,每天大约有5k行

我尝试加入My_purchase并且没有达到标准。

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以使用LEFT JOIN

DECLARE @running_purchase AS TABLE
(
   Today date,
   My_purchase varchar(10),
   MyProduct int,
   delivery_day datetime,
   day_late int,
   late_bool bit
)

INSERT INTO @running_purchase
VALUES
('2017-04-19',  '0000001',       123456,     '2017-04-15',     4,          1),
('2017-04-19',  '0000001',       234567,     '2017-04-20',     0,          0),
('2017-04-19',  '0000001',       345678,     '2017-04-20',     0,          0),
('2017-04-19',  '0000002',       456789,     '2017-04-18',     1,          1),
('2017-04-19',  '0000002',       567891,     '2017-05-15',     0,          0),
('2017-04-19',  '0000003',       678912,     '2017-04-18',     1,          1),
('2017-04-19',  '0000003',       789123,     '2017-04-24',     0,          0)

SELECT DISTINCT 
      rp1.Today,rp1.My_purchase,
      rp1.My_purchase,rp1.delivery_day,rp1.day_late, 
      coalesce(rp2.late_bool, 0) AS latebool
FROM @running_purchase rp1
LEFT JOIN @running_purchase rp2 ON rp1.My_purchase = rp2.My_purchase AND rp2.late_bool = 1

或更新

UPDATE rp1
SET rp1.late_bool = coalesce(rp2.late_bool, 0) 
FROM @running_purchase rp1
LEFT JOIN @running_purchase rp2 ON rp1.My_purchase = rp2.My_purchase AND rp2.late_bool = 1

演示链接:Rextester

答案 1 :(得分:0)

您可以使用窗口功能轻松完成此操作:

select rp.*,
       (case when max(rp.day_late) over (partition by rp.my_purchase) > 0
             then 1 else 0
        end) as late_bool
from running_purchase rp;

通常,窗口函数比其他替代方法具有更好的性能,以获得相同的结果。

答案 2 :(得分:0)

此方法也可以使用

SELECT *,ISNULL((   SELECT TOP 1 1 
                    FROM running_purchase 
                    WHERE day_late > 0 AND My_Product = p.My_Product),0)    AS late_bool
FROM running_purchase p