Need a SQL query to get data based on condition based on count - sql

I have tables called areadetail and state.
areadetail has these columns:
State Id
Area Id-rural(1) or urban(2)
Population
Sample data:
Stateid AreaID Population
1 1 10
1 2 20
1 2 20
2 1 10
2 2 20
3 1 10
State table has these columns:
State name and state id
Sample data:
State Id StateName
1 Delhi
2 Mumbai
3 Jaipur
Now I need a query to display records like
State name Rural Urban
------------------------------------------
Delhi 3 2
Mumbai 1 1
Jaipur 1 0

I guess AreaID=1 means RURAL and 2 is URBAN then try this query:
SELECT
MAX(s.StateName),
SUM(CASE WHEN ad.AreaID=1 THEN ad.Population ELSE 0 END) as Rural ,
SUM(CASE WHEN ad.AreaID=2 THEN ad.Population ELSE 0 END) as Urban
FROM State as s
LEFT JOIN areadetail as ad on s.Stateid=ad.Stateid
GROUP BY s.Stateid
SQLFiddle demo

Select name,Rural,Urban
from
(
Select state, sum(case when area= 1 then 1 else 0 end) 'Rural',sum(case when area= 2 then 1
else 0 end) 'Urban'
from #Area
group by state
) A
join State S
on A.state = S.state

Related

TRIGGER to calculate the total leaves of each left type group by employee

I have 2 tables like this
EmployeeID
LeaveTypeID
1
1
2
2
1
2
1
2
Now I want a TRIGGER that calculate automatically total of type 1 and type 2 in below table
EmployeeID
Type1
Type2
1
1
2
2
0
1
No need for triggers. Just create a view that has the aggregation in it
CREATE VIEW vTotal
AS
SELECT
t.EmployeeID,
Type1 = COUNT(CASE WHEN t.LeaveTypeID = 1 THEN 1 END),
Type2 = COUNT(CASE WHEN t.LeaveTypeID = 2 THEN 1 END)
FROM dbo.YourTable t
GROUP BY
t.EmployeedID;

Group by one column and return several columns on multiple conditions - T-SQL

I have two tables which I can generate with SELECT statements (joining multiple tables) as follows:
Table 1:
ID
Site
type
time
1
Dallas
2
01-01-2021
2
Denver
1
02-01-2021
3
Chicago
1
03-01-2021
4
Chicago
2
29-11-2020
5
Denver
1
28-02-2020
6
Toronto
2
11-05-2019
Table 2:
ID
Site
collected
deposited
1
Denver
NULL
29-01-2021
2
Denver
01-04-2021
29-01-2021
3
Chicago
NULL
19-01-2020
4
Dallas
NULL
29-01-2019
5
Winnipeg
13-02-2021
17-01-2021
6
Toronto
14-02-2020
29-01-2020
I would like the result to be grouped by Site, having on each column the COUNT of type=1 , type=2, deposited and collected, all of the 4 columns between a selected time interval. Example: (interval between 01-06-2020 and 01-06-2021:
Site
type1
type2
deposited
collected
Dallas
0
1
0
0
Denver
1
0
2
1
Chicago
1
1
0
0
Toronto
0
0
0
0
Winnipeg
0
0
1
1
How about union all and aggregation?
select site,
sum(case when type = 1 then 1 else 0 end) as type_1,
sum(case when type = 2 then 1 else 0 end) as type_2,
sum(deposited) as deposited, sum(collected) as collected
from ((select site, type, 0 as deposited, 0 as collected
from table1
) union all
(select site, null,
(case when deposited is not null then 1 else 0 end),
(case when collected is not null then 1 else 0 end)
from table2
)
) t12
group by site;
Combine your tables 1 and 2 with a join on Site
Use COUNT(CASE WHEN type = 1 then 1 END) as type1 and a similar construct for type 2
Use COUNT(CASE WHEN somedate BETWEEN '2020-06-01' and '2021-06-01' then 1 END) as ... for your dates

Display output is columns based on filter criteria

I am trying to display data is columns/subcolumns based on certain filter criteria using case when statement but not getting required output.
data:
ID ID2 Country Type
1 001 US A
1 009 US A
2 002 AU B
3 003 CA A
3 005 CA A
4 007 US B
5 001 FR B
6 003 US B
7 002 US A
8 004 NZ A
based on my current case statement, here is how my output looks:
Type Country Count
B Other 2
B US 1
B Subtotal 3
A Other 4
A US 3
A Subtotal 7
Total 10
I want to display the following format, bonus if I can get the subtotal/totals:
Type-A Type-B
US Other US Other
3 4 1 2
I also need Subtotals, and Grandtotals, but these need to be calculated separately.
SubTotal: 7 SubTotal: 3
Grand Total: 10
You can like this
select
sum(case when Type = 'A' and Country = 'US' then 1 else 0 end) as US_TYPE_A,
sum(case when Type = 'A' and Country != 'US' then 1 else 0 end) as Other_TYPE_A,
sum(case when Type = 'B' and Country = 'US' then 1 else 0 end) as US_TYPE_B,
sum(case when Type = 'B' and Country != 'US' then 1 else 0 end) as Other_TYPE_B
from myTable

Find count group by id in SQL Server

I need some help to solve this query. I have a table which contains the ages of the passengers who are going to stay in a room which is mentioned below:
Age RoomId
----- ---
1 1
12 1
8 1
19 1
3 2
12 2
18 2
21 3
Also, I have properties table which contains the maximum age of the child and maximum age of the infant. Based on the age of the passenger, I need to segregate them to adult, child, and infant to each of the properties.
Properties table structure
Property Id Maximum_child_age Maximum_infant_age
-------------------------------------------------
1 11 2
Desired output
RoomId Adult Child Infant PropertyId
--------------------------------
1 2 1 1 1
2 2 1 0 1
3 1 0 0 1
Use conditional aggregation :
SELECT
SUM(CASE WHEN pas.age > ppt.Maximum_child_age THEN 1 ELSE 0 END) AS Adult,
SUM(CASE WHEN pas.age BETWEEN Maximum_infant_age AND ppt.Maximum_child_age THEN 1 ELSE 0 END) AS Child,
SUM(CASE WHEN pas.age < ppt.Maximum_infant_age THEN 1 ELSE 0 END) AS Infant,
ppt.id
FROM
passengers pas
CROSS JOIN properties ppt
GROUP BY ppt.id
Cross join the properties and then do conditional aggregation.
SELECT count(CASE
WHEN pa.ages > pr.maximum_child_age THEN
1
END) adult,
count(CASE
WHEN pa.ages > pr.maximum_infant_age
AND pa.ages <= pr.maximum_child_age THEN
1
END) child,
count(CASE
WHEN pa.ages <= pr.maximum_infant_age THEN
1
END) infant,
pr.propertyid
FROM passengers pa
CROSS JOIN properties pr
GROUP BY pr.propertyid;

Select sport results ordering by medals

I have a table:
sport country place
ski swe 1
ski nor 2
ski rus 3
luge swe 1
luge usa 2
luge ger 3
bob nor 1
bob rus 2
bob ger 3
where place is 1 for gold, 2 for silver, 3 for bronze
Now the normal displying scenario is a list of countries, first max gold, then silver then bronze. for that exampe it would be:
swe g:2 s:0 b:0 sum:2
rus g:0 s:1 b:1 sum:2
usa g:0 s:1 b:0 sum:1
nor g:0 s:0 b:2 sum:2
what would be SQL query to get list of countries ordering by that way?
regards
select
country,
sum(case when place = 1 then 1 else 0 end) as gold,
sum(case when place = 2 then 1 else 0 end) as silver,
sum(case when place = 3 then 1 else 0 end) as bronce,
count(*) as allmedals
from tab
group by country
For ordering the result you might do
order by sum(4 - place) desc -- weighted medals
using multiple ordering is a key: here is query. Thanks to user:dnoeth
select
country,
sum(case when place = 1 then 1 else 0 end) as gold,
sum(case when place = 2 then 1 else 0 end) as silver,
sum(case when place = 3 then 1 else 0 end) as bronce,
count(*) as allmedals
from tab
group by country ORDER BY gold DESC, silver DESC, bronce DESC