I have two tables (Records and Comments). I am trying to join the two using Min and Max on the Comments id, where the result would be:
Record id; Date; First Comment; Last Comment
Records:
id date
1 1/1/15
2 1/2/15
Comments:
id Rec_id Comment
1 1 First Comment
2 1 Last Comment
3 2 First Comment
4 2 Second Comment
5 2 Last Comment
Here is the script:
SELECT
Records.id
, Records.rcd_date
, MIN(Comments_1.id) AS minID
, Comments_1.Comment AS minCom
, MAX(Comments_2.id) AS maxID
, Comments_2.Comment AS maxCom
FROM
Records
INNER JOIN
Comments AS Comments_1 ON Records.id = Comments_1.Rec_id
INNER JOIN
Comments AS Comments_2 ON Records.id = Comments_2.Rec_id
GROUP BY
Records.id
, Records.rcd_date
, Comments_1.Comment
, Comments_2.Comment
All I want to see is:
id rcd_date minID minCom maxID maxCom
1 2015-01-01 1 First Comment 2 Last Comment
2 2015-01-02 3 First Comment 5 Last Comment
But I am getting every variation on the comments to the result table.
id rcd_date minID minCom maxID maxCom
1 2015-01-01 1 First Comment 1 First Comment
1 2015-01-01 1 First Comment 2 Last Comment
1 2015-01-01 2 Last Comment 1 First Comment
1 2015-01-01 2 Last Comment 2 Last Comment
2 2015-01-02 3 First Comment 3 First Comment
2 2015-01-02 3 First Comment 5 Last Comment
2 2015-01-02 3 First Comment 4 Second Comment
2 2015-01-02 5 Last Comment 3 First Comment
2 2015-01-02 5 Last Comment 5 Last Comment
2 2015-01-02 5 Last Comment 4 Second Comment
2 2015-01-02 4 Second Comment 3 First Comment
2 2015-01-02 4 Second Comment 5 Last Comment
2 2015-01-02 4 Second Comment 4 Second Comment
I have tried Inner Join, Right Outer Join, and Left Outer Join, but the results are all the same.
Any ideas?