Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2006, 01:52 PM
Adrastea0413's Avatar
Adrastea0413 Adrastea0413 is offline
Back after 2 years!
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2003
Location: Washington, DC Metro
Posts: 1,747 Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 23 h 43 m 8 sec
Reputation Power: 884
Facebook
Combination of left outer join and where clause

I'm not sure if my subject line is good because it pretty much describes what *I* tried as the solution, which may not be the correct solution for this problem. If anyone can help me solve my problem, I'd be grateful.

I'll do my best to try and explain the problem I'm having. If I'm unclear, please let me know and I'll try to clarify.

To make matters easier, I am using fake table names and such in this example to make it easier for people to help without knowing exactly what everything is.

In this explanation, I'll use two tables, a department table and a finances table. The department table has a dept_id (PK) and a title. The finances table has a row_id (PK), a dept_id (FK to departments), a month and year to which the row's data is applicable, a revenue amount and an expenses amount.

Here is an example:

Code:
dept_id | title
--------+------------------
1111    | Human Resources
2222    | Accounting
3333    | Customer Service

row_id | dept_id | month | year | revenue | expenses
-------+---------+-------+------+---------+----------
1      | 1111    | 2     | 2006 | 10000   | 8000
2      | 1111    | 3     | 2006 | 15000   | 10000
3      | 2222    | 3     | 2006 | 12500   | 13000


My goal is to have a table that lists all the departments and the applicable revenue/expenses for a given month. For example, if I want to show month 2 and year 2006, the query would spit out:

Code:
dept_id | title            | month | year | revenue | expenses
--------+------------------+-------+------+---------+----------
1111    | Human Resources  | 2     | 2006 | 10000   | 8000
2222    | Accounting       |       |      |         |
3333    | Customer Service |       |      |         |


If I wanted to show month 4 and year 2006, the query would spit out:

Code:
dept_id | title            | month | year | revenue | expenses
--------+------------------+-------+------+---------+----------
1111    | Human Resources  |       |      |         |
2222    | Accounting       |       |      |         |
3333    | Customer Service |       |      |         |


The query that I wrote to handle this uses a left join and a where clause:

sql Code:
Original - sql Code
  1. SELECT departments.dept_id, title, month, year, revenue, expenses FROM departments LEFT JOIN finances ON departments.dept_id = finances.dept_id WHERE (month = 2 OR month IS NULL) AND (year = 2006 OR year IS NULL) ORDER BY dept_id;   


Using that query shows the exact result I posted with data for month 2 and year 2006 above. However, the problem arises when I want to show something like month 4 and year 2006.

That same query produces a result like:

Code:
dept_id | title            | month | year | revenue | expenses
--------+------------------+-------+------+---------+----------
2222    | Accounting       |       |      |         |
3333    | Customer Service |       |      |         |


Which, I'm assuming, is because there is no month = 4 or month is null in the finances table for Human Resources, so it doesn't show up.

I'm looking for is some help in constructing a better query. Am I at least on the right track? Any help would be greatly appreciated.

Reply With Quote
  #2  
Old September 7th, 2006, 07:13 AM
Adrastea0413's Avatar
Adrastea0413 Adrastea0413 is offline
Back after 2 years!
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2003
Location: Washington, DC Metro
Posts: 1,747 Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level)Adrastea0413 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 23 h 43 m 8 sec
Reputation Power: 884
Facebook
Ok, I got it working. I'm going to post the solution here so others can see if they have similar problems.

I ended up using a subquery to get the desired results.

It's Oracle-specific:
sql Code:
Original - sql Code
  1. SELECT A.dept_id, title, month, year, revenue, expenses FROM departments A, (SELECT dept_id, month, year, revenue, expenses FROM finances WHERE month=4 AND year=2006) B WHERE A.dept_id = B.dept_id (+) ORDER BY A.dept_id

Reply With Quote
  #3  
Old September 7th, 2006, 11:35 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
glad you solved it (although i would be leery of using that old deprecated (+) way of writing the outer join)

for next time, all you need to do is move the WHERE conditions to the ON clause --
Code:
select departments.dept_id
     , finances.title
     , finances.month
     , finances.year
     , finances.revenue
     , finances.expenses 
  from departments 
left 
  join finances 
    on departments.dept_id = finances.dept_id 
   and finances.month = 2 
   and finances.year = 2006 
order 
    by dept_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Combination of left outer join and where clause


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap