Combine results into one line - sql

How do I make these results appear on one line, in SQL?

You can use a GROUP BY to do this:
SELECT school_id, school_name, grade, SUM(CASE WHEN Gender='F' THEN 1 ELSE 0 END) as female, SUM(CASE WHEN Gender='M' THEN 1 ELSE 0 END) as male
FROM yourtable
GROUP BY school_id, school_name, grade;

Related

How to find number of nulls in a table SQL

Let's say that I have an employee table. And it has columns like name, salary, and age. If I want to check if there any nulls in the name. I gotta write
SELECT name FROM EMPLOYEE
WHERE name IS NULL;
But what should I do to see the number of nulls each column has?
Probably the simplest method is:
select count(*) - count(name) as num_name_nulls,
count(*) - count(col1) as num_col1_nulls,
. . .
from employee;
However, what I do in this situation is just select the counts:
select count(*), count(name), count(col1), . . .
from employee;
Then I eyeball the result to see if the counts are not equal.
I assume you want to see nulls for a column, you can apply conditional aggregation.
SELECT COUNT(CASE WHEN name is null then 1 end) AS Name_NullCount,
COUNT(CASE WHEN salary is null then 1 end) as salary_nullCount,
COUNT(CASE WHEN age is null then 1 end) as age_nullcount
FROM EMPLOYEE;
You can use below query but it depends on number of columns. It will give number of nulls each row has.
select
((case when column1 is null then 1 else 0 end)
+ (case when column2 is null then 1 else 0 end)
----------------------------------------
----------------------------------------
+(case when columnN is null then 1 else 0 end))
from TableName
For your case
select
((case when name is null then 1 else 0 end)
+(case when salary is null then 1 else 0 end)
+(case when age is null then 1 else 0 end))
from employee
you can find Null values per row from following query
SELECT
(
(CASE WHEN name IS NULL THEN 1 ELSE 0 END)+
...
...
+(CASE WHEN colName IS NULL THEN 1 ELSE 0 END)
) AS sum_of_nulls
FROM EMPLOYEE
WHERE EmpId = 49
where colName is your Db Column Name
select
sum(case
when name is null then 1
when name is not null tehn 0
end) cname,
sum(case
when salary is null then 1
when salary is not null then 0
end) csalary,
...
from employee
limit 1;

How to have condition inside count SQL?

I already tried this code:
SELECT Count(Gender) As MaleCount, Count(Gender) As FemaleCount
FROM [Session4].[dbo].[Survey]
Where Gender = 'M' or Gender = 'F'
I can't get the accurate data when counting with two different conditions in one query.
Pictures below:
This is the result.
This is the original data
SELECT TOP (1000) [Departure]
,[Arrival]
,[Age]
,[Gender]
,[CabinType]
,[Q1]
,[Q2]
,[Q3]
,[Q4]
FROM [Session4].[DBO].[Survey]
count explain :
COUNT(*) counts all rows
COUNT(column) counts non-null value
COUNT(distinct column) counts distinct non-null value
COUNT(1) is the same as COUNT(*)
Use case/when + sum :
SELECT
sum(case when Gender = 'M' then 1 else 0 end ) As MaleCount,
sum(case when Gender = 'F' then 1 else 0 end ) As FemaleCount
FROM [Session4].[dbo].[Survey]
will produce somethings like this :
MaleCount | FemaleCount
1000 | 1255
Another way is using simple goup by
SELECT
Gender,
Count(*)
FROM [Session4].[dbo].[Survey]
GROUP BY
Gender
will produce :
Gender | Count
M | 1000
F | 1255
Try this out:
SELECT sum(case when Gender = 'M' then 1 else 0 end) As MaleCount,
sum(case when Gender = 'F' then 1 else 0 end) As FemaleCount
FROM [Session4].[dbo].[Survey]
Let me know in case of any doubts.

get the closest

How do I get output in the below format using sql?
I am able to do it with case sum, but I am not able to get unique user count in the same line.
id unique_users male_count female_count
101 3 1 2
201 1 0 1
.
.
select id,
count(distinct user) as unique_users,
sum(case when gender = 'M' then 1 else 0 end) as male_count,
sum(case when gender = 'F' then 1 else 0 end) as female_count
from your_table
group by id
Alternative counts distinct males and females.
SELECT
ID,
COUNT(DISTINCT "USER") AS unique_users,
COUNT(DISTINCT DECODE(gender,'M',"USER")) AS male_count,
COUNT(DISTINCT DECODE(gender,'F',"USER")) AS female_count
FROM your_table
GROUP BY ID
ORDER BY ID;
And the same but without the Oracle specific DECODE function.
SELECT
ID,
COUNT(DISTINCT "USER") AS unique_users,
COUNT(DISTINCT CASE gender WHEN 'M' THEN "USER" ELSE NULL END) AS male_count,
COUNT(DISTINCT CASE gender WHEN 'F' THEN "USER" ELSE NULL END) AS female_count
FROM your_table
WHERE ID = 101
GROUP BY ID
ORDER BY ID;
I've quoted "USER" as it has a specific meaning in Oracle.

How to get summary totals on unique IDs using analytics?

My repeating table has duplicate ids but I want summary statistics on unique ids.
Detail_id code book tree
----------- ------ ------ ------
1 BR54 COOK PINE
1 BR55 COOK PINE
1 BR51 COOK PINE
2 BR55 COOK MAPL
2 BR60 COOK MAPL
3 BR61 FORD PINE
3 BR54 FORD PINE
3 BR55 FORD PINE
Here's my query which is also on SQLFiddle
select count(case detail_book when 'COOK' THEN 1 else 0 end) as cook_total,
count(case detail_book when 'FORD' THEN 1 else 0 end) as ford_total,
count(case detail_tree when 'PINE' THEN 1 else 0 end) as pine_total,
count(case detail_book when 'MAPL' THEN 1 else 0 end) as mapl_total
from detail_records;
Desired results:
COOK_TOTAL FORD_TOTAL PINE_TOTAL MAPL_TOTL
---------- ---------- ---------- ----------
2 1 2 1
You could use analytic functions and an inline view to avoid the duplicate counting issue:
select sum(case when detail_book = 'COOK' and book_cntr = 1 then 1 else 0 end) as cook_total,
sum(case when detail_book = 'FORD' and book_cntr = 1 then 1 else 0 end) as ford_total,
sum(case when detail_tree = 'PINE' and tree_cntr = 1 then 1 else 0 end) as pine_total,
sum(case when detail_tree = 'MAPL' and tree_cntr = 1 then 1 else 0 end) as mapl_total
from (select d.*,
row_number() over(partition by detail_book, detail_id order by detail_book, detail_id) as book_cntr,
row_number() over(partition by detail_tree, detail_id order by detail_tree, detail_id) as tree_cntr
from detail_records d) v
Fiddle: http://sqlfiddle.com/#!4/889a8/31/0
I don't think you need analytic functions here:
SELECT COUNT(DISTINCT CASE WHEN detail_book = 'COOK' THEN detail_id END) AS cook_total
, COUNT(DISTINCT CASE WHEN detail_book = 'FORD' THEN detail_id END) AS ford_total
, COUNT(DISTINCT CASE WHEN detail_tree = 'PINE' THEN detail_id END) AS pine_total
, COUNT(DISTINCT CASE WHEN detail_tree = 'MAPL' THEN detail_id END) AS mapl_total
FROM detail_records;
The CASE statement returns NULL when the values don't match; those aren't counted.
Updated SQL Fiddle here. By the way, in your query you were trying to match detail_book to MAPL when I think you wanted to match detail_tree, and my query reflects that.
This answer is based on your example http://sqlfiddle.com/#!4/889a8/29 which you can use to avoid the duplicate ids by getting the distinct ids for detail_book and detail_tree
Kindly check the result here http://sqlfiddle.com/#!4/889a8/44
select sum(case detail_book
when 'COOK' THEN 1
else 0
end) as cook_total,
sum(case detail_book
when 'FORD' THEN 1
else 0
end) as ford_total,
sum(case detail_tree
when 'PINE' THEN 1
else 0
end) as pine_total,
sum(case detail_tree
when 'MAPL' THEN 1
else 0
end) as mapl_total
from
(select distinct detail_id,detail_book,detail_tree
from
detail_records);
You can modify your query to get what you want just by removing the else clause:
select count(case detail_book when 'COOK' THEN 1 end) as cook_total,
count(case detail_book when 'FORD' THEN 1 end) as ford_total,
count(case detail_tree when 'PINE' THEN 1 end) as pine_total,
count(case detail_book when 'MAPL' THEN 1 end) as mapl_total
from detail_records;
The default for case without an else is NULL, so the count() works. Personally, I prefer sum() for this type of aggregation:
select sum(case when detail_book = 'COOK' THEN 1 else 0 end) as cook_total,
sum(case when detail_book = 'FORD' THEN 1 else 0 end) as ford_total,
sum(case when detail_tree = 'PINE' THEN 1 else 0 end) as pine_total,
sum(case when detail_book = 'MAPL' THEN 1 else 0 end) as mapl_total
from detail_records;
Apart from analytical functions, I'd probably use an approach by first "flattening the table" (union all) and then pivoting the result:
select *
from (
select detail_book i
from detail_records
group by detail_id, detail_book
union all
select detail_tree
from detail_records
group by detail_id, detail_tree
)
pivot(count(i) for i in ('COOK', 'FORD', 'PINE', 'MAPL'))
;
sql fiddle
select *
from (
select decode(detail_book,'FORD','FORD_TOTAL','COOK','COOK_TOTAL','MAPL','MAPL_TOTAL','PINE','PINE_TOTAL','OTHER') i,
count(distinct detail_id) j
from detail_records
group by detail_book
union all
select DECODE(detail_tree,'FORD','FORD_TOTAL','COOK','COOK_TOTAL','MAPL','MAPL_TOTAL','PINE','PINE_TOTAL','OTHER') i,
count(distinct detail_id) j
from detail_records
group by detail_tree
)
pivot(sum(j) for i in ('COOK_TOTAL', 'FORD_TOTAL', 'PINE_TOTAL', 'MAPL_TOTAL','OTHER'))
;

count(*) based on the gender condition

I have the following table in oracle10g.
state gender avg_sal status
NC M 5200 Single
OH F 3800 Married
AR M 8800 Married
AR F 6200 Single
TN M 4200 Single
NC F 4500 Single
I am trying to form the following report based on some condition. The report should look like the one below. I tried the below query but count(*) is not working as expected
state gender no.of males no.of females avg_sal_men avg_sal_women
NC M 10 0 5200 0
OH F 0 5 0 3800
AR M 16 0 8800 0
AR F 0 12 0 6200
TN M 22 0 4200 0
NC F 0 8 0 4500
I tried the following query but I am not able to count based onthe no.of males and no.of females..
select State, "NO_OF MALES", "$AVG_sal", "NO_OF_FEMALES", "$AVG_SAL_FEMALE"
from(
select State,
to_char(SUM((CASE WHEN gender = 'M' THEN average_price ELSE 0 END)),'$999,999,999') as "$Avg_sal_men,
to_char(SUM((CASE WHEN gender = 'F' THEN average_price ELSE 0 END)), '$999,999,999') as "$Avg_sal_women,
(select count (*) from table where gender='M')"NO_OF MALES",
(select count (*) from table where gender='F')"NO_OF_FEMALES"
from table group by State order by state);
You can use case as an expression (which you already know...). And the subquery is unnecessary.
select State
, sum(case gender when 'M' then 1 else 0 end) as "no.of males"
, sum(case gender when 'F' then 1 else 0 end) as "no.of females"
, to_char(
SUM(
(
CASE
WHEN gender = 'M' THEN average_price
ELSE 0
END
)
)
, '$999,999,999'
) as "Avg_sal_men",
to_char(SUM((CASE WHEN gender = 'F' THEN average_price ELSE 0 END))
,'$999,999,999'
) as "Avg_sal_women"
from table
group by State;
You are Conting by this sub-query select count (*) from table where gender='M' which always count the total number of male in your whole table....and you are doing same for counting female...
So you Can Try like this...
select State, "NO_OF MALES", "$AVG_sal", "NO_OF_FEMALES", "$AVG_SAL_FEMALE"
from(
select State,
to_char(SUM((CASE WHEN gender = 'M' THEN average_price ELSE 0 END)),'$999,999,999') as "$Avg_sal_men",
to_char(SUM((CASE WHEN gender = 'F' THEN average_price ELSE 0 END)), '$999,999,999') as "$Avg_sal_women,
Sum(Case when gender='M' then 1 else 0 end) "NO_OF MALES",
Sum(Case when gender='F' then 1 else 0 end) "NO_OF_FEMALES"
from table group by State order by state);
Try the following.
select state
,sum(case when gender = 'M' then 1 else 0 end) as nof_males
,sum(case when gender = 'F' then 1 else 0 end) as nof_females
,avg(case when gender = 'M' then average_price end) as avg_sal_male
,avg(case when gender = 'F' then average_price end) as avg_sal_female
from table
group
by state;
..add formatting as required.