sql Count Statement - sql

I have a table name as JOB_Details and the data show as below:
Age Department:IT
Male Female
30yrs 5 1
30-34yrs 3 2
35-39yrs 4 4
40-49yrs 2 3
50-54yrs 1 0
and the output suppose will like:
Age Department:IT Total
Male Female
30yrs 5 1 6
30-34yrs 3 2 5
35-39yrs 4 4 8
40-49yrs 2 3 5
50-54yrs 1 0 1
Total 15 10 25
How can I do it? Because when using the count and group by function the result outcome will be not exactly what I want. Can someone mind to give me some suggestion? For your information I use Oracle.

SELECT AGE, MALE, FEMALE, SUM(MALE + FEMALE) AS TOTAL
FROM JOB_DETAILS
GROUP BY AGE
Edit:
If you want the running total you will have to use ROLLUP (SQL Server)
SELECT AGE, MALE, FEMALE, SUM(MALE + FEMALE) AS TOTAL
FROM JOB_DETAILS
GROUP BY AGE
WITH ROLLUP

Try to use union all like this:
SELECT age, Male, Female, nvl(Male,0) + nvl(Female,0) as total
FROM JOB_Details
union all
SELECT 'TOTAL', sum(Male), sum(Female), sum(nvl(Male,0) + nvl(Female,0))
FROM JOB_Details
With view:
create view vJOB_Details
SELECT (CASE WHEN age='30' THEN Count(EMPLID)
WHEN age='34' THEN Count(EMPLID)
WHEN age='39' THEN Count(EMPLID)
WHEN age='49' THEN Count(EMPLID)
WHEN age='54' THEN Count(EMPLID)
ELSE NULL
END) TOTAL_COLUMN ,
(CASE WHEN GENDER1='M' THEN Count(EMPLID)
WHEN GENDER1='F' THEN Count(EMPLID)
ELSE NULL
END) TOTAL_GENDER
FROM job_details
GROUP BY age,GENDER
SELECT age, Male, Female, nvl(Male,0) + nvl(Female,0) as total
FROM vJOB_Details
union all
SELECT 'TOTAL', sum(Male), sum(Female), sum(nvl(Male,0) + nvl(Female,0))
FROM vJOB_Details;

Related

Sqlite group by distinct count

I have table of customers operations:
date, client_id, gender
1 1 M
1 1 M
1 2 M
1 2 M
1 3 F
2 1 M
2 1 M
2 1 M
2 2 M
2 2 M
2 3 F
2 3 F
2 4 F
2 5 M
2 5 M
etc
Desired output is:
date, amount of males, (also need amount of females)
1 2 1
2 3 2
I need to group it by date, so i did it, then my goal is to find amount of each gender in each grouped group.
so i tried to do this to count amount of males:
sum(case when gender = 'M' then 1 else NULL end) as 'M%'
but its counted clients id 1 and 2 two times each, but i need to count it distinct.
On example above i expect this to return 2 because 2 male. But it return 4 because distinct construction doesnt work.
I tried this but it doesnt work and count '1' in result:
sum(distinct case when gender = 'M' then 1 else NULL end) as 'M%'
It's easier to count from the distinct rows of the table.
Also, use SQLite's feature to treat boolean expressions as 1 for true and 0 for false so you can sum them instead of using CASE expressions:
SELECT date,
SUM(gender = 'M') [amount of males],
SUM(gender = 'F') [amount of females]
FROM (SELECT DISTINCT date, client_id, gender FROM tablename)
GROUP BY date
See the demo.
You seem to want conditional count(distinct):
select date,
count(distinct case when gender = 'M' then client_id end) as count_m,
count(distinct case when gender = 'F' then client_id end) as count_f
from t
group by date;

How to create a new sql table from existing table with small calculations

I have a SQLite table like this:
id
item
totalcost
sharedppl
paidby
second
third
1
Lunch
150
3
Tom
Jack
Harry
2
Dinner
200
2
Jack
Harry
3
Drinks
75
3
Harry
Jack
Tom
I want a new SQLite table to show share of each person. It needs to do the calculation to split the cost for each item between the people.
item
Tom
Jack
Harry
Lunch
50
50
50
Dinner
0
100
100
Drinks
25
25
25
Please advise what query I need to run on sql to get this new table.
Use a CASE expression that calculates the share for each person:
SELECT item,
CASE WHEN 'Tom' IN (paidby, second, third) THEN 1.0 * totalcost / sharedppl ELSE 0 END AS Tom,
CASE WHEN 'Jack' IN (paidby, second, third) THEN 1.0 * totalcost / sharedppl ELSE 0 END AS Jack,
CASE WHEN 'Harry' IN (paidby, second, third) THEN 1.0 * totalcost / sharedppl ELSE 0 END AS Harry
FROM tablename
See the demo.
One approach, using an unpivot followed by a pivot and aggregation:
WITH cte AS (
SELECT item, totalcost, paidby AS person FROM yourTable
UNION ALL
SELECT item, totalcost, second FROM yourTable
UNION ALL
SELECT item, totalcost, third FROM yourTable
)
SELECT
item,
CASE WHEN COUNT(CASE WHEN person = 'Tom' THEN 1 END) > 0
THEN MAX(totalcost) / COUNT(person) ELSE 0 END AS Tom,
CASE WHEN COUNT(CASE WHEN person = 'Jack' THEN 1 END) > 0
THEN MAX(totalcost) / COUNT(person) ELSE 0 END AS Jack,
CASE WHEN COUNT(CASE WHEN person = 'Harry' THEN 1 END) > 0
THEN MAX(totalcost) / COUNT(person) ELSE 0 END AS Harry
FROM cte
GROUP BY item, id
ORDER BY id;
Side note: Your current table design is not normalized properly. You can and should change it to the output you want here.

Result set should be in single row - SQL

Table Name: Employee
Columns: EmpID, EmpName, Gender
Total count of records present in the employee table is 10
in that 6 records having gender as male and 4 records are having gender as female
i want output like below in single line
Result - > 10,6,4
Please help on the query part
Just use conditional aggregation:
select count(*), sum(case when gender = 'male' then 1 else 0 end),
sum(case when gender = 'female' then 1 else 0 end)
from t;

SQL Pecents by Group

I have query and result below.
select count(age) countage, age
from table2
group by age
and result
countage age
2 1
4 2
4 3
1 4
2 5
8 6
4 7
8 8
1 9
2 10
I want to have 2 age group. First group 1 to 5 and second group 6 to 10
I like to have total group 1 and group 2 devide by each count age to get percents
For example:
Total count for group 1 (age 1 to 5) = 13 and group 2 ( age 6 to 10 ) = 23
age 1 count = 2 and devide 2 /13 * 100 = 15%
age 2 count = 4 and devide 4 /13 * 100 = 30%
and so on for group 1.
Group 2 do the same calculation as group 1
My query but not working well
WITH t1 AS
(SELECT Age, Count(*) AS n
FROM table2
GROUP BY Age)
SELECT Age, n,
(0.0+n)/(COUNT(*) OVER (PARTITION BY Age))
FROM t1;
Need help. Thank you
If I understand correctly, you want:
select (case when age <= 5 then 'group1' else 'group2' end) as age_group,
count(*),
count(*) * 1.0 / sum(count(*)) over () as ratio_in_group
from table2
group by (case when age <= 5 then 'group1' else 'group2' end);
Or, if you just want this by age, you can do something similar with conditional window functions:
select age, count(*) as cnt,
count(*) * 1.0 / sum(count(*)) over (partition by case when age < 5 then 'group1' else 'group2' end) as ratio_in_group
from table2
group by age;

How to aggregate functions with groupby in SQL

I have tables like this.
I would like to groupbyand aggregate this in diffrent aggregate functions.
product sex age
A M 10
B F 20
A F 30
C M 40
my desired result is like below.
Now I can group in productkey, but in this case, I must group them byproductandsex.
Are there any way to achieve it?
count(M) count(F) avarage(M) average(F)
A 1 1 10 30
B 0 1 NA 20
C 1 0 40 NA
Thanks
With conditional aggregation:
select product,
sum(case when sex = 'M' then 1 else 0 end),
sum(case when sex = 'F' then 1 else 0 end),
avg(case when sex = 'M' then age end),
avg(case when sex = 'F' then age end)
from tablename
group by product
You can simply use the PIVOT as follows:
select * from your_Table
pivot
(count(1) as cnt, avg(age) as average for sex in ('M','F'))