SQL Server view

Not sure if this is "so-able."

I have three tables with sales information in them.

Each table has a "cust_no" field, year, sales.

How do I write a view that has
Customer          TY Sales      SalesYR-1   SalesYR-2
1                          200               100                  0
2                             0                   200              0
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gnarCommented:
What about just joining the tables on that cust_no field?

Something like:




SELECT a.cust_no, a.sales, b.sales, c.sales
FROM tablea a
JOIN tableb b on a.cust_no = b.cust_no
JOIN tablec c on b.cust_no = c.cust_no

Open in new window

Larry Bristersr. DeveloperAuthor Commented:
gnar:
The cust_no doesn't exist in all tables.  I need have a column that has all the cust_no's whatever table they're in and then place a zero dollars in the sales part where they don't exist.
ee_rleeCommented:
try this
SELECT isnull(a.cust_no,isnull(b.cust_no,c.cust_no)) as custno, 
             isnull(a.sales,0) as salesa, 
             isnull(b.sales,0) as salesb, 
             isnull(c.sales,0) as salesc
FROM tablea a
FULL OUTER JOIN tableb b on a.cust_no = b.cust_no
FULL OUTER JOIN tablec c on (b.cust_no = c.cust_no AND b.cust_no is not null) OR 
                                               (a.cust_no = c.cust_no AND a.cust_no is not null)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
WOW!  Excellent script.  Thanks and I'm on my way!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.