确定次序的问题
如下表:
SeqNoOrderNo PO ShipDatePacking ShipMode
NULL105800A 1591872 2010-12-15bul By Sea
NULL105800A 2587254 2010-12-8pre By Sea
NULL105800A 2587254 2010-12-8bul By Sea
NULL105800A 2587256 2010-12-8bul By Sea
NULL105800A 2587256 2010-12-8pre By Sea
NULL105800A 2589381 2010-12-8gbu By Air
NULL105800A 2589836 2010-12-8gbu By Air
NULL105800A 2590725 2010-11-24bul By Air
NULL105800A 2590725 2010-12-1bul By Air
NULL105800A 2590725 2010-12-1bul By Sea
希望达到如下要求:
同一个OrderNo,PO之下,如果ShipDate有几个的话,按照时间排序,Packing是pre优先于bul,ShipMode是By Air的优先于By Sea,更新SeqNo列,这里是Int类型。
到了第二个ShipDate,仍然是刚才的规则,更新SeqNo列,不过数值要比上一个ShipDate大一。比如:
SeqNoOrderNo PO ShipDatePacking ShipMode
1105800A 1591872 2010-12-15bul By Sea
1105800A 2587254 2010-12-8pre By Sea
2105800A 2587254 2010-12-8bul By Sea
2105800A 2587256 2010-12-8bul By Sea
1105800A 2587256 2010-12-8pre By Sea
1105800A 2589381 2010-12-8gbu By Air
1105800A 2589836 2010-12-8gbu By Air
1105800A 2590725 2010-11-24bul By Air
2105800A 2590725 2010-12-1bul By Air
3105800A 2590725 2010-12-1bul By Sea
请各位不吝赐教,SQL2000,拜托了!
[解决办法]
declare @a table(SeqNo int,OrderNo varchar(20),PO int,ShipDate datetime,Packing varchar(20),ShipMode varchar(20))
insert @a select NULL,'105800A',1591872,'2010-12-15','bul','By Sea'
union all select NULL,'105800A',2587254,'2010-12-8','pre','By Sea'
union all select NULL,'105800A',2587254,'2010-12-8','bul','By Sea'
union all select NULL,'105800A',2587256,'2010-12-8','bul','By Sea'
union all select NULL,'105800A',2587256,'2010-12-8','pre','By Sea'
union all select NULL,'105800A',2589381,'2010-12-8','gbu','By Air'
union all select NULL,'105800A',2589836,'2010-12-8','gbu','By Air'
union all select NULL,'105800A',2590725,'2010-11-24','bul','By Air'
union all select NULL,'105800A',2590725,'2010-12-1','bul','By Air'
union all select NULL,'105800A',2590725,'2010-12-1','bul','By Sea'
SELECT SeqNo=(SELECT COUNT(1)+1 FROM @a WHERE OrderNo=a.OrderNo AND PO=a.PO AND (ShipDate<a.ShipDate OR (ShipDate=a.ShipDate AND Packing>a.Packing)
OR (ShipDate=a.ShipDate AND Packing=a.Packing AND ShipMode<a.ShipMode))
),* FROM @a a
ORDER BY OrderNo,PO,ShipDate,1
--result
/*SeqNo SeqNo OrderNo PO ShipDate Packing ShipMode
----------- ----------- -------------------- ----------- ------------------------------------------------------ -------------------- --------------------
1 NULL 105800A 1591872 2010-12-15 00:00:00.000 bul By Sea
1 NULL 105800A 2587254 2010-12-08 00:00:00.000 pre By Sea
2 NULL 105800A 2587254 2010-12-08 00:00:00.000 bul By Sea
1 NULL 105800A 2587256 2010-12-08 00:00:00.000 pre By Sea
2 NULL 105800A 2587256 2010-12-08 00:00:00.000 bul By Sea
1 NULL 105800A 2589381 2010-12-08 00:00:00.000 gbu By Air
1 NULL 105800A 2589836 2010-12-08 00:00:00.000 gbu By Air
1 NULL 105800A 2590725 2010-11-24 00:00:00.000 bul By Air
2 NULL 105800A 2590725 2010-12-01 00:00:00.000 bul By Air
3 NULL 105800A 2590725 2010-12-01 00:00:00.000 bul By Sea
(所影响的行数为 10 行)*/