我有一个(格式不正确的)每周声明,需要重新格式化,直到最近这才不是问题。
现在,语句发布者在语句中添加了额外的字段,这意味着事情不再保持良好的一致性。
我设法通过以下方法复制了旧的输出格式:
SELECT
Field1
,CASE WHEN Field1 = 'DG3' THEN Field3 ELSE Field2 END AS Field2
,CASE WHEN Field1 = 'DG3' THEN Field4 ELSE Field3 END AS Field3
,CASE WHEN Field1 = 'DG3' THEN Field10 ELSE Field9 END AS Field9
,CASE WHEN Field1 = 'DG3' THEN Field11 ELSE Field10 END AS Field10
,CASE WHEN Field1 = 'DG3' THEN Field12 ELSE Field11 END AS Field11
,CASE WHEN Field1 = 'DG3' THEN Field13 ELSE Field12 END AS Field12
,CASE WHEN Field1 = 'DG3' THEN Field15 ELSE Field14 END AS Field14
,CASE WHEN Field1 = 'DG3' THEN Field16 ELSE Field15 END AS Field15
,CASE WHEN Field1 = 'DG3' THEN Field17 ELSE Field16 END AS Field16
,CASE WHEN Field1 = 'DG3' THEN Field18 ELSE Field17 END AS Field17
,CASE WHEN Field1 = 'DG3' THEN Field19 ELSE Field18 END AS Field18
,CASE WHEN Field1 = 'DG3' THEN Field20 ELSE Field19 END AS Field19
,CASE WHEN Field1 = 'DG3' THEN Field21 ELSE Field20 END AS Field20
,CASE WHEN Field1 = 'DG3' THEN Field22 ELSE Field21 END AS Field21
,CASE WHEN Field1 = 'DG3' THEN Field23 ELSE Field22 END AS Field22
,CASE WHEN Field1 = 'DG3' THEN Field24 WHEN Field1 = 'DG4' THEN Field43 ELSE Field23 END AS Field23
,CASE WHEN Field1 = 'DG3' THEN Field25 WHEN Field1 = 'DG4' THEN Field44 ELSE Field24 END AS Field24
,CASE WHEN Field1 = 'DG3' THEN Field44 WHEN Field1 = 'DG4' THEN Field45 ELSE Field25 END AS Field25
,CASE WHEN Field1 = 'DG3' THEN Field46 WHEN Field1 = 'DG4' THEN Field47 ELSE Field27 END AS Field27
,CASE WHEN Field1 = 'DG3' THEN Field48 WHEN Field1 = 'DG4' THEN Field49 ELSE Field29 END AS Field29
,CASE WHEN Field1 = 'DG3' THEN Field50 WHEN Field1 = 'DG4' THEN Field51 ELSE Field31 END AS Field31
,CASE WHEN Field1 = 'DG3' THEN Field57 WHEN Field1 = 'DG4' THEN Field58 ELSE Field38 END AS Field38
,CASE WHEN Field1 = 'DG3' THEN Field58 WHEN Field1 = 'DG4' THEN Field59 ELSE Field39 END AS Field39
,CASE WHEN Field1 = 'DG3' THEN Field59 WHEN Field1 = 'DG4' THEN Field60 ELSE Field40 END AS Field40
,CASE WHEN Field1 = 'DG3' THEN Field60 WHEN Field1 = 'DG4' THEN Field61 ELSE Field41 END AS Field41
有什么方法可以简化此过程而无需编辑原始文档吗?
答案 0 :(得分:0)
在SQL Server上,我发现使用=
设置别名的方式更易于阅读:
SELECT
Field1 = Field1,
Field2 = CASE WHEN Field1 = 'DG3' THEN Field3 ELSE Field2 END,
Field3 = CASE WHEN Field1 = 'DG3' THEN Field4 ELSE Field3 END,
Field9 = CASE WHEN Field1 = 'DG3' THEN Field10 ELSE Field9 END,
Field10 = CASE WHEN Field1 = 'DG3' THEN Field11 ELSE Field10 END,
Field11 = CASE WHEN Field1 = 'DG3' THEN Field12 ELSE Field11 END,
Field12 = CASE WHEN Field1 = 'DG3' THEN Field13 ELSE Field12 END,
--...
Field23 = CASE WHEN Field1 = 'DG3' THEN Field24 WHEN Field1 = 'DG4' THEN Field43 ELSE Field23 END,
Field24 = CASE WHEN Field1 = 'DG3' THEN Field25 WHEN Field1 = 'DG4' THEN Field44 ELSE Field24 END,
Field25 = CASE WHEN Field1 = 'DG3' THEN Field44 WHEN Field1 = 'DG4' THEN Field45 ELSE Field25 END,
Field27 = CASE WHEN Field1 = 'DG3' THEN Field46 WHEN Field1 = 'DG4' THEN Field47 ELSE Field27 END
--...
也可以将IIF
用于简单的CASE
(SQL Server 2012 +):
SELECT
Field1 = Field1,
Field2 = IIF(Field1 = 'DG3', Field3, Field2),
Field3 = IIF(Field1 = 'DG3', Field4, Field3),
Field9 = IIF(Field1 = 'DG3', Field10, Field9),
Field10 = IIF(Field1 = 'DG3', Field11, Field10),
--...
Field23 = IIF(Field1 = 'DG3', Field24, IIF(Field1 = 'DG4', Field43, Field23)),
--...
如果性能不是问题,并且您有密钥,则可以使用LEFT JOIN
Field1 = 'DG3'
记录,并避免对每个CASE
进行此检查(您将需要{{ 1}}或ISNULL
)。
答案 1 :(得分:0)
您可以通过将Field1放在WHEN
前来避免重复Field1多次
select
Field1
,CASE Field1 WHEN 'DG3' THEN Field3 ELSE Field2 END AS Field2
,CASE Field1 WHEN 'DG3' THEN Field4 ELSE Field3 END AS Field3
,CASE Field1 WHEN 'DG3' THEN Field10 ELSE Field9 END AS Field9
,CASE Field1 WHEN 'DG3' THEN Field11 ELSE Field10 END AS Field10
,CASE Field1 WHEN 'DG3' THEN Field12 ELSE Field11 END AS Field11
,CASE Field1 WHEN 'DG3' THEN Field13 ELSE Field12 END AS Field12
,CASE Field1 WHEN 'DG3' THEN Field15 ELSE Field14 END AS Field14
,CASE Field1 WHEN 'DG3' THEN Field16 ELSE Field15 END AS Field15
,CASE Field1 WHEN 'DG3' THEN Field17 ELSE Field16 END AS Field16
,CASE Field1 WHEN 'DG3' THEN Field18 ELSE Field17 END AS Field17
,CASE Field1 WHEN 'DG3' THEN Field19 ELSE Field18 END AS Field18
,CASE Field1 WHEN 'DG3' THEN Field20 ELSE Field19 END AS Field19
,CASE Field1 WHEN 'DG3' THEN Field21 ELSE Field20 END AS Field20
,CASE Field1 WHEN 'DG3' THEN Field22 ELSE Field21 END AS Field21
,CASE Field1 WHEN 'DG3' THEN Field23 ELSE Field22 END AS Field22
,CASE Field1 WHEN 'DG3' THEN Field24 WHEN 'DG4' THEN Field43 ELSE Field23 END AS Field23 ...