Thursday, March 22, 2012

Joining two Queries

Right now I have two Queries that I've created:
What I'm wanting to do is graft everything from Query1 onto Query2 but only where Query1.UserID = Query2.UserID . Any suggestions on how I would go about doing this?

--Query 1
(
SELECT
PC1.ID,
PC1.UserID,
PC1.ModuleID,
PC1.ModifiedDate
FROM
Projex2_0_0_Cores PC1
WHERE
PC1.ModuleID = 369
)

--Query 2
(
SELECT
PC2.UserID,
MAX(PC2.CreatedDate) as CreatedDate
FROM
Projex2_0_0_Cores PC2
WHERE
PC2.ModuleID = 369
GROUP BY
PC2.UserID,
PC2.ModuleID
)Was the following query not helpful for this situation?

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=840226|||Lol. Thanks. For some reason ASP.NET didn't email me letting me know there was a reply.

Thanks so much!
-Tony Valenti|||I'm also needing to be able to do the same thing, except grab all the rows where the CreatedDate != Max(CreatedDate).

Here's my scenerio, and maybe you know a better way:

I have a table of serialized objects (Cores). In the table I want a users to be able to have as many entries as they want, but only one active entry. I'm currently trying to use CreatedDate to find out what the active entries are:
CreatedDate = Max(CreatedDate) == Active
CreatedDate != Max(CreatedDate) == Archived

I have a stored procedure that I want to be able to pass 3 parameters: ModuleID (int), Active (bit), and Archived (bit). If I pass active =1 archived=1, I get all the records for that moduleID (easy), but if only one of them is true, then I only want the actives or the archives.

How would you do this?

Thanks!|||write a little if statement in your stored proc.


--Assuming you have default values of 0's for both params Active and Archived..
IF Active = 1 AND Archived = 1 THEN
'your select query that returns all rows
ELSE IF Active = 1 AND Archived = 0 THEN
'your query with where condition CreatedDate = Max(CreatedDate)
ELSE IF IF Active = 0 AND Archived = 1 THEN
'your query with where condition CreatedDate <> Max(CreatedDate)
END IF

No comments:

Post a Comment