0

i have a table : this is same with log for user login frequency

╔════════════╦═══════╦═════════════╗
║ month-year ║ user  ║ count_login ║
╠════════════╬═══════╬═════════════╣
║ 01-2016    ║ admin ║          12 ║
║ 01-2016    ║ user1 ║          10 ║
║ 02-2016    ║ admin ║           5 ║
║ 03-2016    ║ admin ║          15 ║
╚════════════╩═══════╩═════════════╝

its possiblly change to

╔════════════╦═══════╦═══════╗
║ month-year ║ admin ║ user1 ║
╠════════════╬═══════╬═══════╣
║ 01-2016    ║    12 ║    10 ║
║ 02-2016    ║     5 ║     0 ║
║ 03-2016    ║    15 ║     0 ║
╚════════════╩═══════╩═══════╝

if possible, what query can i use??

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
Yanuar Ihsan
  • 115
  • 11

1 Answers1

0

Use Conditional Aggregate to do this

select `month-year`, 
       sum(case when user = 'admin' then count_login else 0 end) as admin,
       sum(case when user = 'user1' then count_login else 0 end) as user1
From yourtable
Group by `month-year`

Check this answer for dynamic pivoting : MySQL pivot row into dynamic number of columns

Community
  • 1
  • 1
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172