Sql select displays row data in columns

I have a sql select query that retrieves the result as:

login_count  login_type
2000         iPhone
7000         browser 

But I want to get 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 initial request

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

Thanks, Gaurav

+5
source share
2 answers

Try the following:

SELECT
    SUM( IF(login_type = 'iPhone', 1, 0) ) AS iphone_login,
    SUM( IF(login_type = 'browser', 1, 0) ) AS browser_login
FROM log_table
+3
source

Here is another option that works in MySQL and other dbms.

select sum(case when login_type = 'iPhone'  then 1 else 0 end) as iphone_login
      ,sum(case when login_type = 'browser' then 1 else 0 end) as browser_login
from log_table   
0
source

All Articles