27,579
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select '0-16' as a union select '0-1-6'
)T
order by a
/*
0-16
0-1-6
*/
select * from
(
select '0-16' as a union select '0-1-5'
)T
order by a
/*
0-1-5
0-16
*/
CREATE TABLE [Department1](
[ID] [bigint] NOT NULL,
[No] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[ParentID] [bigint] NULL
)
insert into [Department1]
select 1, 'DNMC', '运营公司', 0 union all
select 7, 'OPS', '生产部', 1 union all
select 8, 'aaaa', 'aaaa', 1 union all
select 9, 'bbbb', 'bbbb', 8 union all
select 10, 'cccc', 'cccc', 8 union all
select 11, 'ddd', 'dddd', 8 union all
select 12, 'vvv', '维修部', 1 union all
select 13, '2233', '技术部', 1 union all
select 14, 'eeee', 'eeee', 7 union all
select 15, 'ffff', 'ffff', 7 union all
select 16, 'test', 'test', 0 union all
select 17, 'rrrrr', 'rrrr', 7 union all
select 18, 'iiii', 'iiii', 10 union all
select 28, '33', '经理室', 13 union all
select 29, 'test111', 'test222', 1
;with SubTab([Level],[PID],[ID],[No],[Name],[Path]) as
(
select 1,[ParentID],[ID],[No],[Name],cast(ltrim(ParentID)+'-'+ltrim(ID) as varchar(4000))
from dbo.Department1
where [ParentID]=0
union all
select b.[Level]+1,a.[ParentID],a.[ID],a.[No],a.[Name],cast((b.[Path]+'-'+ltrim(a.[ID])) as varchar(4000))
from dbo.Department1 a,SubTab b
where a.[ParentID]=b.[ID]
)
select * from SubTab order by [Path]
/*
1 0 1 DNMC 运营公司 0-1
2 1 12 vvv 维修部 0-1-12
2 1 13 2233 技术部 0-1-13
3 13 28 33 经理室 0-1-13-28
2 1 29 test111 test222 0-1-29
1 0 16 test test 0-16
2 1 7 OPS 生产部 0-1-7
3 7 14 eeee eeee 0-1-7-14
3 7 15 ffff ffff 0-1-7-15
3 7 17 rrrrr rrrr 0-1-7-17
2 1 8 aaaa aaaa 0-1-8
3 8 10 cccc cccc 0-1-8-10
4 10 18 iiii iiii 0-1-8-10-18
3 8 11 ddd dddd 0-1-8-11
3 8 9 bbbb bbbb 0-1-8-9
*/
;with SubTab([Level],[PID],[ID],[No],[Name],[Path]) as
(
select 1,[ParentID],[ID],[No],[Name],cast(ltrim(ParentID)+'->'+ltrim(ID) as varchar(4000))
from dbo.Department1
where [ParentID]=0
union all
select b.[Level]+1,a.[ParentID],a.[ID],a.[No],a.[Name],cast((b.[Path]+'->'+ltrim(a.[ID])) as varchar(4000))
from dbo.Department1 a,SubTab b
where a.[ParentID]=b.[ID]
)
select * from SubTab order by [Path]
/* 正确结果
1 0 1 DNMC 运营公司 0->1
2 1 12 vvv 维修部 0->1->12
2 1 13 2233 技术部 0->1->13
3 13 28 33 经理室 0->1->13->28
2 1 29 test111 test222 0->1->29
2 1 7 OPS 生产部 0->1->7
3 7 14 eeee eeee 0->1->7->14
3 7 15 ffff ffff 0->1->7->15
3 7 17 rrrrr rrrr 0->1->7->17
2 1 8 aaaa aaaa 0->1->8
3 8 10 cccc cccc 0->1->8->10
4 10 18 iiii iiii 0->1->8->10->18
3 8 11 ddd dddd 0->1->8->11
3 8 9 bbbb bbbb 0->1->8->9
1 0 16 test test 0->16
*/
with SubTab as
(
select 1 as [Level],[ParentID],[ID],[No],[Name]
,cast(ltrim(ParentID)+'-'+ltrim(ID) as varchar(max)) as path
,CAST(row_number() over(order by id) as varBINARY(max)) as path1
from dbo.Department1
where [ParentID]=0
union all
select b.[Level]+1,a.[ParentID],a.[ID],a.[No],a.[Name]
,b.[Path]+'-'+ltrim(a.[ID])
,path1+CAST(row_number() over(partition by a.[ParentID] order by a.id) as BINARY(4))
from dbo.Department1 a,SubTab b
where a.[ParentID]=b.[ID]
)
select * from SubTab order by [Path1]
Level ParentID ID No Name path path1
----------- -------------------- -------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 0 1 DNMC 运营公司 0-1 0x0000000000000001
2 1 7 OPS 生产部 0-1-7 0x000000000000000100000001
3 7 14 eeee eeee 0-1-7-14 0x00000000000000010000000100000001
3 7 15 ffff ffff 0-1-7-15 0x00000000000000010000000100000002
3 7 17 rrrrr rrrr 0-1-7-17 0x00000000000000010000000100000003
2 1 8 aaaa aaaa 0-1-8 0x000000000000000100000002
3 8 9 bbbb bbbb 0-1-8-9 0x00000000000000010000000200000001
3 8 10 cccc cccc 0-1-8-10 0x00000000000000010000000200000002
4 10 18 iiii iiii 0-1-8-10-18 0x0000000000000001000000020000000200000001
3 8 11 ddd dddd 0-1-8-11 0x00000000000000010000000200000003
2 1 12 vvv 维修部 0-1-12 0x000000000000000100000003
2 1 13 2233 技术部 0-1-13 0x000000000000000100000004
3 13 28 33 经理室 0-1-13-28 0x00000000000000010000000400000001
2 1 29 test111 test222 0-1-29 0x000000000000000100000005
1 0 16 test test 0-16 0x0000000000000002
(15 行受影响)
CREATE TABLE [Department1](
[ID] [bigint] NOT NULL,
[No] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[ParentID] [bigint] NULL
)
insert into [Department1]
select 1, 'DNMC', '运营公司', 0 union all
select 7, 'OPS', '生产部', 1 union all
select 8, 'aaaa', 'aaaa', 1 union all
select 9, 'bbbb', 'bbbb', 8 union all
select 10, 'cccc', 'cccc', 8 union all
select 11, 'ddd', 'dddd', 8 union all
select 12, 'vvv', '维修部', 1 union all
select 13, '2233', '技术部', 1 union all
select 14, 'eeee', 'eeee', 7 union all
select 15, 'ffff', 'ffff', 7 union all
select 16, 'test', 'test', 0 union all
select 17, 'rrrrr', 'rrrr', 7 union all
select 18, 'iiii', 'iiii', 10 union all
select 28, '33', '经理室', 13 union all
select 29, 'test111', 'test222', 1
with SubTab as
(
select 1 as [Level],[ParentID],[ID],[No],[Name]
,cast(ltrim(ParentID)+'-'+ltrim(ID) as varchar(max)) as path
,cast(1 as varbinary(max)) as path1
from dbo.Department1
where [ParentID]=0
union all
select b.[Level]+1,a.[ParentID],a.[ID],a.[No],a.[Name]
,b.[Path]+'-'+ltrim(a.[ID])
,path1+CAST(row_number() over(partition by a.[ParentID] order by a.id) as BINARY(4))
from dbo.Department1 a,SubTab b
where a.[ParentID]=b.[ID]
)
select * from SubTab order by [Path1]
Level ParentID ID No Name path path1
----------- -------------------- -------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 0 1 DNMC 运营公司 0-1 0x00000001
1 0 16 test test 0-16 0x00000001
2 1 7 OPS 生产部 0-1-7 0x0000000100000001
2 1 8 aaaa aaaa 0-1-8 0x0000000100000002
2 1 12 vvv 维修部 0-1-12 0x0000000100000003
2 1 13 2233 技术部 0-1-13 0x0000000100000004
2 1 29 test111 test222 0-1-29 0x0000000100000005
3 13 28 33 经理室 0-1-13-28 0x000000010000000400000001
3 8 9 bbbb bbbb 0-1-8-9 0x000000010000000200000001
3 8 10 cccc cccc 0-1-8-10 0x000000010000000200000002
3 8 11 ddd dddd 0-1-8-11 0x000000010000000200000003
4 10 18 iiii iiii 0-1-8-10-18 0x00000001000000020000000200000001
3 7 14 eeee eeee 0-1-7-14 0x000000010000000100000001
3 7 15 ffff ffff 0-1-7-15 0x000000010000000100000002
3 7 17 rrrrr rrrr 0-1-7-17 0x000000010000000100000003
(15 行受影响)
select top 9 rn=ltrim(row_number()over(order by getdate())),rn_unicode=unicode(ltrim(row_number()over(order by getdate()))) from sysobjects
union all
select '-',unicode('-')
/*
rn rn_unicode
------------------------ -----------
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
- 45
(10 行受影响)
*/
再看看这个,问题哪里复杂select top 9 rn=ltrim(row_number()over(order by getdate())),rn_unicode=unicode(ltrim(row_number()over(order by getdate()))) from sysobjects
union all
select '|',unicode('|')
/*
rn rn_unicode
------------------------ -----------
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
| 124
(10 行受影响)
*/
麻烦你找一下,哪条不是按照这个规律来排序的。IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([col] [nvarchar](10))
INSERT INTO [tb]
SELECT '0-1-5' UNION ALL
SELECT '0-16' union all
select '0-1-6'
go
SELECT * FROM [tb] order by cast(col as BINARY)
/*
col
----------
0-1-5
0-1-6
0-16
*/