I need to select 5 fields in a SELECT
statement.
2 of these 5 fields are different, so when I try a SELECT
with only these 2 fields I obtain the result I want (I put just a DISTINCT
in this request).
The 3 others fields are different, so when I add them to the SELECT
statement, the DISTINCT
isn't enough. I get duplicate rows.
The aim is to obtain the result of my request with the two fields and then add to the result row the top 1 for instance of the other fields. It is possible to do that?
This is the request :
-- Here Id, Description, and Image are differents and give duplicate rows
SELECT DISTINCT Title, GroupingId, Id, Description, Image
FROM dbo.AllContent
WHERE Title like '%'+'harr'+'%'
AND Rating <> 18
--result 89 rows...
-- No problems withs these fields
SELECT Title,GroupingId
FROM dbo.AllContent
WHERE Title like '%'+'harr'+'%'
AND Rating <> 18
--result 28 rows...
It is possible to use GROUP BY
statement on the 5 fields ?
I add the output with the first request (5 fields)
Title | Id | GroupingId | Description | Image
Title1 1 1 Description1 1.jpg
Title1 2 1 Description2 2.jpg
Title1 3 1 Description3 3.jpg
Title4 4 4 Description4 4.jpg
Title5 5 5 Description5 5.jpg
Title5 6 5 Description6 6.jpg
Then with the request with 2 fields:
Title | GroupingId
Title 1 1
Title 4 4
Title 5 5
What I need :
Title | Id | GroupingId | Description | Image
Title 1 1 1 Description 1 1.jpg
Title 4 4 4 Description 4 4.jpg
Title 5 5 5 Description 5 5.jpg