Link to home
Start Free TrialLog in
Avatar of mattfox77
mattfox77Flag for United States of America

asked on

Need a SQL subquery to compile data

Im very new to subqueries.
I have 2 queries that I need to combine.
I have a database with User Tables, Activity Tables, Activity Registration Tables,  and Activity Attempt Tables

  I need to find the Activity REgistrations for a given user and the Attempt info for each registration.

I have two separate queries that find the info,  but I need to combine them so I can return all the values in a single row (which makes actions on the row easier)

Right now I have a dtalist that returns the list of registrations with a button in a datalist.  Clicking the button calls the 2nd query to find the Attempt data for the selected activity. But that process requires too much user interaction.  My final result needs to be a hyperlink in the datalist that contains the attempt data for the activity.




//This query returns the user's list of Activities
SELECT     TOP (5) TBL_TMX_Activity.ActivityName, TBL_TMX_Activity.Activity_PK
FROM         TBL_TMX_Registration INNER JOIN
                      TBL_TMX_Activity ON TBL_TMX_Registration.ActivityFK = TBL_TMX_Activity.Activity_PK INNER JOIN
                      tblEmp INNER JOIN
                      iwc_Usr ON tblEmp.Emp_PK = iwc_Usr.Usr_EmpFK ON TBL_TMX_Registration.EmpFK = tblEmp.Emp_PK
WHERE     (iwc_Usr.Usr_Name = N'matt.fox') AND (TBL_TMX_Registration.Status <> 4) AND (TBL_TMX_Activity.CBTLaunchMtdFK IS NOT NULL)
ORDER BY TBL_TMX_Registration.LstUpd DESC, TBL_TMX_Activity.Activity_PK DESC
 
 
//This query returns the Attempt data for the user's activity
 
SELECT     TBL_TMX_Attempt.Attempt_PK, TBL_TMX_ActCBT.PackageId
FROM         iwc_Usr INNER JOIN
                      tblEmp ON iwc_Usr.Usr_EmpFK = tblEmp.Emp_PK INNER JOIN
                      TBL_TMX_Activity INNER JOIN
                      TBL_TMX_Attempt ON TBL_TMX_Activity.Activity_PK = TBL_TMX_Attempt.ActivityFK INNER JOIN
                      TBL_TMX_ActCBT ON TBL_TMX_Attempt.ActivityFK = TBL_TMX_ActCBT.ActivityFK ON tblEmp.Emp_PK = TBL_TMX_Attempt.EmpFK
WHERE     (iwc_Usr.Usr_Name = N'matt.fox') AND (TBL_TMX_Activity.Activity_PK = 6609)
 
//Can these two queries be combined into 1 that returns a list of activities and the attempt data for each activity?

Open in new window

Avatar of krishrr
krishrr
Flag of India image

Try to include any common field in both the queries (if exists) and make a inner join with that field, which will produce the desired result.

If two queries have no related fields, then try it with Union query.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mattfox77

ASKER

acperkins

Thanks for the input.  I tried your second post first,  but got errors.  I couldn't really understand the second post so I used your first post to create this query,  which worked.

Does it look accurate,  or is there something Im missing that could cause false results?
On My test  DB seems to be working fine...

select top 5 a.ActivityName, a.activity_pk, t.attempt_PK, c.PackageID
from
TBL_TMX_Activity a INNER JOIN TBL_TMX_Registration r on a.Activity_PK = r.ActivityFK
inner join tblEmp e on e.Emp_PK = r.EmpFK
inner join iwc_Usr u on u.Usr_EmpFK = e.Emp_PK
inner join TBL_TMX_Attempt t on t.ActivityFK = a.Activity_PK and t.EMpFK = e.Emp_PK
inner join TBL_TMX_ActCBT c on c.ActivityFK = t.ActivityFK
Where u.Usr_Name = N'matt.fox'
and r.Status <> 4
and a.CBTLaunchMtdFK is not null
order by r.LstUpd DESC, a.Activity_PK DESC

Open in new window

My concern was that you would only get 5 rows of data.  It sounded like you wanted all the activity for the first 5 rows of data.  But if that works for yoy, great!
Providing 2 approaches really nailed it and saved frustration in applying and debugging. Thanks!