質問

I have one table and would like to populate the ImageType_page using COLORID for records with the same ID.

Conditions:

  • If any of the Page# with the same ID has a COLORID = 1, SET ImageType_page = TIFF for all Page# within that same ID
  • If any of the Page# with the same ID has a COLORID = 2, SET ImageType_page = TIFF-JPEG for all Page# within that same ID

Here is my table:

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      | 

Results should be:

 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

Here is some code I wrote that doesn't work. Any help would be greatly appreciated.

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)
役に立ちましたか?

解決

One way is to get all the IDs that have ColorID = 2 and join to that in update:

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

Alternatively you can group by ID and look for max 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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top