如何根据颜色ID确定图像类型

时间:2014-03-04 20:49:18

标签: sql-server

我有一个表,并希望使用COLORID为具有相同ID的记录填充ImageType_page。

条件:

  • 如果具有相同ID的任何页面#具有COLORID = 1,则相同ID中所有页面#的SET ImageType_page = TIFF
  • 如果具有相同ID的任何页面#具有COLORID = 2,则对于同一ID内的所有页面#的SET ImageType_page = TIFF-JPEG

这是我的表:

Page#     | ID  |   PageColor      |  COLORID   | ImageType_page   
AT000001  | 1   |   Black & White  |     1      | 
AT000002  | 1   |   Color          |     2      | 
AT000003  | 2   |   Color          |     2      | 
AT000004  | 2   |   Black & White  |     1      | 
AT000005  | 3   |   Black & White  |     1      | 
AT000006  | 3   |   Black & White  |     1      | 

结果应该是:

 Page#    |    ID   |   PageColor      |  COLORID   | ImageType_page

AT000001  | 1   |   Black & White  |     1      | TIFF-JPEG
AT000002  | 1   |   Color          |     2      | TIFF-JPEG
AT000003  | 2   |   Color          |     2      | TIFF-JPEG
AT000004  | 2   |   Black & White  |     1      | TIFF-JPEG
AT000005  | 3   |   Black & White  |     1      | TIFF
AT000006  | 3   |   Black & White  |     1      | TIFF

这是我编写的一些代码不起作用的代码。任何帮助将不胜感激。

update dbo.tblPage
set [IMAGE TYPE_page] = 'TIFF-JPEG'
Where [COLORID] = 2

update td
set [IMAGE TYPE_page] = (select [IMAGE TYPE_page] FROM tblPAGE td2 where td2.id = td.ID)
from tblPAGE td
where (COLORID = 1)

1 个答案:

答案 0 :(得分:0)

一种方法是获取ColorID = 2的所有ID并加入更新中的ID:

with cte as
(
  select distinct id from tblPage where colorid = 2
)
update tbl
set imagetype = CASE WHEN cte.id IS NOT NULL THEN 'TIFF-JPEG' ELSE 'TIFF' END
from tblPage tbl
left join cte on cte.id = tbl.id

或者,您可以按ID分组并查找最大ColorID:

with cte as
(
  select id, max(colorid) as maxcolorid
  from tblPage
  group by id
)
update tbl
set imagetype = CASE cte.maxcolorid WHEN 2 THEN 'TIFF-JPEG' ELSE 'TIFF' END
from tblPage tbl
inner join cte on cte.id = tbl.id