Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
Please help me out in the following situation. i dont understand what type of query used to get result.

I have 4 tables and i want user_name on the basis on IncidentID. But Username is belongs to a different table and the fields which relate them also present in different tables. But All tables belongs to same DB.

table_incident.Id
table_incident.Entitydimkey

table_workitemdim.Entitydimkey
table_workitemdim.workitemdimkey

table_workitemassign.workitemdimkey
table_workitemassign.workitemassign_userdimkey

table_userdim.userdimkey
table_userdim.firstname

Please tell me urgent what should i do..

Thanks in advance
Regards
M Ahsan Misbah
Posted

Select firstname from table_userdim inner join
table_incident on (table_userdim.userdimkey = table_incident.entitydimkey)inner join
table_workitemdim on (table_incident.entitydimkey = table_workitemdim.Entitydimkey) inner join
table_workitemassign on
(table_workitemdim.Entitydimkey = table_workitemassign.workitemassign_userdimkey)
where
table_incident.id = @id


Or you can also achieve this to change in this way ....


Select firstname from table_userdim inner join
table_workitemassign
on (table_userdim.userdimkey = table_workitemassign.workitemassign_userdimkey)inner join
table_workitemdim on (ttable_workitemassign.workitemdimkey = table_workitemdim .entitydimkey)inner join
table_incident on (table_incident.entitydimkey = table_workitemdim.Entitydimkey)

where
table_incident.id = @id
 
Share this answer
 
Hi,
try below query
SQL
--this query will give first name of the record with id = 10
SELECT t4.firstname 
FROM table_incident AS t1 
INNER JOIN table_workitemdim AS t2 ON t2.Entitydimkey = t1.Entitydimkey 
INNER JOIN table_workitemassign as t3 ON t3.workitemdimkey = t2.workitemdimkey
INNER JOIN table_userdim as t4 ON t4.userdimkey = t3.workitemassign_userdimkey
WHERE t1.ID = 10


hope it helps
 
Share this answer
 
Your schema needs some attention ... it's not clear what table is Entitydimkey supposed to "point" to.

You will need to do some research on Foreign Keys in tables.
Once you've grasped that concept you will need to do some further research on SQL JOINS.

As an example I would have a foreign key (FK) to the user table on the incident table so that I could easily determine which user raised the incident. I would then query along these lines...
SQL
SELECT * FROM table_incident I
INNER JOIN table_userdim U on I.userdimkey=U.userdimkey

As an aside, it's not considered good form to prefix your tables with the word "table"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900