Need a SQL subquery to compile data

Posted on 2009-05-09
Last Modified: 2012-05-06
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'') 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'') 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

Question by:mattfox77
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 24345221
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.
LVL 75

Accepted Solution

Anthony Perkins earned 500 total points
ID: 24346015
Something like this perhaps:

FROM      TBL_TMX_Registration r
            INNER JOIN TBL_TMX_Activity a ON r.ActivityFK = a.Activity_PK
            INNER JOIN tblEmp e ON r.EmpFK = e.Emp_PK
            INNER JOIN iwc_Usr u ON e.Emp_PK = u.Usr_EmpFK
        INNER JOIN TBL_TMX_Attempt t ON a.Activity_PK = t.ActivityFK And e.Emp_PK = t.EmpFK
            INNER JOIN TBL_TMX_ActCBT c ON t.ActivityFK = c.ActivityFK
WHERE      u.Usr_Name = N''
            AND e.Status <> 4
            AND a.CBTLaunchMtdFK IS NOT NULL
            r.LstUpd DESC,
            a.Activity_PK DESC
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 500 total points
ID: 24346034
On second thoughts this might be more accurate:

SELECT      ra.ActivityName,
FROM      (Select      TOP 5
            From      TBL_TMX_Registration r
                        INNER JOIN TBL_TMX_Activity a ON r.ActivityFK = a.Activity_PK
            Where      a.CBTLaunchMtdFK IS NOT NULL
            ORDER BY
                        r.LstUpd DESC,
                        a.Activity_PK DESC) ra
            INNER JOIN tblEmp e ON ra.EmpFK = e.Emp_PK
            INNER JOIN iwc_Usr u ON e.Emp_PK = u.Usr_EmpFK
        INNER JOIN TBL_TMX_Attempt t ON a.Activity_PK = t.ActivityFK And e.Emp_PK = t.EmpFK
            INNER JOIN TBL_TMX_ActCBT c ON t.ActivityFK = c.ActivityFK
WHERE      u.Usr_Name = N''
            AND e.Status <> 4
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.


Author Comment

ID: 24350718

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
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''
and r.Status <> 4
and a.CBTLaunchMtdFK is not null
order by r.LstUpd DESC, a.Activity_PK DESC

Open in new window

LVL 75

Expert Comment

by:Anthony Perkins
ID: 24351024
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!

Author Closing Comment

ID: 31579818
Providing 2 approaches really nailed it and saved frustration in applying and debugging. Thanks!

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question