简体   繁体   中英

SQL categorize table values

Hi all I have an issue related to my SQL table tbl_myTable and values like

rollid  | Name    | fileName
------------------
1       | aaaa    | 
2       | bbbb    |
3       | cccc    |
4       | dddd    |
5       | eeee    |
6       | ffff    |
7       | gggg    | 
8       | hhhh    |
9       | iiii    |
10      | jjjj    |
11      | kkkk    |
12      | llll    |

I need to categorize these values to 5 items block and then I need to update a fileName field in to each block like book1,book2,book3...etc. ie only 5 record exists in each book and my expected output look like

rollid  | Name    | fileName
------------------
1       | aaaa    | book1
2       | bbbb    | book1
3       | cccc    | book1
4       | dddd    | book1
5       | eeee    | book1
6       | ffff    | book2
7       | gggg    | book2
8       | hhhh    | book2
9       | iiii    | book2
10      | jjjj    | book2
11      | kkkk    | book3
12      | llll    | book3

You didn't specify if you need to base your "book groups" on the rollId or the name. I'm assuming it's the name since you said rollId might skip some values. If not you can easily swap out the order in the row_number() window function to correct it:

with tbl_myTable_mimic as (
    select 1 as rollid, 'aaaa' as name, '' as filename
    union select 2 as rollid, 'bbbb' as name, '' as filename
    union select 3 as rollid, 'cccc' as name, '' as filename
    union select 4 as rollid, 'dddd' as name, '' as filename
    union select 5 as rollid, 'eeee' as name, '' as filename
    union select 6 as rollid, 'ffff' as name, '' as filename
    union select 7 as rollid, 'gggg' as name, '' as filename
    union select 10 as rollid, 'hhhh' as name, '' as filename
    union select 11 as rollid, 'iiii' as name, '' as filename
    union select 12 as rollid, 'jjjj' as name, '' as filename
    union select 13 as rollid, 'kkkk' as name, '' as filename
    union select 15 as rollid, 'llll' as name, '' as filename
), j as (
    select 
        *
        , row_number() over (partition by 1 order by name asc, rollid asc) as rn
    from tbl_myTable_mimic 
), i as (
    select 
        *
        , 'book' + cast(((rn-1)/5)+1 as varchar(16)) as newFileName
    from 
        j 
)
select * from i 

Try this query:

update myTable 
set filename = 'book' + CAST((id / 6) + 1 as varchar)

Update

update a
set a.filename = 'book' + CAST((b.rownumber / 6) + 1 as varchar)
from myTable a
left join 
(
select id , ROW_NUMBER() OVER(order by id) as rownumber 
from myTable
) b
on a.id = b.id

I don't know how to sort your rows. but i think you can change the order by id to fit your pattern.

declare @T table
(
  rollid int primary key,
  Name varchar(10),
  fileName varchar(10)
)

insert into @T(rollid, Name) values
(1,        'aaaa'),
(2,        'bbbb'),
(3,        'cccc'),
(4,        'dddd'),
(5,        'eeee'),
(6,        'ffff'),
(7,        'gggg'),
(8,        'hhhh'),
(9,        'iiii'),
(10,       'jjjj'),
(11,       'kkkk'),
(12,       'llll')

update T
set filename = 'book'+cast((rn + 4) / 5 as varchar(10))
from 
  (
    select filename,
           row_number() over(order by rollid) as rn
    from @T
  ) as T

Got a silent downvote on this one so I guess an explanation on my part is required.

I use row_number in a derived table to generate an unbroken series of integers that can be used to calculate the book number by dividing with 5. In SQL Server it is possible to run an update statement against a derived table which means that there is no need to join the result back to the table.

Those of you who are still in doubt can try it out on SE-Data

update tbl_myTable set fileName ='book1' where rollid < 6
update tbl_myTable set fileName ='book2' where rollid between 7 and 10
update tbl_myTable set fileName ='book3' where rollid >10

Try out with following query:

 UPDATE tbl_myTable
SET fileName='book' + (1 + (CAST( (CAST(rollid AS INT) - 1) / 5) AS INT))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM