SQL - Grouping COUNT Results - sql

The query I'm trying to answer is 'How many sales above or equal to 60 has each person made?'
My table (sales$):
SaleID name salevalue
1 Steve 100
2 John 50
3 Ellen 25
4 Steve 100
5 Mary 60
6 Mary 80
7 John 70
8 Mary 55
9 Steve 65
10 Ellen 120
11 Ellen 30
12 Ellen 40
13 John 40
14 Mary 60
15 Steve 50
My code is:
select name,
COUNT(*) as 'sales above 60'
from Sales$
group by salevalue, name
having salevalue >= 60;
Which gives:
Ellen 1
John 1
Mary 2
Mary 1
Steve 1
Steve 2
The information is correct in that Mary & Steve both have 3 sales, however I'm forced by the HAVING command to group them out.
Any ideas? I'm sure I've just taken a wrong turning.

You can use conditional aggregation for this:
select name,
COUNT(case when salevalue >= 60 then 1 end) as 'sales above 60'
from Sales$
group by name
This way COUNT will take into consideration only records having salevalue >= 60.

I've swapped the HAVING statement for a WHERE and achieved the desired result:
select name, count(*) 'sales above 50'
from sales$
where salevalue >=60
group by name
(Lightbulb moment after posting)

Related

autoincrement number function-postgres

i have a table like this:
id
person
20
adams
20
george
40
jina
46
rico
80
naya
90
john
90
peter
90
richard
i want to find a way to select a new_id starting from 1 and increazing +1 every time id is different. for example i want a select with a result like this:
new_id
id
person
1
20
adams
1
20
george
2
40
jina
3
46
rico
4
80
naya
5
90
john
5
90
peter
5
90
richard
is there any function in postgres doing something like that?
use dense_rank()
select dense_rank()over(order by id) as newid,id,persion
from table_name
demo link

Using a top x count query as a where clause to show all qualifying records

I have a count of a top 2
My table has this data
Name Age price visited size
Jon 34 53 2018-01-01 9
Don 22 70 2018-03-01 15
Pete 76 12 2018-11-09 7
Jon 34 55 2018-09-13 9
Paul 90 64 2018-07-08 6
Pete 76 31 2018-03-25 7
Jon 75 34 2018-06-06 8
select top 2
name,
count(name) as cnt
from
tbl1
group by name
order by cnt desc
Which returns my top 2 names
Jon 3
Pete 2
This name will change dynamically as the query is run depending on who has made the most visits in total (this is very simplified the actual table has 1000's of entries).
What I would like to do is then use the result of that query to get the following all of which needs to be in a single query;
Name Age price visited size
Jon 34 53 2018-01-01 9
Jon 34 55 2018-09-13 9
Jon 75 34 2018-06-06 8
Pete 76 12 2018-11-09 7
Pete 76 31 2018-03-25 7
In summary, count who has visited the most and then display all the records under those names.
Thanks in advance
Here's one option using in:
select *
from yourtable
where name in (
select top 2 name
from yourtable
group by name
order by count(*) desc
)
order by name
Online Demo

SQL query to update a column via phpmyadmin

I have a large 'scores' table that resembles the following:
quizid userid score high_score
1 john 50 0
1 bob 60 0
1 bob 65 0
1 steve 40 0
2 bob 20 0
2 bob 30 0
2 bob 15 0
current the 'high_score' column is '0' as it was just added. what I need to do is make a simple query to flag this column with a '1' for every instance where a user's score for each quiz is the highest one that user has for that quiz - i.e. after running the query I should have this:
quizid userid score high_score
1 john 50 1
1 bob 60 0
1 bob 65 1
1 steve 40 1
2 bob 20 0
2 bob 30 1
2 bob 15 0
Any help would be appreciated!
i'm not sure and i have not tested below code but try it maybe it help you
update scores set high_score=1 where (quizid,userid,score) in
(select quizid,userid,max(score) from scores group by quizid,userid)

group by results sets in one row ms access query

I need help with ms access query. My current group by is adding additional rows with my result sets.
currently it is this:
schoolsName organization city agent total organization_award city_award agent_award
John Boscoe 0 0 2 2 10000
John Boscoe 0 26 0 26 1000
John Boscoe 0 2 2 2 4000 100000
John Boscoe 18 0 0 18 10000
John Boscoe 3 3 0 3 5000 10000
my current sql query for ms access is:
SELECT
schools.schoolsName, Count(schools.[organization]) AS organization,
Count(schools.[city]) AS city, Count(schools.[agent]) AS agent,
Count(schools.schoolsName) AS total,
IIf((schools.[organization]) Like 'yes',Sum(schools.[dollaramount]),'') AS organization_award,
IIf((schools.[city]) Like 'yes',Sum(schools.[dollaramount]),'') AS city_award,
IIf((schools.[agent]) Like 'yes',Sum(schools.[dollaramount]),'') AS agent_award
FROM schools
GROUP BY schools.schoolsName, schools.[organization], schools.[city], schools.[agent];
how do i change the above query to get this result set:
schoolsName organization city agent total organization_award city_award agent_award
John Boscoe 21 31 4 51 15000 15000 110000
not sure about the count part, but did you try this?
SELECT schools.schoolsName,
Count(schools.[organization]) AS organization,
Count(schools.[city]) AS city,
Count(schools.[agent]) AS agent,
Count(schools.schoolsName) AS total,
Sum(IIf((schools.[organization]) Like 'yes', schools.[dollaramount],0)) AS organization_award,
Sum(IIf((schools.[city]) Like 'yes', schools.[dollaramount],0)) AS city_award,
Sum(IIf((schools.[agent]) Like 'yes', schools.[dollaramount],0)) AS agent_award
FROM schools
GROUP BY schools.schoolsName;

Retrieve top 48 unique records from database based on a sorted Field

I have database table that I am after some SQL for (Which is defeating me so far!)
Imagine there are 192 Athletic Clubs who all take part in 12 Track Meets per season.
So that is 2304 individual performances per season (for example in the 100Metres)
I would like to find the top 48 (unique) individual performances from the table, these 48 athletes are then going to take part in the end of season World Championships.
So imagine the 2 fastest times are both set by "John Smith", but he can only be entered once in the world champs. So i would then look for the next fastest time not set by "John Smith"... so on and so until I have 48 unique athletes..
hope that makes sense.
thanks in advance if anyone can help
PS
I did have a nice screen shot created that would explain it much better. but as a newish user i cannot post images.
I'll try a copy and paste version instead...
ID AthleteName AthleteID Time
1 Josh Lewis 3 11.99
2 Joe Dundee 4 11.31
3 Mark Danes 5 13.44
4 Josh Lewis 3 13.12
5 John Smith 1 11.12
6 John Smith 1 12.18
7 John Smith 1 11.22
8 Adam Bennett 6 11.33
9 Ronny Bower 7 12.88
10 John Smith 1 13.49
11 Adam Bennett 6 12.55
12 Mark Danes 5 12.12
13 Carl Tompkins 2 13.11
14 Joe Dundee 4 11.28
15 Ronny Bower 7 12.14
16 Carl Tompkin 2 11.88
17 Nigel Downs 8 14.14
18 Nigel Downs 8 12.19
Top 4 unique individual performances
1 John Smith 1 11.12
3 Joe Dundee 4 11.28
5 Adam Bennett 6 11.33
6 Carl Tompkins 2 11.88
Basically something like this:
select top 48 *
from (
select athleteId,min(time) as bestTime
from theRaces
where raceId = '123' -- e.g., 123=100 meters
group by athleteId
) x
order by bestTime
try this --
select x.ID, x.AthleteName , x.AthleteID , x.Time
(
select rownum tr_count,v.AthleteID AthleteID, v.AthleteName AthleteName, v.Time Time,v.id id
from
(
select
tr1.AthleteName AthleteName, tr1.Time time,min(tr1.id) id, tr1.AthleteID AthleteID
from theRaces tr1
where time =
(select min(time) from theRaces tr2 where tr2.athleteId = tr1.athleteId)
group by tr1.AthleteName, tr1.AthleteID, tr1.Time
having tr1.Time = ( select min(tr2.time) from theRaces tr2 where tr1.AthleteID =tr2.AthleteID)
order by tr1.time
) v
) x
where x.tr_count < 48