SELECT a.OCD, a.[f_name],a.[l_name],a.[ads_phone], b.wk1m, b.wk1t, b.wk1w, b.wk1r, b.wk1f, b.wk2m, b.wk2t, b.wk2w, b.wk2r, b.wk2f
FROM [dbo].[tbl_teleworkers] as a
inner join [dbo].[tbl_scheduled] as b ON a.pin = b.pin
where ocd = '022'
这是我的结果:
OCD|f_name|l_name|ads_phone|wk1m|wk1t|wk1w|wk1r|wk1f|wk2m|wk2t|wk2w|wk2r|wk2f
022|John |smith |111-1111 |M | | | R | | | | | |
022|Jane |smith |222-2222 | | | | R | | | | W | |
022|Joe |smith |333-3333 |M | | | | F | | | | |
022|Jim |smith |444-4444 | | T | | | | M | | | |
022|Jill |smith |555-5555 |M | | W | | | | | | R |
以下是我想要得到的内容:因此,我试图展示星期一出现的所有星期一人,而不是一周的字母。像这样......
wk1m |wk1t |wk1w |wk1r |wk1f |wk2m |wk2t|wk2w |wk2r |wk2f
John |Jim |Jill |John |Joe |Jim | |Jane |Jill |
phone|phone|phone|phone|phone|phone| |Phone |phone|
Joe | | |Jane | | | | | |
Phone| | |phone| | | | | |
Jill | |
Phone| |
这是我到目前为止所尝试的内容:
SELECT *
FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY f_name ASC) AS Row, f_name,
CASE WHEN b.wk1m != '' then f_name + ' ' + l_name END as first,
CASE WHEN b.wk1t != '' then f_name + ' ' + l_name END as wk1t,
CASE WHEN b.wk1w != '' then f_name + ' ' + l_name END as wk1w,
CASE WHEN b.wk1r != '' then f_name + ' ' + l_name END as wk1r,
CASE WHEN b.wk1f != '' then f_name + ' ' + l_name END as wk1f,
CASE WHEN b.wk2m != '' then f_name + ' ' + l_name END as wk2m,
CASE WHEN b.wk2t != '' then f_name + ' ' + l_name END as wk2t,
CASE WHEN b.wk2w != '' then f_name + ' ' + l_name END as wk2w,
CASE WHEN b.wk2r != '' then f_name + ' ' + l_name END as wk2r,
CASE WHEN b.wk2f != '' then f_name + ' ' + l_name END as wk2f
FROM [dbo].[tbl_teleworkers] as a
inner join [dbo].[tbl_scheduled] as b ON a.pin = b.pin
where ocd = '165'
) as S
PIVOT
(
MAX (row) FOR f_name in ([wk1m])
) as PivotTable;
非常感谢任何帮助。 谢谢!
答案 0 :(得分:0)
请尝试这个并让我知道它是否有效
SELECT
wk1m = CASE WHEN ISNULL(b.wk1m,'')<>''
THEN a.[f_name]
ELSE '' END,
wk1t = CASE WHEN ISNULL(b.wk1t,'')<>''
THEN a.[f_name]
ELSE '' END,
wk1w = CASE WHEN ISNULL(b.wk1w,'')<>''
THEN a.[f_name]
ELSE '' END,
wk1r = CASE WHEN ISNULL(b.wk1r,'')<>''
THEN a.[f_name]
ELSE '' END,
wk1f = CASE WHEN ISNULL(b.wk1f,'')<>''
THEN a.[f_name]
ELSE '' END,
wk2m = CASE WHEN ISNULL(b.wk2m,'')<>''
THEN a.[f_name]
ELSE '' END,
wk2t = CASE WHEN ISNULL(b.wk2t,'')<>''
THEN a.[f_name]
ELSE '' END,
wk2w = CASE WHEN ISNULL(b.wk2w,'')<>''
THEN a.[f_name]
ELSE '' END,
wk2r = CASE WHEN ISNULL(b.wk2r,'')<>''
THEN a.[f_name]
ELSE '' END,
wk2f = CASE WHEN ISNULL(b.wk2f,'')<>''
THEN a.[f_name]
ELSE '' END
FROM [dbo].[tbl_teleworkers] as a
inner join [dbo].[tbl_scheduled] as b
ON a.pin = b.pin
where ocd = '022'