Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2004, 07:29 AM
Registered User
 
Join Date: Jun 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default outer join on same table

I like to select all Countries where customertype = 'Consumer', together with all months available. So I like something like this:

France Januari
France Februari
null March
null April
UK Januari
etc..

I use only one table, so I have to make a "self"-join on this using the primary key "guide". I thought I could use a outer join (FULL to make it easy) like this:

SELECT a.Country, b.Month
FROM Data a FULL OUTER JOIN Data b
ON a.guide = b.guide
WHERE a.CustomerType = 'Consumer'

But this gives me only the months where Customertype = 'Consumer' !!

With a FULL outer join I would expect:
France null
France Januari
null Februari

I guess that I can't use the primary key, because it is non existent for those countries that have no month. How else can I solve this?



 
Old September 15th, 2004, 09:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
SELECT Country, Month
FROM Data
WHERE CustomerType = 'Consumer'
ORDER BY Country
Does this help? Is this what you are looking for?

Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old September 15th, 2004, 09:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Roog,

It is enough that you post it once at one forum that best fit the subject of the post.

Cross post - http://p2p.wrox.com/topic.asp?TOPIC_ID=19198

_________________________
- Vijay G
Strive for Perfection
 
Old September 16th, 2004, 02:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Perhaps I've misunderstood, do you need a join at all?
Code:
SELECT CASE WHEN CustomerType = 'Customer' THEN Country ELSE NULL END AS Country, [Month] AS Month
FROM Data
--

Joe
 
Old September 30th, 2004, 05:31 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

Looking to the output required, I feel a GROUP BY on COUNTRY,MONTH will worK with ROLLUP.
----------------------
Select country,month from data
where customertype='CONSUMER'
group by country,month with ROLLUP
order by country,month
----------------------

With Rollup can be eliminated if u don't want the Group summery extra rows.




B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't figure out an OUTER JOIN statement dhsmd SQL Language 5 September 18th, 2008 01:02 AM
Non-ANSI outer join operators Jeff Kanning SQL Server 2005 14 September 9th, 2008 08:02 PM
left outer join keyvanjan Classic ASP Basics 1 April 15th, 2006 05:37 AM
left outer join keyvanjan Classic ASP Professional 0 February 5th, 2006 11:54 AM
Outer Join, 2 columns jking SQL Language 1 December 5th, 2004 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.