473,472 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Subqueries

3 New Member
I am having trouble displaying the movie category that generated more rental transactions than any other movie categories. I must also display the number of rental transactions generated by this category.


select movie_category, COUNT(rental_id)
from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
group by movie_category


MOVIE_CATE COUNT(RENTAL_ID)
---------- ----------------
Comedy 2
Horror 2
Western 1
Drama 5
SciFi 2


That is the output my current query gives me. I'm having trouble formulating a subquery that will help me just list the highest count(rental_id) along with it's specified category.
Apr 15 '08 #1
6 2405
amitpatel66
2,367 Recognized Expert Top Contributor
I am having trouble displaying the movie category that generated more rental transactions than any other movie categories. I must also display the number of rental transactions generated by this category.


select movie_category, COUNT(rental_id)
from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
group by movie_category


MOVIE_CATE COUNT(RENTAL_ID)
---------- ----------------
Comedy 2
Horror 2
Western 1
Drama 5
SciFi 2


That is the output my current query gives me. I'm having trouble formulating a subquery that will help me just list the highest count(rental_id) along with it's specified category.
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select movie_category, MAX(COUNT(rental_id)) OVER() from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
  3. group by movie_category
  4.  
  5.  
Apr 15 '08 #2
Dave44
153 New Member
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select movie_category, MAX(COUNT(rental_id)) OVER() from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
  3. group by movie_category
  4.  
  5.  
i think by using the max(count (rental_id)) ... your going to end up with each category having the count of whichever category has the largest count.
Apr 15 '08 #3
Dave44
153 New Member
I am having trouble displaying the movie category that generated more rental transactions than any other movie categories. I must also display the number of rental transactions generated by this category.


select movie_category, COUNT(rental_id)
from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
group by movie_category


MOVIE_CATE COUNT(RENTAL_ID)
---------- ----------------
Comedy 2
Horror 2
Western 1
Drama 5
SciFi 2


That is the output my current query gives me. I'm having trouble formulating a subquery that will help me just list the highest count(rental_id) along with it's specified category.
you could use the row_number analytic to get an order by the largest count descending and then create an outer query that will select the row from the analytic function that is # 1

Expand|Select|Wrap|Line Numbers
  1. SELECT movie_cat,
  2.        the_count
  3. FROM   (SELECT   c.movie_cat,
  4.                  COUNT(*) the_count,
  5.                  ROW_NUMBER() OVER(ORDER BY COUNT(*) DESC) my_row
  6.         FROM     rental a 
  7.                  JOIN movie b ON a.movie_id = b.movie_id
  8.                  JOIN movie_type c on a.movie_id = c.movie_id
  9.         GROUP BY c.movie_cat)
  10. WHERE  my_row = 1;
  11.  
Apr 15 '08 #4
schug
3 New Member
Edited for being incorrect!!!!!!!!!
Apr 15 '08 #5
schug
3 New Member
Couldn't get that last suggestion to work.

Expand|Select|Wrap|Line Numbers
  1.  
  2. select movie_category, count(rental_id)
  3. from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
  4. where count(rental_id) = MAX(count(rental_id))
  5. group by movie_category
  6.  
  7.  
Trying something like this but it will not work =(.
Apr 15 '08 #6
amitpatel66
2,367 Recognized Expert Top Contributor
Couldn't get that last suggestion to work.

Expand|Select|Wrap|Line Numbers
  1.  
  2. select movie_category, count(rental_id)
  3. from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
  4. where count(rental_id) = MAX(count(rental_id))
  5. group by movie_category
  6.  
  7.  
Trying something like this but it will not work =(.
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM
  3. (select movie_category, count(rental_id) cnt
  4. from rental JOIN movie USING(movie_id) join movie_type USING(movie_cat_id)
  5. group by movie_category
  6. ORDER BY cnt DESC)
  7. WHERE ROWNUM < 2
  8.  
  9.  
Apr 16 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: pete | last post by:
Been banging my head against the wall with subqueries. Even simple stuff like this fails: SELECT CompanyName FROM tblcompanies WHERE CompanyName IN (SELECT HostName FROM tblhosts) Am I...
6
by: Daniel Elliott | last post by:
Hello, I was wondering if anyone would be able to help me with a problem I'm having. I'm trying to use the following query: SELECT Distinct c.site_id FROM campsite c WHERE c.site_id NOT IN...
5
by: Nick | last post by:
Im moving a development app (MySQL 5.0) to a different server which runs MySQL 4.0.20-standard. I am getting errors on queries that have subqueries such as... SELECT id FROM table1 WHERE id IN...
2
by: Kevin | last post by:
While converting SQL statements for a database change, I discovered a big performance hit in MYSQL with subqueries vices Sybase. I'm hoping that someone might be able to help me understand why? ...
2
by: CSN | last post by:
Is there much difference between using subqueries and separating out them into separate queries? __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free web site building...
2
by: orin | last post by:
Hi all, I've seen mention that you can use nested subqueries down to as many levels as you like but whenever I run the following: select * from table1 where tab1ID in (select tab1ID from...
2
by: psuaudi | last post by:
I have a main query that I would like to call two different subqueries. In MS Access, I usually just save the two subqueries as separate queries which are then called by a third separate and main...
4
by: muzu1232004 | last post by:
Can anyone explain me when we use correlated subqueries rather than nested subqueries. Do all the correlated subqueries can be written in nested subqueries form as well ? What are the major...
0
debasisdas
by: debasisdas | last post by:
Using Subqueries ================== The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement. The sub query executes once before the main query. The...
1
by: lizandra | last post by:
Greetings, I am a newbie, I have been working to extract data from a basic sales db and trying to decide when I should use joins and when I should use subqueries. Much of what I read online says...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.