Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I love this site! It's so nice to know that there are so many people out there who are willing to share their knowledge..."

Geography

Where in the world do Tek-Tips members come from?

how to populate values when "not found" occurs

klkuab (IS/IT--Management)
29 Dec 08 9:50
I have 2 tables (table-A & table-B). They are defined as such:

TABLE-A

DEPARTMENT
ACCOUNT

TABLE-B

DEPARTMENT
ACCOUNT
SPEND
BUDGET

TABLE-A is my driver for my query as it has all the departments and accounts, whereas TABLE-B only has records for the departments and accounts that have either a spend or a budget amount associated with it.

I need to report on all department / accounts regardless if there is a spend or budget amount, thus I know I need to do an outer join such as:

select a.department, a.account, b.spend, b.budget
from table-a a LEFT OUTER JOIN table-b b on
a.department = b.department and
a.account = b.account

my problem is HOW DO I INSERT VALUES of zero FOR SPEND & BUDGET when there isn't a record in table-b ???

many many thanks!
PHV (MIS)
29 Dec 08 10:06
SELECT a.department, a.account
, CASE WHEN b.spend IS NULL THEN 0 ELSE b.spend END
, CASE WHEN b.budget IS NULL THEN 0 ELSE b.budget END
FROM table-a a LEFT OUTER JOIN table-b b ON
a.department = b.department AND
a.account = b.account

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

LKBrwnDBA (MIS)
29 Dec 08 10:07

Depending on the LANGUAGE you are using, check out the ISNULL(), NVL(), ... or similar function(s) that return a value(like '0') when the value of the column does not exist.
3eyes

 

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

PHV (MIS)
29 Dec 08 10:34
Another shorter (but still ANSI compliant) way:
SELECT a.department, a.account, COALESCE(b.spend,0), COALESCE(b.budget,0)
FROM table-a a LEFT OUTER JOIN table-b b ON
a.department = b.department AND
a.account = b.account

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

JarlH (Programmer)
29 Dec 08 10:35
An ANSI SQL short form of the CASE expression is COALESCE.

COALESCE(b.spend, 0) means if b.spend is not null then return b.spend, else return 0.

I.e. the COALESCE expression returns the value of the first non-NULL operand, found by working from left to right, or NULL if all the operands equal NULL.
 
r937 (TechnicalUser)
29 Dec 08 14:08
by the way, table-a is not a valid identifier

if your table names really have dashes in dem, you have to excape dem

"table-a" in ANSI SQL, [table-a] in SQL Server, `table-a` in MySQL, eck settera

winky smile

r937.com | rudy.ca

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close