sql统计某年的12个月工作数据

SELECT
p.proj_no AS pno,
p.proj_name AS pname,
w.work_name AS wname,
wt.work_id AS wid,
wt. YEAR,
sum(
CASE wt.MONTH
WHEN 1 THEN
wt.work_time
ELSE
0
END
)AS january,
sum(
CASE wt. MONTH
WHEN 2 THEN
wt.work_time
ELSE
0
END
)AS february,
sum(
CASE wt. MONTH
WHEN 3 THEN
wt.work_time
ELSE
0
END
)AS march,
sum(
CASE wt. MONTH
WHEN 4 THEN
wt.work_time
ELSE
0
END
)AS april,
sum(
CASE wt. MONTH
WHEN 5 THEN
wt.work_time
ELSE
0
END
)AS may,
sum(
CASE wt. MONTH
WHEN 6 THEN
wt.work_time
ELSE
0
END
)AS june,
sum(
CASE wt. MONTH
WHEN 7 THEN
wt.work_time
ELSE
0
END
)AS july,
sum(
CASE wt. MONTH
WHEN 8 THEN
wt.work_time
ELSE
0
END
)AS august,
sum(
CASE wt. MONTH
WHEN 9 THEN
wt.work_time
ELSE
0
END
)AS september,
sum(
CASE wt. MONTH
WHEN 10 THEN
wt.work_time
ELSE
0
END
)AS october,
sum(
CASE wt. MONTH
WHEN 11 THEN
wt.work_time
ELSE
0
END
)AS november,
sum(
CASE wt. MONTH
WHEN 12 THEN
wt.work_time
ELSE
0
END
)AS december,
sum(wt.work_time)AS totaltime -- 年工时统计
FROM
tb_work_time wt,
tb_work w,
tb_project p
WHERE
wt.work_id = w.work_id
AND p.proj_id = w.proj_id
AND wt. YEAR = '2015' -- 统计2015年的数据
GROUP BY
wt.work_id -- 根据work_id分组

你可能感兴趣的:(mysql)