| | |
Joining Tables Problem
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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
:
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
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
: sql Syntax (Toggle Plain Text)
SELECT s.SiteID, SiteName, a.StateID, PostCode, Phone1, '-' as '', StateName, CASE WHEN (SELECT t.SiteID FROM tbl_trial_site t WHERE t.SiteID = s.SiteID) IS NOT NULL THEN 1 WHEN (SELECT t.SiteID FROM tbl_user_site t WHERE t.SiteID = s.SiteID) IS NOT NULL THEN 1 ELSE 0 END AS canDelete FROM tbl_site s JOIN tbl_state a /* result SiteID SiteName StateID 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 */
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Hello saisankar_p
Just another solution:
If you only want to know which rows could be deleted, then use first part only:
tesu
Just another solution:
sql Syntax (Toggle Plain Text)
SELECT s.SiteID, SiteName, s.StateID, PostCode, Phone1, StateName, 1 as canDelete FROM tbl_site s, tbl_state a, tbl_user_site u, tbl_trial_site t WHERE s.StateID = a.StateID AND (s.SiteID = u.SiteID OR s.SiteID = t.SiteID) UNION SELECT s.SiteID, SiteName, s.StateID, PostCode, Phone1, StateName, 0 as canDelete FROM tbl_site s, tbl_state a, tbl_user_site u, tbl_trial_site t WHERE s.StateID = a.StateID AND (NOT(s.SiteID = u.SiteID OR s.SiteID = t.SiteID)) ORDER BY 1 -- ordered by s.siteID -- order clause of union requires that columns be specified by their -- positions from in the result set.
sql Syntax (Toggle Plain Text)
SELECT s.SiteID, SiteName, s.StateID, PostCode, Phone1, StateName, 1 as canDelete FROM tbl_site s, tbl_state a, tbl_user_site u, tbl_trial_site t WHERE s.StateID = a.StateID 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.
![]() |
Similar Threads
- How can we do the Pagination of Search Results. (PHP)
- want to retreave data from tables after joining them using "join". (PHP)
- MS Access 2000 - Inserting records in a new table by joining input from two tables (Visual Basic 4 / 5 / 6)
- How to change the currect recordset in a Form (Visual Basic 4 / 5 / 6)
- dataabse connectin string problem? (MySQL)
- Need Help Joining 3 tables (MySQL)
- Problem with Rewriting Subqueries as Joins (MS SQL)
Other Threads in the MS SQL Forum
- Previous Thread: Delete records from a table
- Next Thread: Incorrect syntax near '='
| Thread Tools | Search this Thread |





