Joining Tables Problem

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 2
Reputation: saisankar_p is an unknown quantity at this point 
Solved Threads: 0
saisankar_p saisankar_p is offline Offline
Newbie Poster

Joining Tables Problem

 
0
  #1
Jul 4th, 2008
Hi all,

I have a problem achieving the result. I want data as below which is based on 4 tables

Expected rasult

SiteID SiteName State PostCode Phone1 StateName CanDelete
---------------------------------------------------------------------------------
1 Site1 1 2134 124 123 - ADC 1
2 Site2 3 2323 124 234 - QTY 0
3 Site3 5 7666 124 645 - LDG 0
4 Site4 2 2888 124 894 - WA 1


And the tables are as below

tbl_site

SiteID SiteName State PostCode Phone1
-------------------------------------------------------
1 Site1 1 2134 124 123
2 Site2 3 2323 124 234
3 Site3 5 7666 124 645
4 Site4 2 2888 124 894

tbl_state

StateID StateName
------------------------
1 ADC
2 WA
3 QTY
4 RKH
5 LDG


tbl_trial_site

TrialID SiteID
---------------------
1 1


tbl_user_site

UserID SiteID
---------------------
1 4


And the senario is the CanDelete column should be 1 if SiteID has references in any of the 2 tables (tbl_trial_site, tbl_user_site) it should be zero if non of the 2 tables has a reference to SiteID

Can any one please help me as i'm new to SQL and in greate need for help.

Thank you for your valuable time.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Joining Tables Problem

 
0
  #2
Jul 4th, 2008
Hello

If your tables are properly designed simple inner joins will solve your problem:

select * tbl_site join tbl_state join tbl_trial_site join tbl_user_site.

Ok, this result set will contain duplicate columns. To discard them you must replace the asterisk
by the list of column names you want to get.

Unfortunately, almost all tables aren't properly designed (because usually amateurs do table design never thinking of how to create functioning sql select statements) therefore some join constraints must be added to the above select statement.

btw, why did you put tbl_ to each object name? Isn't it kind of pleonasm 'cause everybody knows such objects can only be tables.

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Joining Tables Problem

 
0
  #3
Jul 6th, 2008
Hello saisankar_p

Because of not having completely read your perfectly written post from begin to end, I presumed canDelete would already be part of one of your tables. I have to apologize to you for that bad mistake. Finally I have understand that you will compute canDelete depending whether tables tbl_trial_site or tbl_user_site have entries for the associate SiteID.

I have got this solution (it is easy to find, if one considers your complete question :
  1. SELECT s.SiteID, SiteName, a.StateID, PostCode, Phone1, '-' as '', StateName,
  2. CASE
  3. WHEN (SELECT t.SiteID FROM tbl_trial_site t WHERE t.SiteID = s.SiteID)
  4. IS NOT NULL THEN 1
  5. WHEN (SELECT t.SiteID FROM tbl_user_site t WHERE t.SiteID = s.SiteID)
  6. IS NOT NULL THEN 1
  7. ELSE 0
  8. END AS canDelete
  9. FROM tbl_site s JOIN tbl_state a
  10.  
  11. /* result
  12. SiteID SiteName StateID PostCode Phone1 StateName canDelete
  13. -----------------------------------------------------------------------------------
  14. 1 Site1 1 2134 124 123 - ADC 1
  15. 2 Site2 3 2323 124 234 - QTY 0
  16. 3 Site3 5 7666 124 645 - LDG 0
  17. 4 Site4 2 2888 124 894 - WA 1
  18. */
This select statement works well on Sybase database what is the "first mother" of MS sql server.
Pls tell me whether it also works on MS sql server (There is a further solution dealing with it-then-else clause.)

tesu
Last edited by tesuji; Jul 6th, 2008 at 7:18 am.
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Joining Tables Problem

 
0
  #4
Jul 6th, 2008
Hello saisankar_p

Just another solution:
  1. SELECT s.SiteID, SiteName, s.StateID, PostCode, Phone1, StateName,
  2. 1 as canDelete
  3. FROM tbl_site s, tbl_state a, tbl_user_site u, tbl_trial_site t
  4. WHERE s.StateID = a.StateID
  5. AND (s.SiteID = u.SiteID OR s.SiteID = t.SiteID)
  6. UNION
  7. SELECT s.SiteID, SiteName, s.StateID, PostCode, Phone1, StateName,
  8. 0 as canDelete
  9. FROM tbl_site s, tbl_state a, tbl_user_site u, tbl_trial_site t
  10. WHERE s.StateID = a.StateID
  11. AND (NOT(s.SiteID = u.SiteID OR s.SiteID = t.SiteID))
  12. ORDER BY 1 -- ordered by s.siteID
  13. -- order clause of union requires that columns be specified by their
  14. -- positions from in the result set.
If you only want to know which rows could be deleted, then use first part only:
  1. SELECT s.SiteID, SiteName, s.StateID, PostCode, Phone1, StateName,
  2. 1 as canDelete
  3. FROM tbl_site s, tbl_state a, tbl_user_site u, tbl_trial_site t
  4. WHERE s.StateID = a.StateID
  5. AND (s.SiteID = u.SiteID OR s.SiteID = t.SiteID)


tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2
Reputation: saisankar_p is an unknown quantity at this point 
Solved Threads: 0
saisankar_p saisankar_p is offline Offline
Newbie Poster

Re: Joining Tables Problem

 
0
  #5
Jul 11th, 2008
Hi tesuji,

Thanks for the reply. It is working perfectly as i needed. Sorry for the delay in reply, i was busy with some other project.

Regards, sankar
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC