求SQL

hjj841020 2008-10-25 11:45:55
sql:
原单号 新单号
0001 0001
0001 0002
0002 0003
0010 0010
0010 0020
0020 0030
............
如何得到以下结果:(新增一列即得到最早的原单号)
结果:
原单号 新单号 最早单号
0001 0001 0001
0001 0002 0001
0002 0003 0001
0010 0010 0010
0010 0020 0010
0020 0030 0010
...全文
215 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjj841020 2008-10-28
  • 打赏
  • 举报
回复
你的方法是不错,但都是要先排序好.
如果一些无规则排序的,如何按父子排序呢?
declare @t table
(
id int identity(1,1),
原单号 char(12),
新单号 char(12)
)

select '000001249723','000001249723'
union all select '000001249723','000003002094'
union all select '000001249744','000001249744'
union all select '000001249744','000002009406'
union all select '000001249744','000002009543'
union all select '000001249747','000001249747'
union all select '000001249747','000002009388'
union all select '000001249747','000002009880'
union all select '000002009543','000003009880'

结果排序结果应该是
000001249723 000001249723
000001249723 000003002094
000001249744 000001249744
000001249744 000002009406
000001249744 000002009543
000002009543 000003009880
000001249747 000001249747
000001249747 000002009388
000001249747 000002009880
hjj841020 2008-10-27
  • 打赏
  • 举报
回复
是你说的ID列吗?我试试,谢谢你啊~~

hjj841020 2008-10-27
  • 打赏
  • 举报
回复
是的,那时候没太注意.
CN_SQL 2008-10-27
  • 打赏
  • 举报
回复
如果你的环境允许有这样一个列,你增加这个列来处理好了,相比写个函数比较简便些。
hjj841020 2008-10-27
  • 打赏
  • 举报
回复
sql 2000 语句怎么写呢?
CN_SQL 2008-10-27
  • 打赏
  • 举报
回复
我那样的写法,只是一个取巧的写法,是需要有一个自增主键,实际上应该是这样:

declare @t table
(
id int identity(1,1),
原单号 char(5),
新单号 char(5)
)

insert @t select '00010','00010'
union all select '00010','00020'
union all select '00020','00021'
union all select '00011','00011'
union all select '00011','00021'
union all select '00021','00022'
union all select '0010','0010'
union all select '0010','0020'
union all select '0020','0030'

select
a.原单号,
a.新单号,
最早单号 = (
select top 1 新单号
from @t
where 原单号 = 新单号
and id <= a.id
order by 新单号 desc)
from @t a

之前因为给你的数据,正好新单号符合自增,所以用了这列,实际算法应该是一个父子的关系,所以必须要用到递归,如果你是2005
你用乌龟和枪的方法就可以了,如果是2000,恐怕你要写个辅助函数了。
hjj841020 2008-10-27
  • 打赏
  • 举报
回复
sql 2000怎么样写呢?
-狙击手- 2008-10-27
  • 打赏
  • 举报
回复
-- Test Data: ta
IF OBJECT_ID('ta') IS NOT NULL
DROP TABLE ta
Go
CREATE TABLE ta(原单号 NVARCHAR(8),新单号 NVARCHAR(8))
Go
insert ta select '00010','00010'
union all select '00010','00020'
union all select '00020','00021'
union all select '00011','00011'
union all select '00011','00021'
union all select '00021','00022'
union all select '0010','0010'
union all select '0010','0020'
union all select '0020','0030'

GO
--Start
;with t
as(
SELECT 原单号 ,新单号 ,原单号 as 最早单号 FROM ta where 原单号 = 新单号
union all
select ta.原单号 ,ta.新单号 ,t.原单号 as n from t ,ta where t.新单号 = ta.原单号 and t.新单号 < ta.新单号)
select * from t
order by 1,2
/*


(9 行受影响)
原单号 新单号 最早单号
-------- -------- --------
00010 00010 00010
00010 00020 00010
00011 00011 00011
00011 00021 00011
00020 00021 00010
00021 00022 00020
00021 00022 00011
0010 0010 0010
0010 0020 0010
0020 0030 0010

(10 行受影响)

*/
hjj841020 2008-10-27
  • 打赏
  • 举报
回复
如果改成这样.结果就不对了.
declare @t table
(
原单号 char(5),
新单号 char(5)
)

insert @t select '00010','00010'
union all select '00010','00020'
union all select '00020','00021'
union all select '00011','00011'
union all select '00011','00021'
union all select '00021','00022'
union all select '0010','0010'
union all select '0010','0020'
union all select '0020','0030'

select
a.原单号,
a.新单号,
最早单号 = (
select top 1 原单号
from @t
where 原单号 = 新单号
and 新单号 <= a.新单号
order by 新单号 desc)
from @t a

结果就变成这样了.
00010 00010 00010
00010 00020 00011
00020 00021 00011
00011 00011 00011
00011 00021 00011
00021 00022 00011
0010 0010 0010
0010 0020 0010
0020 0030 0010
-狙击手- 2008-10-26
  • 打赏
  • 举报
回复
------------------------------------
-- Author: happyflsytone
-- Date:2008-10-25 23:48:07
------------------------------------

-- Test Data: ta
IF OBJECT_ID('ta') IS NOT NULL
DROP TABLE ta
Go
CREATE TABLE ta(原单号 NVARCHAR(4),新单号 NVARCHAR(4))
Go
INSERT INTO ta
SELECT '0001','0001' UNION ALL
SELECT '0001','0002' UNION ALL
SELECT '0002','0003' UNION ALL
SELECT '0010','0010' UNION ALL
SELECT '0010','0020' UNION ALL
SELECT '0020','0030'
GO
--Start
;with t
as(
SELECT 原单号 ,新单号 ,原单号 as 最早单号 FROM ta where 原单号 = 新单号
union all
select ta.原单号 ,ta.新单号 ,t.原单号 as n from t ,ta where t.新单号 = ta.原单号 and t.新单号 < ta.新单号)
select * from t
order by 1,2


--Result:
/*
原单号 新单号 最早单号
---- ---- ----
0001 0001 0001
0001 0002 0001
0002 0003 0001
0010 0010 0010
0010 0020 0010
0020 0030 0010

(6 行受影响)


*/
--End
duanzhi1984 2008-10-26
  • 打赏
  • 举报
回复
3.4楼为正解
hjj841020 2008-10-26
  • 打赏
  • 举报
回复
谢谢你们的回答.
原单号 新单号 最早单号
0001 0001 0001 0001是正常单号.原单号与最早单号就是它本身.
0001 0002 0001 0002是返修的单号,它的原单号是0001.
0002 0003 0001 0003也是返修单号,它的原单号是0002,但它的最早单号是0001.
0010 0010 0010 0010是正常单号.原单号与最早单号就是它本身.
0010 0020 0010 0020是返修的单号,它的原单号是0010.
0020 0030 0010 0030也是返修单号,它的原单号是0020,但它的最早单号是0010.
CN_SQL 2008-10-25
  • 打赏
  • 举报
回复

declare @t table
(
原单号 char(4),
新单号 char(4)
)

insert @t select '0001','0001'
union all select '0001','0002'
union all select '0002','0003'
union all select '0010','0010'
union all select '0010','0020'
union all select '0020','0030'

select
a.原单号,
a.新单号,
最早单号 = (
select top 1 原单号
from @t
where 原单号 = 新单号
and 新单号 <= a.新单号
order by 新单号 desc)
from @t a
/**
0001 0001 0001
0001 0002 0001
0002 0003 0001
0010 0010 0010
0010 0020 0010
0020 0030 0010
**/

猜的~
Andy-W 2008-10-25
  • 打赏
  • 举报
回复
没有规则!
不知从何下刀。
CN_SQL 2008-10-25
  • 打赏
  • 举报
回复
最早单号是个什么概念,说清楚。

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧