Monday, 15 March 2010

MS SQL Server 2008 error on GROUP BY -


I have a table that I'm trying to clear, so I'm the first in the table for each ClientId The address should be

table addresses in these columns

  Pk [Id] [ClientId] [AddressLine1] [AddressLine2] < / Code>  

I use the query:

 select  * ORDER client ID   

result = < / P>

  1 1 fu bar 2 1 fu 2 times 2 3 fu 3 times 3 4 1 fu 4 times 4 5 2 fu times 2 9 2 fu times 5 97 2 fu times 6 8 3 fu 2 times 7   

expected result =

  1 1 foo bar & lt; - The first match for the client is - 1 5 2 foo bar2 & lt; - The first match for the client is - 2 8 3 foo2 times 7 & lt; - The first match for the client is = 3   

This needs to work for n clients

I tried

  The customer id   

is the resultant error in the SELECT * FROM addresses group (the column 'id' is invalid in the selection list because it is not a composite function or a block by group.)

What's missing here?

SQL Server requires that when you use a group, you can select columns in the selected list Must use a composite function or add them to Group BY.

Your original query can be changed to use the following:

  An .id, a.clientid, a.address1, ClientID, client ) Select from IDE Client from address group) at a.clientid = d.clientid and a.id = d.id;   

As you can see, it uses a subquery which gives the minimum for each clientId , It only clientid . Then you join this table to return only those rows with the minimum ID for each customer.

No comments:

Post a Comment