Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003
    Posts
    59
    Provided Answers: 1

    Question Unanswered: MS SQL combining like rows

    Hey all I have the following query:
    Code:
        SELECT DISTINCT [WL].[Id]
          ,[WL].[UserId]
          ,[WL].[DIF]
          ,[WL].[MW]
          ,[WL].[Notes]
          ,[WL].[WDate]
          ,[WL].[CB]
          ,[WL].[MPH] 
          ,[U].[Id]
          ,[U].[UserName]
          ,[U].[We]
          ,[U].[SLength]
          ,[U].[UP]
          ,[U].[PU]
          ,[U].[ANumber]
          ,[U].[G_CK]
                  FROM [Wsite].[dbo].[WLog] as WL 
            INNER JOIN [Wsite].[dbo].[Users] AS U 
    	         	ON [U].[Id] = [WL].[UserId] 
    	         WHERE [WL].[WDate] >= CONVERT(datetime, '2012-01-01 00:00:00', 120)  
                   AND [WL].[WDate] <= CONVERT(datetime, GETDATE(), 120)
              GROUP BY [WL].[UserId]
    And the error i get is:
    > Column 'Wsite.dbo.WLog.Id' is invalid in the select
    > list because it is not contained in either an aggregate function or
    > the GROUP BY clause.
    What I am wanting to do is just conbine the data if there are more than one **UserID** in the list.

    As an example:
    Code:
        Id  | UserId | .... | Id   | UserName    | SLength | ....
        5843| 99304  | .... | 99304| Bob Barker  | 14      | ....
        5844| 06300  | .... | 06300| Dean Martin | 104     | ....
        5845| 99304  | .... | 99304| Bob Barker  | 8       | ....
        5846| 99304  | .... | 99304| Bob Barker  | 11      | ....
        5847| 7699   | .... | 7699 | John Doe    | 0       | ....
    So it should look like this:
    Code:
        Id  | UserId | .... | Id   | UserName    | SLength | ....
        5843| 99304  | .... | 99304| Bob Barker  | 33      | ....
        5844| 06300  | .... | 06300| Dean Martin | 104     | ....
        5847| 7699   | .... | 7699 | John Doe    | 0       | ....
    Notice that Bob Barker's SLength was combined (14+8+11=33).

    Any help would be great! Thanks!
    Last edited by StealthRT; 06-06-14 at 15:06.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Why are you picking Bob Barker 5843 over the the others?
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •