•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Rep Power: 0
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: 295
Reputation:
Rep Power: 1
Solved Threads: 41
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: 295
Reputation:
Rep Power: 1
Solved Threads: 41
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 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.
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
Hello
Just another solution:
If you only want to know which rows could be deleted, then use first part:
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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 '='


Linear Mode