Creating a SQL View where columns are data from another column - sql

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...

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

Pivot not returning aggregate (SQL Adventureworks)

I am using the Adventureworks sample database (HumanResources.Employee table is the database).
For simplicity I will take a sample of the table:
HireDate Gender SalariedFlag
-----------------------------------------
2014-01-01 M 1
2015-01-30 F 1
2014-01-30 M 1
2014-02-12 F 0
2014-03-11 F 1 and so on
Code:
SELECT
YEAR(hiredate), F, M
FROM
HumanResources.employee
PIVOT
(sum(SalariedFlag)
FOR gender IN ([F], [M])
) AS gg
-- Unable to use sum though since its bit field
Expected output:
Year F M
-------------------
2014 1 2 # count(SalariedFlag)
2015 1 0 # count(SalariedFlag)
But I really get:
No name F M
-------------------------------
2014 0 1
2015 1 0
2014 0 1
2014 1 0
2014 1 0
and so on.
So basically in output its not considering the salariedflag column at all, it is simply returning 1 in F if the person was female and 1 in M if person was male.
What am I doing wrong?
Firstly, counting the value of SalariedFlag isn't going to achieve anything. COUNT counts the number of rows with a non-NULL value, and all your rows have a non-NULL value. Instead you want to COUNT the number of rows where the value of SalariedFlag is 1.
You might, therefore, be able to SUM the column, however, as it's a "flag" it's more likely to be a bit and you can't SUM a bit. Therefore using COUNT and checking the value is 1 with a CASE would likely be better.
Personally, rather than using the restrictive PIVOT operator, I would suggest you use conditional aggregation. This gives you the following:
SELECT YEAR(HireDate) AS HireYear,
COUNT(CASE WHEN Gender = 'M' AND SalariedFlag = 1 THEN 1 END) AS M,
COUNT(CASE WHEN Gender = 'F' AND SalariedFlag = 1 THEN 1 END) AS F
FROM #YourTable
GROUP BY YEAR(HireDate);
db<>fiddle

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

Aggregation two table

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