samtran0331
asked on
Get only the last record from each "set", Max?, Distinct?, huh?
Hi Experts!
I have a query that is returning something like this:
Month Year UserID ......
1 2005 1
2 2005 1
3 2005 1
4 2005 1
1 2005 2
2 2005 2
3 2005 2
4 2005 2
1 2005 3
2 2005 3
3 2005 3
4 2005 3
How can I nest or rewrite this query to return this:
Month Year UserID ......
4 2005 1
4 2005 2
4 2005 3
query:
SELECT DATEPART([month], sp_ProductionByUser.Statem entDate) AS [Month], DATEPART([year],
sp_ProductionByUser.Statem entDate) AS [Year], tblProducerType.ProducerTy peID, sp_ProductionByUser.UserID ,
sp_ProductionByUser.FullNa me, SUM(sp_ProductionByUser.su mProd) AS sumTotalProd,SUM(sp_Produc tionByUser .sumComm) AS sumTotalComm,tblUser.defau ltPercenta ge
FROM sp_ProductionByUser INNER JOIN
tblUserProducerType ON sp_ProductionByUser.UserID = tblUserProducerType.UserID INNER JOIN
tblProducerType ON tblUserProducerType.Produc erTypeID = tblProducerType.ProducerTy peID INNER JOIN
tblUser ON tblUserProducerType.UserID = tblUser.UserID
WHERE (sp_ProductionByUser.State mentDate >= '01/01/2004') AND (sp_ProductionByUser.State mentDate <= '03/31/2005')
GROUP BY sp_ProductionByUser.FullNa me, DATEPART([month], sp_ProductionByUser.Statem entDate), DATEPART([year],
sp_ProductionByUser.Statem entDate), sp_ProductionByUser.UserID , tblProducerType.Q5,
tblProducerType.ProducerTy peID, tblUser.defaultPercentage
HAVING (SUM(sp_ProductionByUser.s umProd) <> 0) AND (sp_ProductionByUser.UserI D <> 0) AND
(sp_ProductionByUser.UserI D <> 3)
ORDER BY sp_ProductionByUser.FullNa me, DATEPART([year], sp_ProductionByUser.Statem entDate), DATEPART([month],
sp_ProductionByUser.Statem entDate)
I have a query that is returning something like this:
Month Year UserID ......
1 2005 1
2 2005 1
3 2005 1
4 2005 1
1 2005 2
2 2005 2
3 2005 2
4 2005 2
1 2005 3
2 2005 3
3 2005 3
4 2005 3
How can I nest or rewrite this query to return this:
Month Year UserID ......
4 2005 1
4 2005 2
4 2005 3
query:
SELECT DATEPART([month], sp_ProductionByUser.Statem
sp_ProductionByUser.Statem
sp_ProductionByUser.FullNa
FROM sp_ProductionByUser INNER JOIN
tblUserProducerType ON sp_ProductionByUser.UserID
tblProducerType ON tblUserProducerType.Produc
tblUser ON tblUserProducerType.UserID
WHERE (sp_ProductionByUser.State
GROUP BY sp_ProductionByUser.FullNa
sp_ProductionByUser.Statem
tblProducerType.ProducerTy
HAVING (SUM(sp_ProductionByUser.s
(sp_ProductionByUser.UserI
ORDER BY sp_ProductionByUser.FullNa
sp_ProductionByUser.Statem
Try this:
SELECT DATEPART([month], sp_ProductionByUser.Statem entDate) AS [Month], DATEPART([year],
sp_ProductionByUser.Statem entDate) AS [Year], tblProducerType.ProducerTy peID, sp_ProductionByUser.UserID ,
sp_ProductionByUser.FullNa me, SUM(sp_ProductionByUser.su mProd) AS sumTotalProd,SUM(sp_Produc tionByUser .sumComm) AS sumTotalComm,tblUser.defau ltPercenta ge
FROM sp_ProductionByUser INNER JOIN
tblUserProducerType ON sp_ProductionByUser.UserID = tblUserProducerType.UserID INNER JOIN
tblProducerType ON tblUserProducerType.Produc erTypeID = tblProducerType.ProducerTy peID INNER JOIN
tblUser ON tblUserProducerType.UserID = tblUser.UserID
WHERE (sp_ProductionByUser.State mentDate >= '01/01/2004') AND (sp_ProductionByUser.State mentDate <= '03/31/2005')
AND
sp_ProductionByUser.UserID = (SELECT MAX(UserID) FROM sp_ProductionByUser B
WHERE B.StatementDate >= '01/01/2004' AND B.StatementDate <= '03/31/2005' AND
DATEPART([month], sp_ProductionByUser.Statem entDate) = DATEPART([month], B.StatementDate) AND
DATEPART([year], sp_ProductionByUser.Statem entDate) = DATEPART([year],
B.StatementDate))
GROUP BY sp_ProductionByUser.FullNa me, DATEPART([month], sp_ProductionByUser.Statem entDate), DATEPART([year],
sp_ProductionByUser.Statem entDate), sp_ProductionByUser.UserID , tblProducerType.Q5,
tblProducerType.ProducerTy peID, tblUser.defaultPercentage
HAVING (SUM(sp_ProductionByUser.s umProd) <> 0) AND (sp_ProductionByUser.UserI D <> 0) AND
(sp_ProductionByUser.UserI D <> 3)
ORDER BY sp_ProductionByUser.FullNa me, DATEPART([year], sp_ProductionByUser.Statem entDate), DATEPART([month],
sp_ProductionByUser.Statem entDate)
SELECT DATEPART([month], sp_ProductionByUser.Statem
sp_ProductionByUser.Statem
sp_ProductionByUser.FullNa
FROM sp_ProductionByUser INNER JOIN
tblUserProducerType ON sp_ProductionByUser.UserID
tblProducerType ON tblUserProducerType.Produc
tblUser ON tblUserProducerType.UserID
WHERE (sp_ProductionByUser.State
AND
sp_ProductionByUser.UserID
WHERE B.StatementDate >= '01/01/2004' AND B.StatementDate <= '03/31/2005' AND
DATEPART([month], sp_ProductionByUser.Statem
DATEPART([year], sp_ProductionByUser.Statem
B.StatementDate))
GROUP BY sp_ProductionByUser.FullNa
sp_ProductionByUser.Statem
tblProducerType.ProducerTy
HAVING (SUM(sp_ProductionByUser.s
(sp_ProductionByUser.UserI
ORDER BY sp_ProductionByUser.FullNa
sp_ProductionByUser.Statem
ASKER
Thanks routinet...that does put it in that order...but I guess I wasn't clear on the result I need...
It needs to return *only* the highest month/year for each user....
So in my lil example above,
Month Year UserID ......
4 2005 1
4 2005 2
4 2005 3
4/2005 would be the only combo returned for each userID, no other months or years
It needs to return *only* the highest month/year for each user....
So in my lil example above,
Month Year UserID ......
4 2005 1
4 2005 2
4 2005 3
4/2005 would be the only combo returned for each userID, no other months or years
ASKER
rafrancisco,
That query took almost 2 seconds to run and returned me a really odd dataset:
Month Year UserID ......
3 2005 1
9 2005 1
11 2005 1
1 2005 6
2 2005 6
3 2005 6
1 2005 10
2 2005 10
3 2005 10
4 2005 10
5 2005 10
6 2005 10
That query took almost 2 seconds to run and returned me a really odd dataset:
Month Year UserID ......
3 2005 1
9 2005 1
11 2005 1
1 2005 6
2 2005 6
3 2005 6
1 2005 10
2 2005 10
3 2005 10
4 2005 10
5 2005 10
6 2005 10
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
rafrancisco,
PURE GENIUS!
PURE GENIUS!
ORDER BY DATEPART([month], sp_ProductionByUser.Statem