Solved

GROUP BY question

Posted on 2014-11-19
2
121 Views
Last Modified: 2014-11-19
Hello, I must "build" a "top-ten" of loans in a library querying all the loans registered in an oracle database.
I've found the right query to do:

select Z30_REC_KEY, Z30_NO_LOANS from z30 left join z36h on z36h_rec_key = z30_rec_key where Z36H_SUB_LIBRARY  = 'COLUG' AND Z36H_LOAN_DATE BETWEEN 20120401 AND 20141113
GROUP BY z36h_rec_key

but the result has multiple entries of the same type. here follows an example of the first rows:

000222514                              26
000222514                              26
000222514                              26
000168117                              21
000168117                              21

(say..the book 000222514 has 26 loans, the book 000168117 21 loans...)
I'd like to obtain instead only one entry for each record, i.e.:
000222514                              26
000168117                              21

I suppose I must use a GROUP BY, but if I group by Z30_REC_KEY I obtain the following error:

select Z30_REC_KEY, Z30_NO_LOANS from z30 left join z36h on z36h_rec_key = z30_rec_key where Z36H_SUB_LIBRARY  = 'COLUG' AND Z36H_LOAN_DATE BETWEEN 20120401 AND 20141113
GROUP BY Z30_REC_KEY

ERROR at line 1:
ORA-00979: not a GROUP BY expression

can you help me?
thanks
fabiano
0
Comment
Question by:fabianope65
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40451974
Use Select Distinct instead:

select distinct Z30_REC_KEY, Z30_NO_LOANS from z30 
left join z36h on z36h_rec_key = z30_rec_key 
where Z36H_SUB_LIBRARY  = 'COLUG' AND Z36H_LOAN_DATE BETWEEN 20120401 AND 20141113

Open in new window

0
 

Author Closing Comment

by:fabianope65
ID: 40451995
it works perfectly, thanks!! :=))
fabiano
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question