troubleshooting Question

Why isn't the first query producing correct result?

Avatar of sammySeltzer
sammySeltzerFlag for United States of America asked on
ASP.NETMicrosoft SQL Server 2005SQL
12 Comments1 Solution445 ViewsLast Modified:
Greetings mates,

Maybe someone alot more knowledgeable than me in SQL can explain this to me and there are certainly a lot of  you.

When I run this query:

select make, count(*) totalNumber from VehicleModelYear where make='acura' and year=1987
group by make

I get correct results.

For instance, I do have, for testing purposes, 2 acura legends for the year 1987 and it is listed correctly as:

Acura Legend(2)

If, however, I run this query:

select vehicleId,( make+ '(' + CONVERT(NVARCHAR, (select count(*) from VehicleModelYear B where a.vehicleId=b.vehicleId)) + ')') as Make from VehicleModelYear A where a.Year = 1987 order by a.Make

I get the following results:

Acura Legend(1)
Acura Legend(2)

Any reason for the descrepancies and how do I resolve this?

Just  a little history, I am building a car dealer web app.

Users will select Year from dropdownlist, say 1987.

Once this select is made, all vehicles and their Makes and models for the year 1987 are loaded into the second dropdown.

The functionality works but the results don't.

Your assistance is greatly appreciated.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros