0

I have looked at many examples of selecting many columns but only grouping by 1 column and mine seem to give me duplicate results. See below I would like to select all the columns in my table but would like to only GROUP BY VehicleId. On the screenshot you'll see that the results are actually not grouped by VehicleId.

Any idea on what I am doing wrong?

Try 1:

SELECT 
    h.*,

    TotalFines = 1,
    TotalIncidents = 1,
    TotalVehicleAllocations = 1,
    TotalVehicleConditions = 1,
    TotalMileageApplications = 1
FROM
(
    SELECT h1.VehicleId FROM [dbo].[VehicleHistory] h1 GROUP BY h1.VehicleId
) GroupedList
INNER JOIN [dbo].[VehicleHistory] h ON GroupedList.VehicleId=h.VehicleId
ORDER BY
    h.VehicleId;

Try 2:

SELECT t1.* FROM VehicleHistory t1
INNER JOIN
(
    SELECT VehicleId FROM VehicleHistory GROUP BY VehicleId
)   t2
ON t1.VehicleId=t2.VehicleId

Both queries produce the same results with duplicate rows for each VehicleId as per below:

enter image description here

Here's my expected results below. The results are a query produced by Entity Framework. But I would like to rewrite the linq query into T-SQL:

enter image description here

2
  • Can you post your expected results? Commented Jun 7, 2019 at 9:31
  • @forpas, I have added how the results should look like.. I am trying to rewrite an Entity Framework Linq Query into a T-SQL query... Commented Jun 7, 2019 at 9:35

1 Answer 1

1

This is because you are gtouping in subquery (which would be same as SELECT DISTINCT h1.VehicleId FROM [dbo].[VehicleHistory] h1):

SELECT h1.VehicleId FROM [dbo].[VehicleHistory] h1 GROUP BY h1.VehicleId

and then you are joining in on that column, which can cause duplicates to occur (you have duplicate IDs in VehicleHistory).

All you need to do is:

SELECT VehicleId,
       MAX(DateUpdated) DateUpdated, --or other aggregate function
       --rest of your columns in appropriate aggreagte functions
FROM VehicleHistory
GROUP BY VehicleId
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that produces expected results, but is that the best approach, adding MAX on all other columns?
@Morgs You can use whatever you need, that depends on you and cannot be thought as "best approach"", because it depends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.