[英]CONCAT in SQL Server with a CASE WHEN troubleshooting
Demand Structure需求结构
select
Model,
concat('',max(case when ranking = 1 then [Demand Structure] end ),round(cast(max(case when ranking = 1 then [Demand Structure] as float/count([Demand Structure]))*100,2),'%') top1_Demand_Structure,
concat('',max(case when ranking = 2 then [Demand Structure] end ),round(cast(max(case when ranking = 2 then [Demand Structure] as float/count([Demand Structure]))*100,2),'%') top2_Demand_Structure
--into #demand_structure_temp
from
(
SELECT
Model,
[Demand Structure],
COUNT(1)Demand_Structure_count,
row_number() OVER(partition by model ORDER BY count(1) DESC) as Ranking
FROM [EDW].[sio].[TB_R_SURVEY_IN_OPERATION]
group by Model,[Demand Structure]
)a
group by model
order by 1;
round(cast(max(case when ranking = 1 then [Demand Structure] as float and round(cast(max(case when ranking = 2 then [Demand Structure] as float round(cast(max(当排名 = 1 然后 [需求结构] 为 float和round(cast(max(当排名 = 2 然后 [需求结构] 为 float
this two is highlighted by sql server as the error.这两个被 sql 服务器突出显示为错误。
I plan to show the percentage result of the numbers returned in this query我计划显示此查询中返回的数字的百分比结果
example of the result:结果示例:
|Top_1_Age|
-----------
|A 50% |
|B 35% |
any suggestions?有什么建议么?
Your parentheses are off.你的括号没了。 In complicated expressions, I often try to align opening and closing parens so the logic is clear:
在复杂的表达式中,我经常尝试对齐左括号和右括号,以便逻辑清晰:
select
Model,
concat('',
max(case when ranking = 1 then [Demand Structure] end),
round(cast(max(case when ranking = 1 then [Demand Structure] end
) as float
) /
count([Demand Structure]) * 100, 2
), '%') as top1_Demand_Structure,
concat('',
max(case when ranking = 2 then [Demand Structure] end ),
round(cast(max(case when ranking = 2 then [Demand Structure]
) as float
) /
count([Demand Structure]) * 100, 2
), '%') as top2_Demand_Structure
--into #demand_structure_temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.