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

creating joins between tables with "group by" clause to limit rows

Hi!
I have problems creating joins between tables and to limit rows for a
specified clolumn. I have 2 tables!

employees
departments

I face the problems with the GROUP BY clause and i don't get it handled
solving that problem. Neither with oracle sql or SQL1999 syntax!

Oracle SQL:

SELECT
dep.department_name as "Name",
dep.location_id as "Location",
emp.salary AS "Salary"
FROM employees emp, departments dep
WHERE (emp.department_id = dep.department_id)
GROUP BY dep.department_name;

SQL1999:

SELECT
department_name as "Name",
location_id as "Location",
salary AS "Salary"
FROM employees emp
JOIN departments dep
USING(department_id)
GROUP BY department_id;
for any help
Thank you
Tamer
Jul 19 '05 #1
1 17061
Tamer,

Since you didn't actually ask a question, I'll just offer this:

Find a nice tutorial or introductory SQL text.

With GROUP BY, your immediate select list is limited to aggregates of the
group.

In your example, it's fine to have department_name in the select list, but
not proper to have location_id or salary in the select list, since they are
not
aggregates of the group. One solution would be to add those items to the
GROUP BY list, but often that's not appropriate.

In your case, it appears that location_id could appropriately be added to
the select list, but I doubt salary would be.

For salary, you would more likely have to choose some reasonable aggregate
to show, like:

SELECT
dep.department_name as "Name",
dep.location_id as "Location",
avg(emp.salary) AS "averageSalary"
FROM employees emp, departments dep
WHERE emp.department_id = dep.department_id
GROUP BY dep.department_name, dep.location_id;
If you really want employee specific results, I suspect you don't want to
use
GROUP BY at all. You might just want to ORDER BY department_name
instead.

SELECT
dep.department_name as "Name",
dep.location_id as "Location",
emp.salary AS "Salary"
FROM employees emp, departments dep
WHERE emp.department_id = dep.department_id
ORDER BY dep.department_name;
Best regards... Jon

Jon Armstrong
"Tamer Higazi" <ta*****@myrealbox.com> wrote in message
news:c7**********@ngspool-d02.news.aol.com...
Hi!
I have problems creating joins between tables and to limit rows for a
specified clolumn. I have 2 tables!

employees
departments

I face the problems with the GROUP BY clause and i don't get it handled
solving that problem. Neither with oracle sql or SQL1999 syntax!

Oracle SQL:

SELECT
dep.department_name as "Name",
dep.location_id as "Location",
emp.salary AS "Salary"
FROM employees emp, departments dep
WHERE (emp.department_id = dep.department_id)
GROUP BY dep.department_name;

SQL1999:

SELECT
department_name as "Name",
location_id as "Location",
salary AS "Salary"
FROM employees emp
JOIN departments dep
USING(department_id)
GROUP BY department_id;
for any help
Thank you
Tamer



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mark | last post by:
How can I use "Group By" or a formula to group my query results in 1-year periods from a given date, e.g. 3 groups: 1 Sept 2001 - 1 Sept 2002 1 Sept 2002 - 1 Sept 2003 1 Sept 2003 - 1 Sept 2004 ...
0
by: TaeHo Yoo | last post by:
After running my code, group tree(the left section of the report) changes but not the content in the report. My code is ---------------------------------------------------------- Private Sub...
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
2
by: littlebeam | last post by:
Suppose there is a table containing these recodes. country ------- CON_CHN CON_JAP JAP CON_CHN When I use the following sql:
1
by: ASF | last post by:
Is it possible to create a crystal report graph in which a user selects some parameters---a date range for instance--- AND the table field that they wish to "Group By" then hits the submit button...
1
by: Thomas Qi | last post by:
There is a basic sql below: SELECT CTEnr AS ID, TimeStamp, DatagramSize, Source AS LocalIP, Destination AS RemoteIP, Protocol, Messages, SourcePort AS LocalPort, DestPort AS RemotePort FROM...
2
by: nico3334 | last post by:
Hi, I have a SQL table that has data like this: Title Month Info 1 ---- 7 ---- 100 1 ---- 7 ---- 100 1 ---- 8 ...
2
by: BrightFuture | last post by:
I'm fairly new to detailed data analysis. A lot of my queries end up using the Group By function, but other than knowing when to use it, I don't understand how it works. All I'm doing is hitting...
0
by: Tamer Higazi | last post by:
Hi! I have problems creating joins between tables and to limit rows for a specified clolumn. I have 2 tables! employees departments I face the problems with the GROUP BY clause and i don't...
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...
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
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
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
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...

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.