Select count group by combined with any [duplicate] - sql

This question already has answers here:
SUM of grouped COUNT in SQL Query
(17 answers)
Closed 12 months ago.
There is this query:
select town, count(town)
from user
group by town
which returns
Town Count
Copenhagen 5
NewYork 6
Athens 7
But I would like an additional line which shows all towns number:
Town Count
Copenhagen 5
NewYork 6
Athens 7
All 18

Probably not the best way, but I believe this should work:
WITH counts_by_town AS (
SELECT town, COUNT(town) AS cnt
FROM user
GROUP BY 1
)
SELECT town, cnt
FROM counts_by_town
UNION ALL
SELECT 'All' AS town, SUM(cnt) AS cnt
FROM counts_by_town;

Related

How do I calculate the percent of the total per year when COUNT and WHERE is used?

In SQL Server, I have a query
SELECT season, COUNT(DISTINCT player_name) AS 'No. of Foreign Players'
FROM nbastats
WHERE country <> 'USA'
GROUP BY season
It return these results
id
season
No. of Foreign Players
1
1996-97
9
2
1997-98
14
3
1998-99
22
4
1999-00
24
5
2000-01
40
6
2001-02
51
7
2002-03
62
What I'm trying to do is to instead get the percentage of foreign players (over total players) each season. The database only provides "country" so I assume I can only use
WHERE country <> 'USA'
and perhaps divide the total but I am unsure how to with WHERE in the way. Any help would be greatly appreciated!
I think you want a ratio of a conditional:
SELECT season,
COUNT(DISTINCT CASE WHEN country <> 'USA' THEN player_name END) * 1.0 / COUNT(DISTINCT player_name) AS foreign_ratio
FROM nbastats
GROUP BY season

SQL query all register but select only one of one column [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
Assume that I have one table looking like this:
ID
ClientID
Name
Country
1
JX100
John
Canada
2
JX100
John
Japan
3
JX690
Rob
EUA
4
PX301
Alice
France
And My query:
SELECT DISTINCT ClientID,Name,Country FROM CLIENTS
OUTPUT:
ClientID
Name
Country
JX100
John
Canada
JX100
John
Japan
JX690
Rob
EUA
PX301
Alice
France
I want to take that:
DESIRED:
ClientID
Name
Country
JX100
John
Canada
JX690
Rob
EUA
PX301
Alice
France
TL;DR
I just want to select one country for all Clients, I don't want to get repeated rows.
You can use row_number()over() with common table expression like below:
with cte as (
SELECT DISTINCT ClientID,Name,Country,row_number()over(partition by ClientID ,Name order by Country) rownumber FROM CLIENTS)
select * from cte where rownumber=1
Row_number()over(partition by ClientID ,Name order by Country) will generate a sequence for contries starting from 1 against each clientid and name. So when you will select rownumber=1 this query will select one country for a single clientid,name combination.

SQL query to get only rows match the condition based on two separated columns under one 'group by'

The simple SELECT query would return the data as below:
Select ID, User, Country, TimeLogged from Data
ID User Country TimeLogged
1 Samantha SCO 10
1 John UK 5
1 Andrew NZL 15
2 John UK 20
3 Mark UK 10
3 Mark UK 20
3 Steven UK 10
3 Andrew NZL 15
3 Sharon IRL 5
4 Andrew NZL 25
4 Michael AUS 5
5 Jessica USA 30
I would like to return a sum of time logged for each user grouped by ID
But for only ID numbers where both of these values Country = UK and User = Andrew are included within their rows.
So the output in the above example would be
ID User Country TimeLogged
1 John UK 5
1 Andrew NZL 15
3 Mark UK 30
3 Steven UK 10
3 Andrew NZL 15
First you need to identify which IDs you're going to be returning
SELECT ID FROM MyTable WHERE Country='UK'
INTERSECT
SELECT ID FROM MyTable WHERE [User]='Andrew';
and based on that, you can then filter to aggregate the expected rows.
SELECT ID,
[User],
Country,
SUM(Timelogged) as Timelogged
FROM mytable
WHERE (Country='UK' OR [User]='Andrew')
AND ID IN( SELECT ID FROM MyTable WHERE Country='UK'
INTERSECT
SELECT ID FROM MyTable WHERE [User]='Andrew')
GROUP BY ID, [User], country;
So, you have described what you need to write almost perfectly but not quite. Your result table indicates that you want Country = UK OR User = Andrew, rather than AND
You need to select and group by, then include a WHERE:-
Select ID, User, Country, SUM(Timelogged) as Timelogged from mytable
WHERE Country='UK' OR User='Andrew'
Group by ID, user, country

Sum(case results not right

I have a contact table that includes the length of time each contact lived in the neighborhood:
ID First_Name Last_Name Neighborhood_Time
1 John Smith 1-2 years
2 Mary Jones 2-5 years
3 Dennis White 2-5 years
4 Martha Olson 5+ years
5 Jeff Black 5+ years
6 Jean Rogers 2-5 years
I want to show the percentage of time, the result would look like this:
One_to_2_Years Two_to_5_Years 5+_Years
16 50 33
This is what I'm using:
select
sum(case when Neighborhoods_time ='1-2 years' then 1 else 0 end)*100/(select count(*) from contact) as One_to_2_Years,
sum(case when Neighborhoods_time ='2-5 years' then 1 else 0 end)*100/(select count(*) from contact) as Two_to_6_Years,
sum(case when Neighborhoods_time ='5+years' then 1 else 0 end)*100/(select count(*) from contact) as Six_to_10_Years
from dbo.contact
This is my result:
One_to_2_Years Two_to_5_Years 5+_Years
0 0 16
16 33 0
0 16 16
I see the numbers under each column are correct, I'm having a problem summing them.
What am I missing?
Thanks.
Add Group by Neighborhoods_time
The basis of your query can be produced like
select
Neighborhood_Time,
100*COUNT(*)/(Select COUNT(*) from contact) as percentvalue
from
contact
group by
Neighborhood_Time
If you want to arrange it horizontally, then you should use a pivot
select
*
from
(
select
Neighborhood_Time,
100*COUNT(*)/(Select COUNT(*) from contact) as percentvalue
from
contact
group by
Neighborhood_Time
) src
PIVOT
( SUM(percentvalue) for Neighborhood_Time in ([1-2 years],[2-5 years],[5+ years])) as pt

SQL Group By Help Required

I have a table named People in the following format:
Date | Name.
When I count the people by Grouping By Name with
Select Date, Name, count(*)
From People
Group By Date, Name;
Will give the following
Date Name count(*)
10 Peter 25
10 John 30
10 Mark 25
11 Peter 15
11 John 10
11 Mark 5
But I would like the following result:
Date Peter John Mark
10 25 30 25
11 15 10 5
Is this possible? This is a simple example of a more complicated database. If someone helps me in solving this problem I will use the concept to implement it in my table
Thanks!
Select Date
, count(case when Name = 'Peter' then 1 else null end)
, count(case when Name = 'John' then 1 else null end)
, count(case when Name = 'Mark' then 1 else null end)
From People
Group By Date;
another option different from turbanoff's if, for some reason, you find yourself in a situation that you cant apply a group by:
Select distinct(P.Date),
(select count(*) from People where date=p.date and name='Peter') as Peter,
(select count(*) from People where date=p.date and name='John') as John,
(select count(*) from People where date=p.date and name='Mark') as Mark
From People P