SQL Query for max value from subsets - sql

I have a table bls_jobs with the following columns: city, state, occ_title, jobs_1000, and loc_quotient
I am trying to retrieve the highest loc_quotient for each city (each city has several occ_titles, and each occ_title has a loc_quotient)
Currently, I can use this query:
SELECT *
FROM bls_jobs
WHERE city = 'Hattiesburg'
ORDER BY loc_quotient DESC
LIMIT 1
Which does return what I'm looking for (the highest loc_quotient in the city, with each of the columns returned), but I'm struggling to figure out how to have it do this for all cities so I have a returned output of just each city's highest loc_quotient along with it's data from the other columns ...

Use distinct on:
SELECT DISTINCT ON (j.city) j.*
FROM bls_jobs j
ORDER BY j.city, j.loc_quotient DESC;
DISTINCT ON is a convenient Postgres extension. It returns the first row in each group, where groups are the keys in the DISTINCT ON () clause (and the ORDER BY is consistent with them).

Related

Logic of No of count of row with the value of column of column

I write a SQL query for getting the number of rows using WHERE clause with the city.
My code is:
SELECT COUNT(*)
FROM rent
WHERE city = 'Lahore';
This successfully returns the result. But now I make logic of getting number of count where in which row has city name and by that name in which row that name exist that row count. Simply mean that I don't want to define the city column value, it restricts my result.
If I understand correctly, you're asking for a count of each unique city?
select City, count(*) as CityCount
from Rent
group by City
order by CityCount desc; // Show highest count first

Hacker Rank SQL problem | Problem using count() and MINUS | Weather Observation Station 4

I used
SELECT CITY
FROM STATION
MINUS
SELECT DISTINCT CITY
FROM STATION;
Should I use count() to get the number of the elements in each column or does MINUS automatically get the numbers?
BTW the code didn't work as an answer even when I used count().
The link:
https://www.hackerrank.com/challenges/weather-observation-station-4/problem
Your query does not do what you want. MINUS is a set-based operator, while what you need is aggregation. Here, you can take the difference between COUNT(*) (that's the total number of rows in the table) and COUNT(DISTINCT city) (that's the count of distinct values in column city):
select count(*) - count(distinct city) result
from station

SQL: Find duplicates and for each duplicate group assign value of first duplicate of that group

I have the results in the top table. I would like the results in the bottom table.
Using an SQL query on the table above, I would like to find groups of duplicates (where the values in all columns except Id and Category are identical) and from that create a result that has for each entry the lowest Id from its group of duplicates and the (unmodified) Category from the original table.
Window function min can be used here:
select min(id) over (partition by first_name, last_name, company) id,
category
from t;

Usage of aggregate function Group by

I have observed that Count function can be used without the usage of aggregate function Group by. Like for example:
Select Count(*) from Employee
It would surely return the count of all the rows without the usage of aggregate function. Then where do we really need the usage of group by?
Omitting the GROUP BY implies that the entire table is one group. Sometimes you want there to be multiple groups. Consider the following example:
SELECT month, SUM(sales) AS total_sales
FROM all_sales
GROUP BY month;
This query gives you a month-by-month breakdown of sales. If you omitted month and the GROUP BY clause, you would only receive the total sales of all time which may not have the granularity you require.
You can also group by multiple columns, giving finer detail still:
SELECT state, city, COUNT(*) AS population
FROM all_people
GROUP BY state, city;
Additionally, using a GROUP BY allows us to use HAVING clauses. Which lets us filter groups. Using the above example, we can filter the result to cities with over 1,000,000 people:
SELECT state, city, COUNT(*) AS population
FROM all_people
GROUP BY state, city
HAVING COUNT(*) > 1000000;
The group by clause is used to break up aggregate results to groups of unique values. E.g., let's say you don't want to know how many employees you have, but how many by each first name (e.g., two Gregs, one Adam and three Scotts):
SELECT first_name, COUNT(*)
FROM employee
GROUP BY first_name

MS Access query count and show

I have a table with cities and publishers in those cities.
How do I create a query that counts how many publishers are in which city, and then also shows the names of those publishers only for the city with the most publishers.
See if this works for you.
SELECT Sub2.City,
Table1.Publisher
FROM (
SELECT TOP 1 Sub.City, Sub.CountOfPublisher AS MaxOfCountOfPublisher
FROM (SELECT Table1.City,
Count(Table1.Publisher) AS CountOfPublisher
FROM Table1
GROUP BY Table1.City
) AS Sub
ORDER BY Sub.CountOfPublisher DESC
) AS Sub2
INNER JOIN Table1
ON Sub2.City = Table1.City
Note: Replace Table1 with your table name.
Explanation
So, we needed to roll up our aggregation so Access can use the information we provided in our Count.
From the inside out:
We need a Count of Publishers for each City, and we want to Group By each City. This gives us some numbers we can work with per City.
The next piece, we want to select the TOP 1 record from those results, sorted by the Count of Publishers DESC (descending - highest to lowest). So, sorting by the Count Descending makes the first record the City with the highest count of Publishers. We then use that in our final step.
Finally, we wanted the City and the Publishers for the City with the most publishers. Well, thus far we have the City with the most Publishers, but we don't have a list of Publishers. To get those, we need to join our original Table on City.
This basically says, I have the City with the most Publishers, now give me all of the records from our Table where the City is equal to the City (INNER JOIN Table1 ON Sub2.City = Table1.City) we are supplying in our query.