Good Guy Good Guy -4 years ago 121
SQL Question

sql select display row data as columns

I have a sql select query that extracts result as:

login_count login_type
2000 iPhone
7000 browser


But i want the result as:

iphone_login browser_login
2000 7000


i.e. i want to extract row1-col1 as col1 and row2-col2 as col2 using a select query.

My original query is

select count(login_count), login_type from log_table group by login_type;


Thanks,
Gaurav

Answer Source

Try this:

SELECT
    SUM( IF(login_type = 'iPhone', 1, 0) ) AS iphone_login,
    SUM( IF(login_type = 'browser', 1, 0) ) AS browser_login
FROM log_table
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download