We help IT Professionals succeed at work.
Get Started

Count of distinct records in a group

Last Modified: 2021-04-21
The query:

select distinct
tblAutoSystem.AutoSystemID, tblAutoSystem.AutoSystemDescription ,
count(1) as CNT
from tblTSBToVehicleXref
join tblTSBToAutoSystemXref on
join tblAutoSystem on
where vehicleID in ( '1001114','1001125','1001136')
group by tblAutoSystem.AutoSystemDescription,tblAutoSystem.AutoSystemID
order by tblAutoSystem.AutoSystemDescription

71 Automatic Trans, Coolers, Torque Converter    3

The number on the end of the data is the count of the records in each group

The actual DISTINCT count of the 2 fields is 1.

The following query shows the actual data.

select VehicleID, tblTSBToVehicleXref.TSBID,
tblTSBToAutoSystemXref.AutoSystemID, AutoSystemDescription
 from tblTSBToVehicleXref
 join tblTSBToAutoSystemXref on
 join tblAutoSystem on
where vehicleID in('1001114','1001125','1001136') and

1001114 7404    71    Automatic Trans, Coolers, Torque Converter

1001125 7404    71    Automatic Trans, Coolers, Torque Converter

1001136 7404    71    Automatic Trans, Coolers, Torque Converter

What am I doing wrong?
Do I need to a a subquery in place of the count() to get the correct number?
Watch Question
SQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2019
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE