MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

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:
VeriSign Code Signing Digital Certificates provides assurance to end users. Read about this and more in the free white paper: “How to Digitally Sign Downloadable Code for Secure Content Transfer.” Learn More!
  #1  
Old February 17th, 2004, 05:55 PM
mpearce's Avatar
mpearce mpearce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: NewHampshire, USA
Posts: 416 mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 30 m 44 sec
Reputation Power: 9
Send a message via AIM to mpearce
Question Help with query. Joins? Unions?

Hi,
I have a few tables that I want to query and I need the output back in a certain way.

This is an example of my problem.

Users Table:
ID, UserName
1 , Fred
2 , Bill

DVD Table:
ID, DVDName
1 , XMEN
2 , Terminiator 2
3 , MontyPhthon Holy Grail

VHS Table:
ID, VHSName
1 , Terminator
2 , StarWars

DVD Users table:
ID, UserID, DVD_ID
1 , 1 , 1
2 , 1 , 3
3 , 2 , 1
4 , 2 , 2

VHS Users Table:
ID, UserID, VHS_ID
1 , 2 , 1
3 , 1 , 2

So, each person might 0 or more DVDs and 0 or more VHS.
What I need to output is a flat table something like this.

User, DVD , VHS
Fred, XMEN , <null>
Fred, MontyPhthon Holy Grail , <null>
Fred, <null> , StarWars
Bill , XMEN , <null>
Bill , Terminator 2 , <null>
Bill , <null> , Terminator

What I am getting is something like.
User, DVD , VHS
Fred, XMEN , StarWars
Fred, MontyPhthon Holy Grail , StarWars
Bill , XMEN , Terminator
Bill , Terminator 2 , Terminator

Any help would be apreciated.

Thanks,
__________________
Mark Pearce

Reply With Quote
  #2  
Old February 18th, 2004, 07:55 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,765 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 9 m 28 sec
Reputation Power: 870
Code:
select UserName
     , DVDName as DVD
     , null    as VHS
  from Users
inner
  join DVDUsers
    on Users.ID = DVDUsers.UserID
inner
  join DVD
    on DVDUsers.DVD_ID = DVD.ID
union all
select UserName
     , null 
     , VHSName
  from Users
inner
  join VHSUsers
    on Users.ID = VHSUsers.UserID
inner
  join VHS
    on VHSUsers.VHS_ID = VHS.ID
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old February 18th, 2004, 09:52 AM
mpearce's Avatar
mpearce mpearce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: NewHampshire, USA
Posts: 416 mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 30 m 44 sec
Reputation Power: 9
Send a message via AIM to mpearce
Hi,
That is great.. The only problem I have now, is that I need it to be grouped by name. I know this may be an issue using the UNION command, and understand if it's just not possible.

Using the amazing query that you gave me, I get the following.
PHP Code:
 Fred   XMEN  
Fred   Monty Python 
Holy Grail  
Bill   XMEN  
Bill   Terminator 2  
Fred                                         Terminator 
Bill                                         StarWars 

I would like it to look like this.
PHP Code:
 Fred   XMEN  
Fred   Monty Python 
Holy Grail
Fred                                         Terminator  
Bill   XMEN  
Bill   Terminator 2  
Bill                                         StarWars 


Thanks for your help.
Mark Pearce

Reply With Quote
  #4  
Old February 18th, 2004, 09:57 AM
mpearce's Avatar
mpearce mpearce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: NewHampshire, USA
Posts: 416 mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 30 m 44 sec
Reputation Power: 9
Send a message via AIM to mpearce
Forget it... I just stuck "order by Name ,DVD ,VHS" on the end and it works fine. I thought I had read somewhere that once you do a UNION, you can't do any other sorting or conditional statements.

Again, thank you very very very much.

You are the Master..

Guess that's why you have almost 2000 posts.

Reply With Quote
  #5  
Old February 18th, 2004, 10:39 AM
mpearce's Avatar
mpearce mpearce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: NewHampshire, USA
Posts: 416 mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 30 m 44 sec
Reputation Power: 9
Send a message via AIM to mpearce
More complexity...

Sorry to bother you again, but I was just wondering if it is possible to add a bit more complexity to this situation.

Lets say you wanted to get this report to include at a higher level, the store and state of the store.

So, you would then have..

PHP Code:
 State  Store      User   DVD                                   VHS
MA     Littleton  Fred   XMEN  
MA     Littleton  Fred   Monty Python 
Holy Grail
MA     Littleton  Fred                                         Terminator  
NH     Keene      Bill   XMEN  
NH     Keene      Bill   Terminator 2  
NH     Keene      Bill                                         StarWars 

So, I added a State table, a Store table and a StateID and StoreID fields to the users table.
State table:
ID, State

Store Table:
ID, Store

Users Table:
ID, Name, StateID, StoreID
Would this be possible? I do have a reason for asking, I'm not just trying to be a pain in the butt..

I actually already have this outputting the way I want, but I did it using nested loops and nested DB connections and that is not a good way to do this. But, trying to turn it into a flat table is not being much fun either.

Last edited by mpearce : February 18th, 2004 at 10:54 AM.

Reply With Quote
  #6  
Old February 18th, 2004, 11:22 AM
saodl saodl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Los Angeles, CA
Posts: 21 saodl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If I may...

First, the "State Code" (MA, CA, etc) is already a Primary Key, so you don't need a table just to generate a Key to represent that. You are only making more work for yourself. You only need a "State" table if you want to make sure (via foriegn key) that only existing States are entered in any other table, and/or if you need to store more information about the State (Full state name, state flower, state capital...). Instead store State as an attribute of Store.

Store Table:
ID, Store, StateCode

Then, from Rudy's query:

Code:
select StateCode
     , Store
     , UserName
     , DVDName as DVD
     , null    as VHS
  from Users
inner
  join Store
    on Store.ID = Users.StoreID
inner
  join DVDUsers
    on Users.ID = DVDUsers.UserID
inner
  join DVD
    on DVDUsers.DVD_ID = DVD.ID
union all
select StateCode
     , Store
     , UserName
     , null 
     , VHSName
  from Users
inner
  join Store
    on Store.ID = Users.StoreID
inner
  join VHSUsers
    on Users.ID = VHSUsers.UserID
inner
  join VHS
    on VHSUsers.VHS_ID = VHS.ID

Reply With Quote
  #7  
Old February 18th, 2004, 02:12 PM
mpearce's Avatar
mpearce mpearce is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: NewHampshire, USA
Posts: 416 mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level)mpearce User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 30 m 44 sec
Reputation Power: 9
Send a message via AIM to mpearce
OK, I understand what you are saying about the state table. The video stuff is just an example of what am doing, so my actual data doesn't have a State table. I started with the VHS/DVD thing just to have something quick to throw up here. Thankfully, you were very helpful, so I decided to ask about the next step. I had to make up some tables to go with the DVD/VHS thing and I just picked State and Store out of the air.

Anyway, I think I am all set now, your examples were great and were able to show me what I had to do.

I used the MS SQL Query builder thingy to help me with the sub queries and then just stuck them together with a Union All as you showed me. Can you just take one more quick look at my final query and tell me if it looks ok. Should both halfs of the union look the same?

Thank you very much for your help.

Code:
SELECT  State
         , Store
         , Name
         , NULL AS DVD
         , VHSName AS VHS
   FROM  vhs 
INNER 
  JOIN vhsusers 
    ON vhs.ID = vhsusers.VHS_ID
INNER
  JOIN users 
INNER 
  JOIN State 
    ON users.StateID = State.ID
INNER 
  JOIN Store 
    ON users.StoreID = Store.ID 
    ON vhsusers.UserID = users.ID
union all
SELECT  State
          , Store
          , users.Name
          , dvd.DVDName AS DVD
          , NULL AS VHS
    FROM users
INNER
  JOIN State 
    ON users.StateID = State.ID
INNER
  JOIN dvdusers 
    ON users.ID = dvdusers.UserID
INNER
  JOIN dvd 
    ON dvdusers.DVD_ID = dvd.ID
order by State, Store, name, dvd, vhs

Reply With Quote
  #8  
Old February 18th, 2004, 04:16 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,765 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 9 m 28 sec
Reputation Power: 870
the ON conditions in the first subquery are wrong

the second subquery looks okay

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Help with query. Joins? Unions?


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway