| |

VerySource

 Forgot password?
 Register
Search
View: 1376|Reply: 10

Seek expert advice here

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

Post time: 2020-1-21 21:20:01
| Show all posts |Read mode
IDX CODE DEPTCODE PARTNAME
-------------------------------------------------- ------
1 CN00001 CN00001001 PART1
2 CN00001 CN00001002 PART2
3 CN00001 CN00001001001 PART3
4 CN00001 CN00001002001 PART4
5 CN00002 CN00002001 PART5
6 CN00002 CN00002001001 PART6


IDX CODE DEPTCODE DETAIL
-----------------------------------------------
1 CN00001 CN0001001 CN0001001; PART1 | CN0001001001; PART3
2 CN00001 CN0001002 CN0001002; PART2 | CN0001002001; PART4
3 CN00002 CN0002001 CN0002001; PART5 | CN0002001001; PART6


DEPTCODE is the department code, the rule is that CODE + 3 digits are the first-level department, and so on, the lower-level department adds 3 digits to the upper-level department code
The result I want is just to list the first-level department information, and the lower-level department information is categorized into a DETAIL field. Please give me pointers.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

 Author| Post time: 2020-1-31 10:00:01
| Show all posts
Above is the original data, below is the desired result,

There are infinitely many levels in theory.
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-1-31 10:45:01
| Show all posts
Follow, learn
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-1-31 11:27:01
| Show all posts
Ideas; define process rank conversion ~
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-1-31 14:27:02
| Show all posts
Is the definition of the function row and column conversion:
example:
-There is a table t whose data is as follows:
  a b
  1 1
  1 2
  1 3
  twenty one
  twenty two
  3 1
--How to convert into the following results:
  a b
  1 1,2,3
  2 1,2
  3 1
create table tb
(
  a int,
  b int
)
insert into tb (a, b) values ​​(1,1)
insert into tb (a, b) values ​​(1,2)
insert into tb (a, b) values ​​(1,3)
insert into tb (a, b) values ​​(2,1)
insert into tb (a, b) values ​​(2,2)
insert into tb (a, b) values ​​(3,1)
go

--Create a merged function
create function f_hb (@a int)
returns varchar (8000)
as
begin
  declare @str varchar (8000)
  set @str = ''
  select @str = @str + ',' + cast (b as varchar) from tb where a = @a
  set @str = right (@str, len (@str)-1)
  return (@str)
End
go

--Call the custom function to get the result:
select distinct a, dbo.f_hb (a) as b from tb

drop table tb

--result
a b
----------- ------
1 1,2,3
2 1,2
3 1
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-1-31 15:27:02
| Show all posts
create table T (IDX int, CODE varchar (50), DEPTCODE varchar (50), PARTNAME varchar (50))
insert T select 1, 'CN00001', 'CN00001001', 'PART1'
union all select 2, 'CN00001', 'CN00001002', 'PART2'
union all select 3, 'CN00001', 'CN00001001001', 'PART3'
union all select 4, 'CN00001', 'CN00001002001', 'PART4'
union all select 5, 'CN00002', 'CN00002001', 'PART5'
union all select 6, 'CN00002', 'CN00002001001', 'PART6'

create function fun (@DEPTCODE varchar (50))
returns varchar (200)
as
begin
declare @re varchar (200)
set @ re = ''
select @ re = @ re + DEPTCODE + ';' + PARTNAME + '|' from T where DEPTCODE like @DEPTCODE + '%'
select @ re = left (@re, len (@re) -1)
Ranch
return @re
end

select IDX, CODE, DEPTCODE, DETAIL = dbo.fun (DEPTCODE) from T where len (DEPTCODE) = 10

--result
IDX CODE DEPTCODE DETAIL
----------- --------------------------------------- ----------- --------------------------------------- ----------- --------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------
1 CN00001 CN00001001 CN00001001; PART1 | CN00001001001; PART3
2 CN00001 CN00001002 CN00001002; PART2 | CN00001002001; PART4
5 CN00002 CN00002001 CN00002001; PART5 | CN00002001001; PART6

(3 row (s) affected)
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-31 16:45:01
| Show all posts
If there is only one layer
select CODE, DEPTCODE, DETAIL = (DEPTCODE + ';' + PARTNAME + '|' + (select DEPTCODE + ';' + PARTNAME from # where left (DEPTCODE, 10) = t.DEPTCODE and len (DEPTCODE) = 13)) from # t where len (DEPTCODE) = 10


--result

CN00001 CN00001001 CN00001001; PART1 | CN00001001001; PART3
CN00001 CN00001002 CN00001002; PART2 | CN00001002001; PART4
CN00002 CN00002001 CN00002001; PART5 | CN00002001001; PART6
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

 Author| Post time: 2020-2-1 10:36:01
| Show all posts
fanfan1980()
This method is good, I will try it out and post it later, thank you all,


There are good ways to look forward to it;
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-1 12:09:01
| Show all posts
create table ta (IDX int, CODE varchar (50), DEPTCODE varchar (50), PARTNAME varchar (50))
insert ta select 1, 'CN00001', 'CN00001001', 'PART1'
union all select 2, 'CN00001', 'CN00001002', 'PART2'
union all select 3, 'CN00001', 'CN00001001001', 'PART3'
union all select 4, 'CN00001', 'CN00001002001', 'PART4'
union all select 5, 'CN00002', 'CN00002001', 'PART5'
union all select 6, 'CN00002', 'CN00002001001', 'PART6'


create function test_f (@DEPTCODE varchar (50))
returns varchar (100)
as
begin
declare @s varchar (100)
select @ s = isnull (@s, '') + DEPTCODE + ':' + PARTNAME + '|' from ta where charindex (@ DEPTCODE, DEPTCODE) = 1
return @s
end

select IDX, CODE, show = dbo.test_f (DEPTCODE) from ta where len (dbo.test_f (DEPTCODE))> 21

IDX CODE display
----------- --------------------------------------- ----------- --------------------------------------- -------------------------------------------------- -----------
1 CN00001 CN00001001: PART1 | CN00001001001: PART3 |
2 CN00001 CN00001002: PART2 | CN00001002001: PART4 |
5 CN00002 CN00002001: PART5 | CN00002001001: PART6 |

(3 rows affected)
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-1 13:00:02
| Show all posts
insert ta select 1, 'CN00001', 'CN00001001', 'PART1'
union all select 2, 'CN00001', 'CN00001002', 'PART2'
union all select 3, 'CN00001', 'CN00001001001', 'PART3'
union all select 4, 'CN00001', 'CN00001002001', 'PART4'
union all select 5, 'CN00002', 'CN00002001', 'PART5'
union all select 6, 'CN00002', 'CN00002001001', 'PART6'

Change it:
create function test_f (@DEPTCODE varchar (50))
returns varchar (100)
as
begin
declare @s varchar (100)
select @ s = isnull (@s, '') + DEPTCODE + ':' + PARTNAME + '|' from ta where charindex (@ DEPTCODE, DEPTCODE) = 1
return left (@ s, len (@s) -1)
end

select IDX, CODE, DEPTCODE, show = dbo.test_f (DEPTCODE) from ta where len (dbo.test_f (DEPTCODE))> 21
1 CN00001 CN00001001 CN00001001: PART1 | CN00001001001: PART3
2 CN00001 CN00001002 CN00001002: PART2 | CN00001002001: PART4
5 CN00002 CN00002001 CN00002001: PART5 | CN00002001001: PART6
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list