SELECT T.USERNAME, T.MONTH, T.YEAR, Yt.COST, Yt.GUID, Yt.ENTRYDATE, 'FIRST' [Type]
FROM (
SELECT USERNAME,
MONTH(ENTRYDATE) [MONTH],
YEAR(ENTRYDATE) [YEAR],
MIN(ENTRYDATE) MinDate
FROM YourTable
GROUP BY USERNAME, MONTH(ENTRYDATE), YEAR(ENTRYDATE)
) T INNER JOIN YourTable Yt ON T.USERNAME = Yt.USERNAME AND T.MinDate = Yt.ENTRYDATE
UNION
SELECT T.USERNAME, T.MONTH, T.YEAR, Yt.COST, Yt.GUID, Yt.ENTRYDATE, 'LAST' [Type]
FROM (
SELECT USERNAME,
MONTH(ENTRYDATE) [MONTH],
YEAR(ENTRYDATE) [YEAR],
MAX(ENTRYDATE) MaxDate
FROM YourTable
GROUP BY USERNAME, MONTH(ENTRYDATE), YEAR(ENTRYDATE)
) T INNER JOIN YourTable Yt ON T.USERNAME = Yt.USERNAME AND T.MaxDate = Yt.ENTRYDATE
|