User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 427,225 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,221 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 608 | Replies: 4
Reply
Join Date: Jul 2008
Posts: 2
Reputation: saisankar_p is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
saisankar_p saisankar_p is offline Offline
Newbie Poster

Joining Tables Problem

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Joining Tables Problem

  #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  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Joining Tables Problem

  #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 6: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  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Joining Tables Problem

  #4  
Jul 6th, 2008
Hello

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:
  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  
Join Date: Jul 2008
Posts: 2
Reputation: saisankar_p is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
saisankar_p saisankar_p is offline Offline
Newbie Poster

Re: Joining Tables Problem

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 11:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC