João serras-pereira
asked on
Select values from two tables logic help
Hello Experts,
I have searched for a solution before posting, couldnt find a conclusive answer so i am posting for help. Do apologise me if this question has already been answered and i missed it.
I have two tables with the following design:
Table VF_ConceptInfo
CID(int,identity,pk) ConceptName(str) ConceptDescription(str)
1 Concept1 this is concept one description
2 Concept2 this is concept two description
Table VF_ConceptSynonymInfo
_recID(int,identity,pk) CID(int) ConceptSynonymID(int)
1 2 23
2 2 11
So, table 1 contains all the concepts, table two is a relation table between concepts, that reflects their synonyms. So, on the above example, Concept2 Has two Synonyms, concept23 and concept11
this is how far i've gotten with what i want:
SELECT VF_ConceptInfo.CID, VF_ConceptInfo.ConceptName , VF_ConceptSynonymInfo.Conc eptSynonym ID
FROM VF_ConceptSynonymInfo INNER JOIN
VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID
and the resulting table is:
CID ConceptName ConceptSynonymID
2 Concept2 11
2 concept2 23
What i really wanted it to display is the following table:
CID ConceptName ConceptSynonymID "SynonymName"
2 Concept2 11 concept11
2 concept2 23 concept23
I am lost on how to acomplish this, stuck in a logic loophole i cant solve.
Is this Database design problem?
Thanks in advance
I have searched for a solution before posting, couldnt find a conclusive answer so i am posting for help. Do apologise me if this question has already been answered and i missed it.
I have two tables with the following design:
Table VF_ConceptInfo
CID(int,identity,pk) ConceptName(str) ConceptDescription(str)
1 Concept1 this is concept one description
2 Concept2 this is concept two description
Table VF_ConceptSynonymInfo
_recID(int,identity,pk) CID(int) ConceptSynonymID(int)
1 2 23
2 2 11
So, table 1 contains all the concepts, table two is a relation table between concepts, that reflects their synonyms. So, on the above example, Concept2 Has two Synonyms, concept23 and concept11
this is how far i've gotten with what i want:
SELECT VF_ConceptInfo.CID, VF_ConceptInfo.ConceptName
FROM VF_ConceptSynonymInfo INNER JOIN
VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID
and the resulting table is:
CID ConceptName ConceptSynonymID
2 Concept2 11
2 concept2 23
What i really wanted it to display is the following table:
CID ConceptName ConceptSynonymID "SynonymName"
2 Concept2 11 concept11
2 concept2 23 concept23
I am lost on how to acomplish this, stuck in a logic loophole i cant solve.
Is this Database design problem?
Thanks in advance
try:-
select CID, ConceptName, ConceptSynonymID,'concept' + ConceptSynonymID
as SynonymName from
(SELECT VF_ConceptInfo.CID, VF_ConceptInfo.ConceptName, VF_ConceptSynonymInfo.ConceptSynonymID
FROM VF_ConceptSynonymInfo INNER JOIN
VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID) derived_table
select ci.CID, ci.ConceptName, csi.ConceptSynonymID
, 'concept' + cast(csi.ConceptSynonymID as varchar(4)) as SynonymName
from Table VF_ConceptInfo ci
inner join VF_ConceptSynonymInfo csi on csi.CID = ci.CID
, 'concept' + cast(csi.ConceptSynonymID as varchar(4)) as SynonymName
from Table VF_ConceptInfo ci
inner join VF_ConceptSynonymInfo csi on csi.CID = ci.CID
You haven't shown the 3rd table that contains the synonym data however this is the relationship as I understand it.
VF_ConceptInfo -----------> VF_ConceptSynonymInfo <---------------- VF_SynonymInfo
Your query should look like this:
SELECT
VF_ConceptInfo.CID,
VF_ConceptInfo.ConceptName ,
VF_ConceptSynonymInfo.Conc eptSynonym ID,
VF_SynonymInfo.SynonymName
FROM VF_ConceptSynonymInfo
INNER JOIN VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID
INNER JOIN VF_SynonymInfo ON VF_ConceptSynonymInfo.Conc eptSynonym ID = VF_SynonymInfo.SID
DaTribe
VF_ConceptInfo -----------> VF_ConceptSynonymInfo <---------------- VF_SynonymInfo
Your query should look like this:
SELECT
VF_ConceptInfo.CID,
VF_ConceptInfo.ConceptName
VF_ConceptSynonymInfo.Conc
VF_SynonymInfo.SynonymName
FROM VF_ConceptSynonymInfo
INNER JOIN VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID
INNER JOIN VF_SynonymInfo ON VF_ConceptSynonymInfo.Conc
DaTribe
better:-
SELECT VF_ConceptInfo.CID, VF_ConceptInfo.ConceptName, VF_ConceptSynonymInfo.ConceptSynonymID, 'Concept' + VF_ConceptSynonymInfo.ConceptSynonymID as SynonymName
FROM VF_ConceptSynonymInfo INNER JOIN
VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID
Doing some mental SQL here. The above query may need slight restucturing.
DaTribe
DaTribe
SELECT
VF_ConceptInfo.CID,
VF_ConceptInfo.ConceptName,
VF_ConceptSynonymInfo.ConceptSynonymID,
VF_SynonymInfo.SynonymName
FROM VF_ConceptInfo
INNER JOIN VF_ConceptSynonymInfo ON VF_ConceptInfo.CID = VF_ConceptSynonymInfo.CID
INNER JOIN VF_SynonymInfo ON VF_ConceptSynonymInfo.ConceptSynonymID = VF_SynonymInfo.SID
ASKER
Not quite there yet.
@ mayankagarwal - im getting a CONCAT error when runing your query - 'CONCAT' is not a recognized built-in function name.
Im using VS 2010 query builder, and SQL server 2008.
@ mayank_joshi - Conversion failed when converting the varchar value 'concept' to data type int.
@ mkobrin - query runs great, and its really close to what i intend to have, but it might be my database design problem, or my explanation that wasnt quite .. well.. explanatory.
@ DaTribe - From seeing your reply, im "concluding" it really might be my design, cause what i dont have a 3rd table.
Let me try and put some real data onscreen:
Table VF_ConceptInfo
CID(int,identity,pk) ConceptName(str) ConceptDescription(str)
1 Test Description
2 Soccer Description
3 Ocean Description
4 Sea Description
5 Quizz Description
6 Football Description
7 Exam Description
Table VF_ConceptSynonymInfo
_recID(int,identity,pk) CID(int) ConceptSynonymID(int)
1 1 5
2 1 7
3 2 6
4 3 4
What i wanted it to display is the following table:
CID ConceptName ConceptSynonymID "SynonymName"
1 Test 5 Quizz
2 Test 7 Exam
etc..
Do i need a SynInfo table with the synonim names? cause the sunonyms will be included in the concept table as well, as they're concepts as well
Thanks for all the answers so far
@ mayankagarwal - im getting a CONCAT error when runing your query - 'CONCAT' is not a recognized built-in function name.
Im using VS 2010 query builder, and SQL server 2008.
@ mayank_joshi - Conversion failed when converting the varchar value 'concept' to data type int.
@ mkobrin - query runs great, and its really close to what i intend to have, but it might be my database design problem, or my explanation that wasnt quite .. well.. explanatory.
@ DaTribe - From seeing your reply, im "concluding" it really might be my design, cause what i dont have a 3rd table.
Let me try and put some real data onscreen:
Table VF_ConceptInfo
CID(int,identity,pk) ConceptName(str) ConceptDescription(str)
1 Test Description
2 Soccer Description
3 Ocean Description
4 Sea Description
5 Quizz Description
6 Football Description
7 Exam Description
Table VF_ConceptSynonymInfo
_recID(int,identity,pk) CID(int) ConceptSynonymID(int)
1 1 5
2 1 7
3 2 6
4 3 4
What i wanted it to display is the following table:
CID ConceptName ConceptSynonymID "SynonymName"
1 Test 5 Quizz
2 Test 7 Exam
etc..
Do i need a SynInfo table with the synonim names? cause the sunonyms will be included in the concept table as well, as they're concepts as well
Thanks for all the answers so far
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select ci.CID, ci.ConceptName, csi.ConceptSynonymID, (select ConceptName from VF_ConceptInfo where CID = csi.CID) as SynonymName
from Table VF_ConceptInfo ci
inner join VF_ConceptSynonymInfo csi on csi.CID = ci.CID
from Table VF_ConceptInfo ci
inner join VF_ConceptSynonymInfo csi on csi.CID = ci.CID
try this:-
SELECT VF_ConceptInfo.CID, VF_ConceptInfo.ConceptName, VF_ConceptSynonymInfo.ConceptSynonymID, 'Concept' +cast(VF_ConceptSynonymInfo.ConceptSynonymID as NVARCHAR(100)) as SynonymName
FROM VF_ConceptSynonymInfo INNER JOIN
VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID
ASKER
Thanks all, mayankagarwal hit the nail on the head.
SELECT VF_ConceptInfo.CID, VF_ConceptInfo.ConceptName
FROM VF_ConceptSynonymInfo INNER JOIN
VF_ConceptInfo ON VF_ConceptSynonymInfo.CID = VF_ConceptInfo.CID