sql select display row data as columns - sql

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

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

Here is another option that works in MySQL and in other dbms as well.
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

Related

SQL Server Count - Group By

I have a table contain records as per below image. I want to do a count for each status and am able to do that by selecting each type of status. Which I will needs to exec 4 query to get the result. I would like to know how can I achieve that by using single query statement? Any advices or suggestion is welcome and highly appreciated. Thanks in advance.
WITH CTE(NO,STATUS)AS
(
SELECT 1,'OPEN' UNION ALL
SELECT 2,'OPEN'UNION ALL
SELECT 3,'BILLED'UNION ALL
SELECT 4,'CANCELLED'UNION ALL
SELECT 5,'BILLING'UNION ALL
SELECT 6,'BILLED'UNION ALL
SELECT 7,'CANCELLED'UNION ALL
SELECT 8,'BILLING'UNION ALL
SELECT 9,'CONFIRM'UNION ALL
SELECT 10,'IN PROGRESS'UNION ALL
SELECT 11,'OPEN'UNION ALL
SELECT 12,'CONFIRM'
)
SELECT
SUM(CASE WHEN C.STATUS='BILLED'THEN 1 ELSE 0 END)AS BILLED,
SUM(CASE WHEN C.STATUS='BILLING'THEN 1 ELSE 0 END)AS BILLING,
SUM(CASE WHEN C.STATUS='CANCELLED'THEN 1 ELSE 0 END)AS CANCELLED,
SUM(CASE WHEN C.STATUS NOT IN ('CANCELLED','BILLING','BILLED')THEN 1 ELSE 0 END)AS UNBILL
FROM CTE AS C
CTE is an example you have provided. Please replace reference to it with reference to your table

SQL MAX value of two sub queries

I have two queries and I want to get the maximum value of the two of them.
MAX((SELECT COUNT(p.[ItemID]) FROM [dbo].[Table] p WHERE HasHuman=0),
(SELECT COUNT(p.[ItemID]) FROM [dbo].[Table] p WHERE HasHuman=1))
You can calculate both result in a single query and then apply TOP:
select top 1
HasHuman,
COUNT(p.[ItemID]) as cnt
from [dbo].[Table]
group by HasHuman
order by cnt desc
You could even do this in a single query:
SELECT
CASE WHEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END) >
SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END)
THEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END)
ELSE SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END) END
FROM [dbo].[Table]
WHERE ItemID IS NOT NULL -- you were not counting NULLs
SELECT MAX(RC)
FROM (SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
WHERE HasHuman=0
UNION
SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
WHERE HasHuman=1
) A

SQL Query to add result set values as column values

I want to add the below queries as the column values without creating a table.
Select 'NetworkKey' as AuthKey
Select count(NetworkSK) as Totalcount from EDW.Fact.AuthorizationRequest
Select count(*) as NUllcount from EDW.Fact.AuthorizationRequest where NetworkSK is NULL
Select count(*) as NotNullcount from EDW.Fact.AuthorizationRequest where NetworkSK is Not NULL
My result should look like this without creating a table physically...
AuthKey Totalcount Nullcount NotNullCount
NetworkKey 100 5 95
YOU WANT TO DO IT LIKE THIS BECAUSE THIS WILL WORK TO SOLVE YOUR PROBLEM.
SELECT 'NetworkKey' AS AuthKey,
COUNT(*) AS TotalCount,
SUM(CASE WHEN NetworkSK IS NULL THEN 1 ELSE 0 END) AS NUllcount,
SUM(CASE WHEN NetworkSK IS NOT NULL THEN 1 ELSE 0 END) AS NotNullcount
FROM EDW.Fact.AuthorizationRequest
Happy holidays.

sql query sum count

I have a table
id date state
1 10.01.01 reg
2 08.01.01 reg
3 05.01.01 check
4 02.01.01 check
5 01.01.01 reg
and want to show result like this
count reg
5 1
e.g sum of "reg" statuses should be counted only if the previous status was "check".
Please, help me or give the right direction to solve it
In SQL Server 2012 and above you could use LAG to access the previous row value:
SQL SERVER 2012
;WITH MyCTE AS
(
select id, date,state,lag(state,1) over(order by id) as prevstate
from Table1
)
SELECT COUNT(*),SUM(CASE WHEN state = 'reg' AND prevstate = 'check' THEN 1 ELSE 0 END)
FROM MyCTE
Fiddler Demo
ORACLE
With T AS
(
select "id", "date", "state", lag("state",1) over(order by "id") as "prevstate"
from Table1
)
SELECT COUNT(*),SUM(CASE WHEN "state" = 'reg' AND "prevstate" = 'check' THEN 1 ELSE 0 END)
FROM T
Fiddler Demo
MySQL
SELECT COUNT(*),SUM(CASE WHEN state = 'reg' AND prevstate = 'check' THEN 1 ELSE 0 END)
FROM
(
SELECT T1.ID,T1.Date,T1.state,T2.state AS prevstate
FROM Table1 T1
LEFT JOIN Table1 T2
ON T1.ID - 1 = T2.ID
) AS T
Fiddler Demo
Actually the third case would work in prety much everything.

How do I fix my SQL statement?

I am using Microsoft SQL Server 2008. I have a table called "Tools" with the following columns [ID], [QtyOnHand], [SizeUS], [PlantID]. A typical small data sample may be something like this:
I would like to group the data by size and have a column with a sum of qty grouped by each plant that would look something like this:
I tried the following query but it's not correct.
SELECT
T.SizeUS
,(
SELECT SUM(T.QtyOnHand)
From Tools T1
WHERE T1.PlantID=1 AND T1.SizeUS=T.SizeUS
) As QtyPlant1
,(
SELECT SUM(T.QtyOnHand)
From Tools T2
WHERE T2.PlantID=2 AND T2.SizeUS=T.SizeUS
) As QtyPlant2
,(
SELECT SUM(T.QtyOnHand)
From Tools T5
WHERE T5.PlantID=5 AND T5.SizeUS=T.SizeUS
) As QtyPlant5
FROM
Tools T
GROUP BY
T.SizeUS;
Try this:
SELECT T.SizeUS,
, sum(case when T1.PlantID = 1 then 1 else 0 end) as QtyPlant1
, sum(case when T1.PlantID = 2 then 1 else 0 end) as QtyPlant2
, sum(case when T1.PlantID = 5 then 1 else 0 end) as QtyPlant5
FROM Tools T
Group By T.SizeUS