My SQL Query Print result like
North South West East Central
0 280 0 41 36
But I want it Like
North 0
South 280
West 0
East 41
Central 36
SQL:-
Select Count(Case When Region=1 Then 1 Else Null End)[North],
Count(Case When Region=2 Then 1 Else Null End)[South],
Count(Case When Region=3 Then 1 Else Null End)[West],
Count(Case When Region=4 Then 1 Else Null End)[East],
Count(Case When Region=5 Then 1 Else Null End)[Central]
From ATM Where ATMStatus=0 And Bank=1
USE Group by
SELECT
CASE Region
WHEN 1 THEN 'North'
WHEN 2 THEN 'South'
WHEN 3 THEN 'West'
WHEN 4 THEN 'East'
WHEN 5 THEN 'Central' END AS Region
, COUNT(ID) --or your primary key if it is different
FROM ATM
WHERE ATMStatus = 0 AND Bank = 1
GROUP BY Region
select case Region
when 1 then 'North'
when 2 then 'South'
etc
end, count(*)
From ATM Where ATMStatus=0 And Bank=1
group by Region
Do you have a region table? That would make it simpler, especially if the 0 rows are important.
Related
Here's my sample input of match results from a tournament and the result column indicates whether team 1 or team 2 won the game (0 stands for a draw).
score table -
-----------------
t1 t2 result
-----------------
us aus 1
aus sa 2
sa us 0
us nz 1
Desired output -
---------------------------------
team played won lost draw
---------------------------------
aus 2 0 2 0
us 3 2 0 1
nz 1 0 1 0
sa 2 1 0 1
Here's my solution that I came up with, but Is it possible to solve it without using a UNION operator?
select
t1 as team,
count(1) as played,
sum(case when result=1 then 1 else 0 end) as won,
sum(case when result=2 then 1 else 0 end) as lost,
sum(case when result=0 then 1 else 0 end) as draw
from
(select
t1,
t2,
result
from score
union
select
t2,
t1,
case when result=1 then 2
when result=2 then 1
else 0 end as result
from score
) t
group by t.t1
You could UNPIVOT your data via a CROSS APPLY and then perform the aggregation on the results
Example
Select Team
,played = count(1)
,won = sum(case when Val=1 then 1 else 0 end)
,lost = sum(case when Val=2 then 1 else 0 end)
,draw = sum(case when Val=0 then 1 else 0 end)
From score A
Cross Apply ( values (T1,Result )
,(T2,case when result=1 then 2 when result=2 then 1 else 0 end)
) B(Team,Val)
Group By Team
Results
team played won lost draw
aus 2 0 2 0
nz 1 0 1 0
sa 2 1 0 1
us 3 2 0 1
I have the following Data:
Vehicle ID Region District City Revenue
X50 Southwest FLD Miami 50
X50 Northeast NYC Rochester 70
X70 Southwest FLD Miami 20
I want to pivot the Region, District and City with the Revenue column.
Output:
Vehicle ID Southwest_R Northeast_R FLD_R NYC_R Miami_R Rochester_R
X50 50 0 50 0 50 0
X50 0 75 0 75 0 75
X70 20 0 25 0 25 0
The problem I am facing is to add those pivots in a single query.
Select * from table
Pivot( SUM(Revenue) for Region IN ('Southwest' Southwest_R, 'Northeast' Northeast_R))
in this query if I add District and City, it throws error.
Hmmm . . . This looks like conditional logic not aggregation:
select vehicle_id,
(case when region = 'Southwest' then revenue else 0 end) as southwest_r,
(case when region = 'Northeast' then revenue else 0 end) as northeast_r,
(case when district = 'District' then revenue else 0 end) as fld_r,
(case when district = 'NYC' then revenue else 0 end) as nyc_r,
. . .
from t;
If you can have multiple rows per vehicle, then you can use aggregation:
select vehicle_id,
sum(case when region = 'Southwest' then revenue else 0 end) as southwest_r,
sum(case when region = 'Northeast' then revenue else 0 end) as northeast_r,
sum(case when district = 'District' then revenue else 0 end) as fld_r,
sum(case when district = 'NYC' then revenue else 0 end) as nyc_r,
. . .
from t
group by vehicle_id;
Suppose I have a SQL table titled Users with the following example data:
Description Days
--------------------------
Healthy 10
High-blood pressure 20
Cholesterol 23
Diabetes 31
High-blood pressure 8
Healthy 12
Diabetes 18
Cholesterol 25
High-blood pressure 20
Healthy 6
How would I produce a result that looks like the following where the columns: Less_than_20_days, 20_days and Greater_than_20_days contains counts from the table above
Description Less_than_20_days 20_days Greater_than_20_days
-----------------------------------------------------------------------------------
Healthy 3 0 0
High-blood pressure 1 2 0
Cholesterol 0 0 2
Diabetes 1 0 1
I'm trying to get this to work in SQL Server and have tried using the union operator, temp tables and ctes but I can't seem to get the desired result.
Any help would be much appreciated!
You can combine case with sum():
select
[Description]
,sum(case when [Days] < 20 then 1 else 0 end) as Less_than_20_days
,sum(case when [Days] = 20 then 1 else 0 end) as d20_days
,sum(case when [Days] > 20 then 1 else 0 end) as Greater_than_20_days
from users
group by [Description]
Use conditional aggregation:
select description,
sum(case when days < 20 then 1 else 0 end) as num_lt_20,
sum(case when days = 20 then 1 else 0 end) as num_eq_20,
sum(case when days > 20 then 1 else 0 end) as num_gt_20
from t
group by description
I have one table like
SQL> select * from CRICKET_DETAILS;
TEAM1 TEAM2 WINNER
-------------------- -------------------- --------------------
INDIA PAKISTAN INDIA
INDIA SRILANKA INDIA
SRILANKA INDIA INDIA
PAKISTAN SRILANKA SRILANKA
PAKISTAN ENGLAND PAKISTAN
SRILANKA ENGLAND SRILANKA
6 rows selected.
I want output like this:
TEAM PLAYED WON LOST
ENGLAND 2 0 2
INDIA 3 3 0
PAKISTAN 3 1 2
SRILANKA 4 2 2
I would go with group by and union all:
select team, count(*), sum(won), sum(lost)
from ((select team1 as team,
(case when winner = team1 then 1 else 0 end) as won,
(case when winner = team1 then 0 else 1 end) as lost
from cricket_details cd
) union all
(select team2,
(case when winner = team2 then 1 else 0 end) as won,
(case when winner = team2 then 0 else 1 end) as lost
from cricket_details cd
)
) tt
group by team;
select team, count(*) as played,
count(case when result = 'W' then 1 end) as won,
count(case when result = 'L' then 1 end) as lost
from ( select team3 as team, 'W' as result from cricket_details union all
select case team3 when team1 then team2 else team1 end, 'L' from cricket_details)
group by team;
I have this table
SECTION
Which consist of field called
Semester
2
1
1
1
2
1
1
2
2
2
1
2
I need sql to count how many 1's and how many 2's are there
And out put like this
semester 1 | semester 2
6 | 6
Demo here:After testing
select
sum(Case when semester=1 then 1 else 0 end) as 'Semester1',
sum(Case when semester=2 then 1 else 0 end) as 'Semester2'
from section
Try out this:
Select sum(case when semster =1 then 1 else 0 end) as semster1 ,
sum(case when semster =2 then 1 else 0 end) as semster2
from section;
Assuming there are more values than 1 and 2, and that the values are integers:
SELECT semester, count(*)
FROM section
WHERE semester = 1 OR semester = 2
GROUP BY semester