tricky join with 2 sub queries

Both queries are working properly separately but can't figure out how to join them on UserID to retrun one result set. THANKS!

QUERY 1:

SELECT     _userBets.betId, wl.Id AS weightLogId, wl.userId, _users.firstName, _users.lastName, wl.entryDate, wl.weight, _userBets.goalWeight,
                      wl.weight - _userBets.goalWeight AS diff
FROM         (SELECT     userId, MAX(entryDate) AS lastEntryDate
                       FROM          _weightLog
                       GROUP BY userId) AS x INNER JOIN
                      _weightLog AS wl ON wl.userId = x.userId AND wl.entryDate = x.lastEntryDate INNER JOIN
                      _users ON wl.userId = _users.Id INNER JOIN
                      _userBets ON _users.Id = _userBets.userId
WHERE     (_userBets.betId = @BetId)
ORDER BY wl.weight - _userBets.goalWeight

QUERY 2:

SELECT     wl2.userId, y.firstEntryDate, wl2.weight AS startingWeight
FROM         (SELECT     userId, MIN(entryDate) AS firstEntryDate
                       FROM          _weightLog
                       GROUP BY userId) AS y INNER JOIN
                      _weightLog AS wl2 ON wl2.userId = y.userId AND wl2.entryDate = y.firstEntryDate
aorkandAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT * FROM (

SELECT     _userBets.betId, wl.Id AS weightLogId, wl.userId, _users.firstName, _users.lastName, wl.entryDate, wl.weight, _userBets.goalWeight,
                      wl.weight - _userBets.goalWeight AS diff
FROM         (SELECT     userId, MAX(entryDate) AS lastEntryDate
                       FROM          _weightLog
                       GROUP BY userId) AS x INNER JOIN
                      _weightLog AS wl ON wl.userId = x.userId AND wl.entryDate = x.lastEntryDate INNER JOIN
                      _users ON wl.userId = _users.Id INNER JOIN
                      _userBets ON _users.Id = _userBets.userId
WHERE     (_userBets.betId = @BetId) )A
INNER JOIN (
SELECT     wl2.userId, y.firstEntryDate, wl2.weight AS startingWeight
FROM         (SELECT     userId, MIN(entryDate) AS firstEntryDate
                       FROM          _weightLog
                       GROUP BY userId) AS y INNER JOIN
                      _weightLog AS wl2 ON wl2.userId = y.userId AND wl2.entryDate = y.firstEntryDate )  B
ON a.UserID = B.UserID
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wingkoCommented:
Dear arkand,

Here is my alternative solution regarding that problem.
The idea is making a union  between two query, so i would create record like this
ex:
UserID    firstEntryDate    lastEntryDate
123             0                      1/31/2007
123         1/1/2007              0
232             0                      1/29/2007
232         1/2/2007              0

by using max function and grouping each field you'll get:
UserID    firstEntryDate    lastEntryDate
123          1/1/2007              1/31/2007
232          1/2/2007              1/29/2007




SELECT Max(betId),Max(weightLogId),Max(userId), Max(firstName), Max(lastName), Max(entryDate), Max(weight), Max(goalWeight), Max(diff), Max(firstEntryDate), Max(startingWeight) FROM  (
 
SELECT     _userBets.betId, wl.Id AS weightLogId, wl.userId, _users.firstName, _users.lastName, wl.entryDate, wl.weight, _userBets.goalWeight,
                      wl.weight - _userBets.goalWeight AS diff, 0 as firstEntryDate, 0 AS startingWeight
FROM         (SELECT     userId, MAX(entryDate) AS lastEntryDate
                       FROM          _weightLog
                       GROUP BY userId) AS x INNER JOIN
                      _weightLog AS wl ON wl.userId = x.userId AND wl.entryDate = x.lastEntryDate INNER JOIN
                      _users ON wl.userId = _users.Id INNER JOIN
                      _userBets ON _users.Id = _userBets.userId
WHERE     (_userBets.betId = @BetId)
ORDER BY wl.weight - _userBets.goalWeight
 
UNION
 
SELECT    0 as betId, 0 as weightLogId, 0 as UserId, 0 as firstName, 0 as lastName, 0 as entryDate, 0 as weight, 0 as goalWeight, 0 as diff,  y.firstEntryDate, wl2.weight AS startingWeight
FROM         (SELECT     userId, MIN(entryDate) AS firstEntryDate
                       FROM          _weightLog
                       GROUP BY userId) AS y INNER JOIN
                      _weightLog AS wl2 ON wl2.userId = y.userId AND wl2.entryDate = y.firstEntryDate
 
 
) AS SQLCombination
Group by betId, weightLogId, UserId, firstName, lastName, entryDate, weight, goalWeight, diff,firstEntryDate, startingWeight

Open in new window

0
aorkandAuthor Commented:
this WORKS! perfect - thank you very much!
0
aorkandAuthor Commented:
brilliant.. thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.