Aggregation two table - sql

I have two tables
nameTB contains:
id | name | gender | nationalityid
nationalityTB contains:
nationalityid | nationalityname
I want create sql query to return the result:
nationalityname female male total
UK 10 5 15
USA 11 7 18

You should be able to easily get the result by joining the tables and using an aggregate function with conditional logic (like a CASE expression):
select nt.nationalityname,
sum(case when nm.gender = 'Female' then 1 else 0 end) Female,
sum(case when nm.gender = 'Male' then 1 else 0 end) Male,
count(*) Total
from nationalityTB nt
left join nameTB nm
on nt.nationalityid = nm.nationalityid
group by nt.nationalityname;
See SQL Fiddle with Demo

Related

SQL Group by on multiple conditions on same table

enter image description hereI am trying to write an SQL query to get below output.
table has below data:
ID GENDER
10 M
10 F
10 F
20 F
20 M
Output:
ID Male Female
10 1 2
20 1 1
do i need to use case with group by. Can someone help here.
use simple group
select id,sum(case when GENDER = 'M' then 1 else 0 end) as Male,
sum(case when GENDER = 'F' then 1 else 0 end) as FeMale
from tablename
group by id

SQL Aggregation prior to advanced SQL

Given a simple case like this:
Gender DeptID
--------------
M 1
F 1
M 2
F 2
F 2
What SQL statement I need to write If I want to generate the following result using SQL without using advanced CUBE, Rollup, etc., just using plain SQL-92:
GenderSum Dept1Sum Dept2Sum
----------------------------
M 1 1
F 1 2
I was wondering how such information would be generated by ETL in the past using SQL?
Remark: It is possible to use Group by on gender and union that with a group by on DetptId to get a vertical result set but this is clearly not what I want.
You can try the following pivot query:
SELECT Gender AS GenderSum,
SUM(CASE WHEN DeptID = 1 THEN 1 ELSE 0 END) AS Dept1Sum,
SUM(CASE WHEN DeptID = 2 THEN 1 ELSE 0 END) AS Dept2Sum
FROM yourTable
GROUP BY Gender

SQL find total count of each type in a column

I'm learning SQL and am stumped on what should be a simple query. I have a table with the following pattern:
Id | Type
------------
1 | Red
2 | Blue
3 | Blue
4 | Red
..
I would like to write a query to return a table that counts the total number of instances of each type and returns a table with the following pattern, for example, if 'Blue' occurs in 12 rows, and 'Red' occurs in 16 rows in the table above, the result would be:
Blue | Red
-----------
12 | 16
You could do it this way:
SELECT Type, COUNT(*) FROM TABLE GROUP BY Type
If you'd like to see the Types in separate columns, you could do this:
SELECT SUM(CASE WHEN Type = 'Blue' THEN 1 ELSE 0 END) AS Blue, SUM(CASE WHEN Type = 'Red' THEN 1 ELSE 0 END) AS Red FROM TABLE
I suggest using count over partition by. Here's a code I wrote to help my company check for duplicate Technician EmployeeID's and Pincodes, including count and YES/NO columns to allow filtering in excel so they can see what corrections need to be made:
select
t.TechnicianId, t.TechnicianName, t.Pincode, t.EmployeeID
, [Pincode Count] = count(t.Pincode) over (partition by t.Pincode)
, [Duplicate Pincode?] = case count(t.Pincode) over (partition by t.Pincode) when 1 then 'NO' else 'YES' end
, [EmployeeID Count] = count(t.EmployeeID) over (partition by t.EmployeeID)
, [Duplicate EmployeeID?] = case count(t.EmployeeID) over (partition by t.EmployeeID) when 1 then 'NO' else 'YES' end
from Technicians t
group by t.TechnicianId, t.TechnicianName, t.Pincode, t.EmployeeID
order by 4

Creating a SQL View where columns are data from another column

I am developping a simple criminal records management system using SQL Server.
I have the following tables:
Criminal (
CriminalID,
Gender ...)
Victim (
VictimID,
Gender ...)
Crime(
CrimeID,
CriminalID,
VictimID ...)
I am interested in creating a view that gives us a simple statistic about Gender of victims by gender of criminals.
Example of wanted output:
Victim \ Criminal | Male | Female
Male | 4 | 2
Female | 8 | 5
Grouping the Number of crimes only by gender of victims or only by gender of criminals isnt hard , But how can i perform this task ? How to Expand the Gender of criminals into 2 separate columns and perform the needed task ?
You need a conditional sum:
select criminal.gender
,SUM(case when victim.gender = 'M' then 1 else 0 end) as "Male"
,SUM(case when victim.gender = 'F' then 1 else 0 end) as "Female"
from ...
group by criminal.gender
The CASE returns either 1 or 0...

Sql Result two columns

I have the following table:
Full name status
ricardo 1 2
ricardo 2 4
How do I make a select to return like this:
name totalstatus1 totalstatus2 total
ricardo 2 4 6
You did not include the name of the column with the 2 and 4 but you could use something similar to this:
select name,
sum(case when status = 1 then value end) totalStatus1,
sum(case when status = 2 then value end) totalStatus2,
sum(value) Total
from yourtable
group by name;
See SQL Fiddle with Demo