473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

select count(table) problem

3
Hello.
I have a database with movies similar with imdb and i want to find out which directors have directed both thriller and drama movies.
The output i want is like this "DIR_NAME,amount of thriller movies,amout of drama movies"
First i did a check to see the expected number.Checkin only directors that have made thriller movies with
Expand|Select|Wrap|Line Numbers
  1. select DIR_NAME, count(GENRE) 
  2. from MGENRES, DIRECTORS, DIRECTED_BY
  3. where GENRE = 'Thriller' and MGENRES.MOVIE_ID = DIRECTED_BY.MOVIE_ID and DIRECTED_BY.DIR_ID = DIRECTORS.DIR_ID
  4. group by DIR_NAME
  5. order by count(GENRE) desc
  6.  
I found 3600 results and max count per director 36,Doing the same with keyword drama i found 19000 results and max count per director 200.So results must be 3600 or less and max count 200.
I use this query for finding out directors taht have made thriller and drama movies
Expand|Select|Wrap|Line Numbers
  1. select DIR_NAME, count(t1.GENRE) as Thriller, count(t2.GENRE) as Drama
  2. from MGENRES as t1, MGENRES as t2, DIRECTORS, DIRECTED_BY as t3, DIRECTED_BY as t4
  3. where t1.GENRE = 'Thriller' and t1.MOVIE_ID = t3.MOVIE_ID and t3.DIR_ID = DIRECTORS.DIR_ID and t2.GENRE = 'Drama' and t2.MOVIE_ID = t4.MOVIE_ID and t4.DIR_ID = t3.DIR_ID
  4. group by DIR_NAME
  5. order by count(t1.GENRE) desc 
  6.  
The directors results seems good but the count is for sure wrong.I get the same count for each row count(t1.GENRE) = count(t2.GENRE) and very big amounts that can't be true 1000 thriller movies directed by one director etc which is fault.
Any clue?
Dec 6 '06 #1
3 4591
I don't think that will work because you are still doing a record count on the rows, which because you have joined the 2 tables for thriller and Drama. They row will give you a count of 1 no matter what genra it is.

Try something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   DIR_NAME,
  3.   SUM(CASE WHEN [T1].[GENRE] = 'Thriller' THEN 1 ELSE 0) AS THRILLERS,
  4.   SUM(CASE WHEN [T1].[GENRE] = 'Drama' THEN 1 ELSE 0) AS DRAMAS
  5. FROM
  6.   MGENRES [M] LEFT OUTER JOIN
  7.   DIRECTEDBY [DB] ON [M].[MOVIE_ID] = [DB].[MOVIE_ID] LEFT OUTER JOIN
  8.   DIRECTORS [D] ON [DB].[DIR_ID] = [D].[DIR_ID]
  9. GROUP BY DIR_NAME
  10.  

This would give you the count for each of these genres. However, I would suggest creating a view of the count of each genre/director... then join that view.

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   COUNT(*),
  3.   GENRE,
  4.   DIR_ID,
  5.   DIRECTOR_NAME
  6. FROM
  7.   MGENRES M LEFT OUTER JOIN
  8.   DIRECTEDBY D ON M.DIR_ID = D.DIR_ID LEFT OUTER JOIN
  9.   DIRECTORS DRS ON D.DIR_ID = DRS.DIR_ID
  10. GROUP BY
  11.   DIR_ID, DIRECTOR_NAME, GENRE
  12.  
I only showed you the first way so you can have some creative ideas for future scripts. The second is the best way to do it. You can even build it into a stored procedure to pass dir_id, or genre... It would be quicker.

Best regards,

Michael C. Gates
Dec 6 '06 #2
ret4rt
3
Thx for reply.I understand ur 2nd query but your 1st didn't get it.Tried it and gave me errors.The 2nd one is working ok.I thought it like u said with views and so i created two views.One that contains THRILLERS and the other DRAMAS,
I did it like this
Expand|Select|Wrap|Line Numbers
  1. create view DRAMAS as
  2. select DIR_NAME, count(GENRE) as DramaCount , DIRECTORS.DIR_ID
  3. from MGENRES, DIRECTORS, DIRECTED_BY
  4. where GENRE = 'Drama' and MGENRES.MOVIE_ID = DIRECTED_BY.MOVIE_ID and DIRECTED_BY.DIR_ID = DIRECTORS.DIR_ID
  5. group by DIR_NAME , DIRECTORS.DIR_ID
  6.  
  7. create view THRILLERS as
  8. select DIR_NAME, count(GENRE) as ThrillerCount , DIRECTORS.DIR_ID
  9. from MGENRES, DIRECTORS, DIRECTED_BY
  10. where GENRE = 'Thriller' and MGENRES.MOVIE_ID = DIRECTED_BY.MOVIE_ID and DIRECTED_BY.DIR_ID = DIRECTORS.DIR_ID
  11. group by DIR_NAME , DIRECTORS.DIR_ID
  12.  
  13.  
  14. select distinct DRAMAS.DIR_NAME, ThrillerCount, DramaCount
  15. from DRAMAS, THRILLERS
  16. where DRAMAS.DIR_ID = THRILLERS.DIR_ID
  17. group by DRAMAS.DIR_NAME , ThrillerCount , DramaCount
  18. order by ThrillerCount desc
  19.  
As you see then i use a select to output what i want.Seems ok now to me.Hope i'm not mistaken :)
Dec 6 '06 #3
Glad to help. I think you can do this with one view though. It would group it by Director / Genre, then you can do a select count(*) from myView where genre = x, etc. Once you have the view, you can summarize the data further. The view is like a temporary table. Structured the same way.

Best regards,

Michael C. Gates
Dec 6 '06 #4

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

Similar topics

0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
10
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no...
12
by: Bing Wu | last post by:
Hi, Has anyone got problem with this on large table. It seems it take ages to return the result ( half an hour :-( ). SELECT COUNT(*) FROM COORDINATE A strange thing is the tempory space...
3
by: Ben Terry | last post by:
I have imported a legacy C++ project into a VisualStudio.NET 2003 solution. I have some new C# projects in the solution as well. I am using the managed extensions in my C++ project so that I can...
4
by: Aryan | last post by:
Hi, I am having problem with DataTable.Select() method. I am using ASP.NET 2.0. I have DataSet which reads data from XML file using DataSet.ReadXML(). Now this dataset has various datatable,...
0
by: Sektor | last post by:
Hi guys, I have just a problem with the Select timeout parameter. The MSDN tells us: microSeconds The time-out value, in microseconds. A -1 value indicates an infinite time-out. I try to use...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
2
by: tak786 | last post by:
hello.. i have created query which has 1 "date field" which I need to count the instances of each month. i.e field 1: date field = count of "*01/2007" field 2: date field = count of...
4
by: jodi319 | last post by:
This was p0sted a while back: ********************************************************************* Relationship view - can't select table Question posted by: Don (Guest) on November 12th, 2005...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.