SQL对表值进行分类

时间:2012-03-15 05:11:48

标签: sql sql-server sql-server-2008

大家好我有一个与我的SQL表tbl_myTable相关的问题和像

这样的值
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    |

我需要将这些值分类为5个块,然后我需要更新每个块中的fileName字段,如book1,book2,book3等。即每本书中只有5条记录,我的预期输出看起来像

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

5 个答案:

答案 0 :(得分:2)

您没有指定是否需要将您的“图书组”基于rollId或名称。我假设它是名字,因为你说rollId可能会跳过一些值。如果没有,您可以轻松地在row_number()窗口函数中交换顺序来纠正它:

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 

答案 1 :(得分:1)

尝试此查询:

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

<强>更新

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

我不知道如何排序你的行。但我认为你可以改变order by id以适应你的模式。

答案 2 :(得分:1)

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

对此进行了默认下注,所以我猜测我需要做出解释。

我在派生表中使用row_number生成一系列不间断的整数,可用于通过除以5来计算图书编号。在SQL Server中, 可以运行针对派生表的更新语句,这意味着无需将结果连接回表。

那些仍有疑问的人可以在SE-Data

上试一试

答案 3 :(得分:0)

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

答案 4 :(得分:0)

尝试使用以下查询:

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