【问题标题】:Concat the table records based on the condtion根据条件连接表记录
【发布时间】:2015-11-01 23:28:44
【问题描述】:

我正在构建一个 SSRS 报告来显示学生班级信息。 我有一个名为 class 的表,其中包含有关学生参加的班级的信息。

我的要求是显示上课的日期。例如:如果 Column Name Monday = 'Y' 那么我必须将日期显示为 [Mon]。我试过下面的代码。

SELECT 
    case when Monday   ='Y' then'[Mon]' else '' end +case when Monday ='Y' then ',' else '' end + 
    case when Tuesday  ='Y' then'[Tue]' else '' end +case when Tuesday ='Y' then ','else '' end + 
    case when Wednesday='Y' then'[Wed]' else '' end +case when Wednesday ='Y' then ',' else '' end + 
    case when Thursday ='Y' then'[Thu]' else '' end +case when Thursday ='Y' then ',' else '' end + 
    case when Friday   ='Y' then'[Fri]' else '' end +case when Friday ='Y' then ',' else ''  end + 
    case when Saturday ='Y' then'[Sat]' else '' end +case when Saturday ='Y' then ',' else ''  end + 
    case when Sunday   ='Y' then'[Sun]' else '' end as classday
    FROM vw_Class_Without_Instructor

我得到如下输出。

在输出中,每行末尾都有一个额外的逗号。有什么方法可以在 select 语句本身中排除它?有没有其他方法可以获得输出?使用 case 语句似乎会减慢执行速度。

【问题讨论】:

  • 您好,您需要一个用于替换逗号的查询或一个单独的查询来获得上述输出或两者兼而有之??
  • @Tarun 我两个都需要。我还担心由于过度使用案例而导致的任何性能损失
  • @bmsqldev 请检查我的答案。它应该工作。我通过减少案例数量提高了查询性能。坦率地说,您也可以使用PIVOT。此外,使用 INNER QUERY 的解决方案与直 select 相比有点贵
  • 我怎样才能使用枢轴。我认为枢轴用于将列转换为行

标签: sql-server tsql reporting-services ssms


【解决方案1】:

请尝试以下查询

SELECT 
    SUBSTRING(case when Monday   ='Y' then',[Mon]' else '' end + 
    case when Tuesday  ='Y' then',[Tue]' else '' end +
    case when Wednesday='Y' then',[Wed]' else '' end +
    case when Thursday ='Y' then',[Thu]' else '' end +
    case when Friday   ='Y' then',[Fri]' else '' end +
    case when Saturday ='Y' then',[Sat]' else '' end +
    case when Sunday   ='Y' then',[Sun]' else '' end, 2,200) as classday
    FROM vw_Class_Without_Instructor

技巧:我已经在文本中添加了逗号,所以没有一半的案例评估。使用,[Mon] 值,我确保开头始终有一个逗号,使用SUBSTRING 将其删除。 明智的做法是在 SUBSTRING 的最后一部分参数中选择一个非常高的值,以便它返回从第二部分的 charindex 开始的所有内容。

查看msdn参考:https://msdn.microsoft.com/en-us/library/ms187748.aspx

如果 start 和 length 之和大于 表达式中的字符,整个值表达式从 开始返回。

【讨论】:

  • 您好,感谢您的解决方案。但是为什么你提到 200 作为子字符串的分隔符?
【解决方案2】:

你可以试试这个;

select 
    CASE WHEN LEN(classday_with_comma) > 0 THEN LEFT(classday_with_comma,LEN(classday_with_comma) - 1) ELSE '' END as classday
From (
    SELECT 
        case when Monday    = 'Y' then '[Mon],' else '' end +
        case when Tuesday   = 'Y' then '[Tue],' else '' end +
        case when Wednesday = 'Y' then '[Wed],' else '' end +
        case when Thursday  = 'Y' then '[Thu],' else '' end +
        case when Friday    = 'Y' then '[Fri],' else '' end +
        case when Saturday  = 'Y' then '[Sat],' else '' end +
        case when Sunday    = 'Y' then '[Sun],' else '' end as classday_with_comma
    FROM vw_Class_Without_Instructor
)  as x

两个case语句可以被截断为一个

case when Monday   ='Y' then'[Mon]' else '' end + 
case when Monday ='Y' then ',' else '' end + 

case when Monday    = 'Y' then '[Mon],' else '' end +

LEFT(classday_with_comma, LEN(classday_with_comma) - 1)

将删除最后一个,

【讨论】:

    猜你喜欢
    • 2012-03-09
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2011-07-26
    相关资源
    最近更新 更多