How to get Max from Count in SQL (Access)? - sql

There is a table called "Athletes" which has columns for "Group" and "Award". It is necessary to calculate using Count the number of awards in groups separately and display the group with the maximum number of awards and the number of awards for this group. I tried to make a request of this type:
SELECT Max (Reward1) AS Reward
FROM (
SELECT Count (Athletes.Reward) AS Reward1
FROM Athletes
GROUP BY Athletes.Group
) AS [% $ ## # _ Alias];
This works, but the column corresponding to the maximum number of awards is not displayed for the group that received these awards. Can you please tell me what is worth adding so that this column is also displayed?

You're not selecting the GROUP column. You don't need to nest the query in a subquery, you can just pull the largest count of athlete rewards by selecting the largest row from your query.
SELECT TOP 1 Athletes.Group as Group
, Count (Athletes.Reward) AS Reward1
FROM Athletes
GROUP BY Athletes.Group
ORDER BY Reward1 DESC;

Related

SQL - request to find the average loans by user

I am struggling with a SQL request.
I need to find a request to calculate the average loans per user.
I was thinking in doing a LEFT OUTER JOIN between the table SUSCRIBER and the table Loan in order to get all the subscribers even if they have made a loan or not.
Then I have used a GROUP BY based on the IDNumber of the suscribers to COUNT the number of lines. Then I need to get the total numbers of suscribers to calculate the average but by doing this I get 1 or 0 only instead of an average. I don't know what is wrong in this request. Maybe as I have made a group by the IDNumber from the table Loan. How could I get the number of all IDNumber based on this request to make the division ? Thanks
See, the request written in English:
SELECT COUNT(Loan.IDNumber) / COUNT(SUSCRIBER.IDNumber), SUSCRIBER.Name AS Name
FROM SUSCRIBER
LEFT OUTER JOIN Loan
ON SUSCRIBER.IDNumber = Loan.IDNumber
GROUP BY Loan.IDNumber;
Here are the tables:
TOPIC(Code_topic, Description)
KEY_WORD(Code_key_word, keyword)
PUBLISHER(Code_publisher, Name, Adress)
AUTHOR(Code_author, Name, Surname)
BOOK(Code_catalogue, Title, #Code_topic)
COPY (Code_bookshelf, Code_wear, Date_aquisition, #Code_publisher, #Code_catalogue)
SUSCRIBER(IDNumber, Name, Adress, Phone, Birthdate, Subscription_date)
Loan(#IDNumber,Code_bookshelf,Loan_date, Return_date)
Thank you in advance
I need to find a request to calculate the average loans per user.
If you want the average number of loans per subscriber, then you want the total number of loans divided by the number of subscribers. You can do this with an expression like this:
SELECT num_l * 1.0 / num_s
FROM (SELECT COUNT(*) as num_s FROM SUBCRIBER) s CROSS JOIN
(SELECT COUNT(*) as num_l FROM Loan) l;
The * 1.0 is because some databases do integer division, so 3 / 2 = 1 rather than 1.5.

Find the movie with the largest cast, out of the list of movies that have a review

Can someone please assist me on how to write a query that will return the following:
"Find the movie with the largest cast, out of the list of movies that have a review."
OUTPUT: movie_title, number_of_cast_members
using this database https://neo4j.com/developer/movie-database/
This needs to be written in Cypher.
MATCH (:Actor)-[:ACTS_IN]->(m:Movie)<-[:RATED]-()
with m, count(*) AS actor_count order by actor_count desc
return m.title, actor_count limit 1
MATCH the pattern you search (actors that acts in a movie that have a rating)
Use an aggregation function to count the numbers of actors (count(*)) and group them by Movie (with m)
ORDER the result by the count descendingly desc
Return the title and the count of the first item limit 1. As the list is ordered with the most largest cast first, limiting the result to the first item gives the largest cast.
Note: if two movies have the same size of casting, only one is returned.

Counting distinct values output from a grouped SQL Count function

I've got a database that holds information about volunteers and their participation in a range of events.
The following query gives me a list of their names and total attendances
SELECT
volunteers.last_name,
volunteers.first_name,
count (bookings.id)
FROM
volunteers,
bookings
WHERE
volunteers.id = bookings.volunteer_id
GROUP BY
volunteers.last_name,
volunteers.first_name
I want the result table to show the distinct number of attendances and how many there are of each; So if five people did one event it'd display 1 in the first column and 5 in the second and so on.
Thanks
If I understand correctly, you want what I call a "histogram of histograms" query:
select numvolunteers, count(*) as numevents, min(eventid), max(eventid)
from (select b.eventid, count(*) as numvolunteers
from bookings b
group by b.eventid
) b
group by numvolunteers
order by numvolunteers;
The first column is the number of volunteers booked for an "event". The second is the number of events where this occurs. The last two columns are just examples of events that have the given number of volunteers.

SQL: How to use sum in group by

SELECT idteam,
job,
price,
COUNT('X') as INFORMS,
SUM(COUNT('X') * price) as TOTAL
FROM REP
JOIN COSTS ON (job = categ AND to_number(to_char(REP,'YYYY')) = year)
GROUP BY idteam, job, price, TOTAL
ORDER BY IDTEAM;
I don't know why if I write TOTAL in GROUP BY and sql sends me error.. Identifier invalid.
I don't know how can I resolve that.
Thanks.
The column "TOTAL" is an alias for SUM(COUNT('X') * price).
It cannot be used as a column identifier in the GROUP BY clause. You must say GROUP BY SUM(COUNT('X') * price), because "TOTAL" is unknown/not a column, at the time of grouping.
After using GROUPING, you can refer to "TOTAL" in a HAVING clause.
In any case, the version/type of SQL your are using, doesn't allow it.
Additionally, why are you COUNTing 'X'? That X is a fixed value, and does not depend on any of your columns. If you would like to count each row, just use Count(1) or Count(*). Also, you don't need to SUM a COUNT. A COUNT is already summed.
You should post the structure of both REP and COSTS. Your linked image doesn't have enough info to support the query you wrote.
select
idteam,
-- job, /* not selected since it would need to be grouped*/
sum(price) as 'theSUM'
from REP
join COSTS
on REP.categ = COSTS.job
and COSTS.year = 2016
group by idteam
order by idteam

Remove Min and Max in group by

I am trying to get a total hours from a dataset and because you can have the same asset with the same company (company_B) twice at two different times I have this join issue. I know I want the min for company_B gone and the Max for company_B gone because they represent wrong dates being matched. The negative is easy but what about the Max?
I have:
AssetID------StartDate-------FinishDate-------CompanyName----HoursOnSite
22222-------2016-02-12-------2016-02-20-------Company_A--------192
22222-------2016-02-01-------2016-02-09-------Company_B--------208 (keep)
22222-------2016-02-12-------2016-02-09-------Company_B-------(-56) (remove)
22222-------2016-02-01-------2016-02-21-------Company_B--------480 (remove)
22222-------2016-02-12-------2016-02-21-------Company_B--------216 (keep)
55555-------2016-02-18-------2016-02-22-------Company_C--------96
99584-------2016-02-22-------2016-02-25-------Company_D--------63
I think you can do the query for the records with max and min HoursOnSite for company B, and use (not in) or not equal to exclude those records.
If you still have concern, please paste your query.
I'm assuming that there has to be atleast 3 instances of unique assetid - companyname combination for the Max, Min filters to work. You can change it in the final where statement tO suit your requirement
WITH CTE
AS (
SELECT *
,count(CompanyName) OVER (PARTITION BY AssetID,CompanyName) AS a
FROM <TABLE_NAME>
)
SELECT *
FROM CTE
WHERE HoursOnSite NOT IN (
SELECT MAX(HoursOnSite)
FROM <TABLE_NAME>
)
AND gdp NOT IN (
SELECT min(HoursOnSite)
FROM <TABLE_NAME>
)
AND a > 2 --MODIFY AS PER YOUR REQUIREMENT