If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > tough query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-04, 15:33
DC Ross DC Ross is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
Question tough query help

Hey all,
I have a query that I need some help with. Here's what I have, so far it's pretty basic, I'll explain what I need...

Code:
SELECT t.TaskID, t.IWOwner, t.Title, p.ProductName, r.RegionName, c.ChannelName
FROM Tasks t
LEFT JOIN TaskProduct tp
	ON t.TaskID = tp.TaskID
LEFT JOIN Products p
	ON tp.ProductID = p.ProductID
LEFT JOIN TaskRegion tr
	ON t.TaskID = tr.TaskID
LEFT JOIN Regions r
	ON tr.RegionID = r.RegionID
LEFT JOIN TaskChannel tc
	ON t.TaskID = tc.TaskID
LEFT JOIN Channels c
	ON tc.ChannelID = c.ChannelID
Here's a snippet of the results that this is returning:

Code:
1000    guy1    Testing 123     InfoPath      Japan      Volume Licensing
1000    guy1    Testing 123     Office Pro    US ONLY    Volume Licensing
1001    guy2    Testing 1234    Access        Europe     Retail FPP
1001    guy2    Testing 1234    FrontPage     Europe     Retail FPP
1003    guy3    Testing 321     Office Pro    NULL       NULL
What I need to return is a bit more involved than this. I would like one result per TaskID, but if there is more than one ProductID, RegionID and/or ChannelID, instead of listing each one as a result, I need it to say "Multiple", otherwise, if there is only one ProductID, RegionID and/or ChannelID, I need it to return the corresponding name. To clarify here's the result set I would like returned:

Code:
1000    guy1    Testing 123    Multiple      Multiple    Volume Licensing
1001    guy2    Testing 1234   Multiple      Europe      Retail FPP
1003    guy3    Testing 321    Office Pro    NULL        NULL
I apologize if this a bit convoluted, let me know if you need clarification.

Thanks in advance,
-DC Ross
Reply With Quote
  #2 (permalink)  
Old 07-28-04, 06:46
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Try ths:
Code:
SELECT t.TaskID, t.IWOwner, t.Title,
       CASE WHEN (COUNT DISTINCT p.productName) > 1 THEN 'Multiple'
            ELSE MIN(p.productName) END product,
       CASE WHEN (COUNT DISTINCT r.regionName) > 1 THEN 'Multiple'
            ELSE MIN(r.regionName) END region,
       CASE WHEN (COUNT DISTINCT c.channelName) > 1 THEN 'Multiple'
            ELSE MIN(c.channelName) END channel
FROM Tasks t
LEFT JOIN TaskProduct tp
	ON t.TaskID = tp.TaskID
LEFT JOIN Products p
	ON tp.ProductID = p.ProductID
LEFT JOIN TaskRegion tr
	ON t.TaskID = tr.TaskID
LEFT JOIN Regions r
	ON tr.RegionID = r.RegionID
LEFT JOIN TaskChannel tc
	ON t.TaskID = tc.TaskID
LEFT JOIN Channels c
	ON tc.ChannelID = c.ChannelID
GROUP BY t.TaskID, t.IWOwner, t.Title;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 07-28-04, 13:29
DC Ross DC Ross is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks Tony. You're my hero
It worked with one modification: ...CASE WHEN COUNT (DISTINCT...)

Thanks again!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On