pivot - MySQL converting colums to rows -
i'm trying figure out how count presidents, managers etc , convert columns rows in 1 query. example there sample db 'employee' here: http://www.mysqltutorial.org/tryit/ can count employees of types using query this:
select sum(case when jobtitle = 'president' 1 else 0 end) 'presidents', sum(case when jobtitle 'vp%' 1 else 0 end) 'vps', sum(case when jobtitle '%manager%' 1 else 0 end) 'managers', sum(case when jobtitle '%rep' 1 else 0 end) 'reps' employees;
but want convert columns rows , have no idea how include in query similar answer here: mysql convert column row (pivot table ) have idea?
you use sub query standardise job titles, group , order case statement to produce output in descending order of bossiness.
select jobtitle,count(*) ( select case when jobtitle = 'president' 'presidents' when jobtitle 'vp%' 'vps' when jobtitle '%manager%' 'managers' when jobtitle '%rep' 'reps' end jobtitle employees ) s group jobtitle order case when jobtitle = 'presidents' 1 when jobtitle = 'vps' 2 when jobtitle = 'managers' 3 when jobtitle = 'reps' 4 end
Comments
Post a Comment