I do an distinct query like this:
query = "SELECT DISTINCT name, total = COUNT(*) from products where name LIKE '%" & searchString & "%' GROUP BY name"
The query works fine but I need to select one more field called "info" so I just tried this:
query = "SELECT DISTINCT name, info, total = COUNT(*) from products where name LIKE '%" & searchString & "%' GROUP BY name"
but then I get the error message:
"Column 'products.info' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
I want do group just by "name" and not "info"
How can I select this "info" field to?? somebody know??
You can use
Select name, max(info), total=count........ Group by name
To "fool" the Sql server, but I am not sure this is what you want.
Will the info column have the same value for all the rows with the samevalue in the name column? Then you might as well use group by name,info the result willbe the same.
But if the info column has different values within the same name, which one will you have displayed?
BTW: It should not be necessary to use distinct in this query
|||
Now i do realize I cant get the info field because its just a distinct query. Forget this message.
thanks anyway
No comments:
Post a Comment