行转列SQL语句

select top 10 * from schedule_month where years=2016 and months=7 and managerid=45
SELECT top 10 cast(years AS varchar) + '-' + RIGHT('0' + CAST(months AS varchar), 2) + '-' + RIGHT('0' + REPLACE(days,
'day', ''), 2) AS trueday, job_title_id, managerid, years, months, days, dayvalue
FROM (SELECT job_title_id, managerid, years, months, day1, day2, day3, day4, day5, day6, day7, day8, day9,
day10, day11, day12, day13, day14, day15, day16, day17, day18, day19, day20, day21, day22,
day23, day24, day25, day26, day27, day28, day29, day30, day31
FROM schedule_month) p UNPIVOT (dayvalue FOR days IN (day1, day2, day3, day4, day5, day6, day7,
day8, day9, day10, day11, day12, day13, day14, day15, day16, day17, day18, day19, day20, day21, day22,
day23, day24, day25, day26, day27, day28, day29, day30, day31))
AS unpvt/*years=@years and months=@months*/

posted @ 2016-08-17 11:04  Alfred.Xu  阅读(220)  评论(0编辑  收藏  举报