Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm new to working with Hive, but I am trying to print a table with a total number of car body types for different cities.
select body_type, city, count(body_type) AS total_for_body
from usedcartestfinal
group by body_type, city
order by total_for_body DESC
LIMIT 20;
When I run the above, I get a print out of duplicate cities, and I only want a city to be printed once, I figured I'd use SELECT DISTINCT city, yet I can't as I get an error that Group By cannot be used in the same query.
Not quite sure how else to go about this query, any advice or suggestions would be appreciated.
Here is my output: https://imgur.com/BfQVsjF
I essentially only want Houston to print once since the highest sold there is SUV/CROSSOVER
–
–
–
You should remove the body_type from the group by clause, and instead have a distinct count on it:
select city, count(distinct body_type) AS total_for_body
from usedcartestfinal
group by city
order by total_for_body DESC LIMIT 20;
Use subquery with analytic row_number to get record with highest count for each city:
select body_type, city, total_for_body
select body_type, city, total_for_body
row_number() over(partition by city order by total_for_body desc) rn
select body_type, city, count(body_type) total_for_body
from usedcartestfinal
group by body_type, city
)s where rn = 1
–
If you will include the BODY_TYPE in the GROUP BY then it will be grouped by BODY_TYPE and CITY so for each CITY and each BODYTYPE, you will get one row.
You should remove BODYTYPE from the GROUP BY and SELECT list as follows:
SELECT * FROM
(SELECT BODY_TYPE,
CITY,
COUNT(DISTINCT BODY_TYPE) AS TOTAL_FOR_BODY,
ROW_NUMBER() OVER (PARTITION BY CITY
ORDER BY COUNT(DISTINCT BODY_TYPE) DESC) AS RN
FROM USEDCARTESTFINAL
GROUP BY BODY_TYPE,
CITY) AS T WHERE RN = 1
ORDER BY TOTAL_FOR_BODY DESC LIMIT 20;
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.