473,396 Members | 2,030 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,396 software developers and data experts.

Aggregate function, count or max, on >2 joined tables where pk is composite

How do I use an aggregate function, e.g. count or max of dvds where I have 4 tables & the table I want to get a count or max from (rental) has a composite key that is a combo of dvd and copy

How do I use an aggregate function, e.g. count or max of dvds where I have 4 tables & the table I want to get a count or max from (rental) has a composite key that is a combo of dvd and copy. I want to know the number of dvds sold out/not sold out (compare the number of dvds out but not returned/returned in rental table to the numbercopies in dvd)
Tables
dvd: dvd_id, title, numbercopies
copy: copy_id
dvdcopy: dvdcopy_id, dvd_id, copy_id
rental: rental_id, dvdcopy_id, rentaldate , rentalreturn

I've tried using Oracle's SQL Developer to do the following:
(for dvds sold out)
Expand|Select|Wrap|Line Numbers
  1. SELECT dc.dvd_id, 
  2.    dc.title, 
  3.    dc.copyid, 
  4.    COUNT(d.dvd_id) 
  5.        AS numberdvd, 
  6. FROM dvdcopy dc
  7.    JOIN dvd d 
  8.       ON dc.dvd_id=m.dvd_id
  9.    JOIN rental r 
  10.       ON r.dvdcopy_id=d.dvd_id
  11. WHERE rentaldate Is Not Null 
  12.    AND rentalreturn Is Null;
  13. GROUP BY dc.dvd_id, 
  14.    dc.copy_id;
My code gives me a ORA-00937'Not a single-group group function' error message. I would appreciate any help!
Dec 4 '12 #1
2 2107
zmbd
5,501 Expert Mod 4TB
Did you mean to place the semicolon, ";", at the end of the WHERE clause (Line12)? I did not remove it while formatting your posted SQL just to be sure it was intended vs. typo.

-
This is a fairly heavy read: Oracle - Aggregate Functions it also has a link to the GROUP BY clause and that is a REALLY heavy read.
Dec 4 '12 #2
rski
700 Expert 512MB
First of all you have to add dc.title column to group by clause.

Next, what is m in
Expand|Select|Wrap|Line Numbers
  1. ON dc.dvd_id=m.dvd_id
  2.  
?

If alias m is for dvd (should be d) then also change
Expand|Select|Wrap|Line Numbers
  1. COUNT(d.dvd_id)
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. COUNT(dc.dvd_id)
  2.  


What about this query
Expand|Select|Wrap|Line Numbers
  1. SELECT dc.dvd_id, 
  2.    dc.title, 
  3.    dc.copyid, 
  4.    COUNT(dc.dvd_id) 
  5.        AS numberdvd, 
  6. FROM dvdcopy dc
  7.    JOIN dvd d 
  8.       ON dc.dvd_id=d.dvd_id
  9.    JOIN rental r 
  10.       ON r.dvdcopy_id=d.dvd_id
  11. WHERE rentaldate Is Not Null 
  12.    AND rentalreturn Is Null;
  13. GROUP BY dc.dvd_id, 
  14.    dc.copy_id,dc.title;
  15.  
Dec 5 '12 #3

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

Similar topics

2
by: SG | last post by:
Is there a stored procedure or query that will count all the records in all my tables in a database? Many thanks Sam
2
by: Shannan Casteel via AccessMonster.com | last post by:
I have an invoice form with Parts and Labor as subforms. The parts section is based on a query that pulls data from 2 tables (tblParts & tblPartPerClaim) .. It has fields: ClaimID:...
1
by: Pascal Polleunus | last post by:
Hi, Is there an *easy* way to display a list of tables with their number of input values in psql ? Wouldn't it be useful to have the ability to execute aggregate functions with the \d command...
2
by: commanderjason | last post by:
This seems like a very simple question but i have never been able to find an easy answer to it. I have a user table and i do a join with another table, we'll call the other table a results...
2
by: danny_m | last post by:
Hi there I have two tables, pages and sections, of which pages are a subset of sections. I'm listing out the sections in a list and want to include a COUNT of how many pages each section has...
3
by: ncsthbell | last post by:
I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!! I...
3
by: cpanthro | last post by:
Hello, I'm using Access on XP and I am trying to count the number of cases in a Table via a query using two criteria. The criteria are being drawn from two different tables. I keep getting an...
3
by: --CELKO-- | last post by:
Is there a simple way to get a COUNT(*) for all tables in a schema in DB2 LUW 9.0?
4
by: thayes5150 | last post by:
I have an Access 2003 database linked to an MS SQL 2005 backend. We use the database as part of a process whereby we use Zone OCR to scan records off paper forms into an XML file, suck the data off...
1
by: Stevestan1 | last post by:
I am getting the "cannot produce expression ..... as part of aggregate function" error everytime I try to run a query with this expression Pricing: IIf(! Is Not Null And ! <= !,"Pricing","None") ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.