根据条件连接 table 条记录
Concat the table records based on the condtion
我正在构建 SSRS 报告以显示学生 Class 信息。
我有一个名为 class 的 table,其中包含有关 classes 学生参加的信息。
我的要求是显示 classes 发生的日期。例如:如果 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 语句似乎会减慢执行速度。
请尝试以下查询
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 的最后一部分参数中选择一个非常高的值是明智的,这样它 return 从第二部分的 charindex 开始的所有内容。
参见 msdn 参考:https://msdn.microsoft.com/en-us/library/ms187748.aspx
If the sum of start and length is greater than the number of
characters in expression, the whole value expression beginning at
start is returned.
你可以试试这个;
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)
将删除最后一个 ,
我正在构建 SSRS 报告以显示学生 Class 信息。
我有一个名为 class 的 table,其中包含有关 classes 学生参加的信息。
我的要求是显示 classes 发生的日期。例如:如果 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 语句似乎会减慢执行速度。
请尝试以下查询
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 的最后一部分参数中选择一个非常高的值是明智的,这样它 return 从第二部分的 charindex 开始的所有内容。
参见 msdn 参考:https://msdn.microsoft.com/en-us/library/ms187748.aspx
If the sum of start and length is greater than the number of characters in expression, the whole value expression beginning at start is returned.
你可以试试这个;
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)
将删除最后一个 ,