title: SQL Server: aggregate error on grouping
body: <p>I have a table called tasks, which lists different tasks a worker can complete. Then i have a relationship table that links a completed task to a worker. 
  I'm trying to write query that groups the tasks into a list based on the worker id, but the query gives me the following error (see below).  </p>
  
  <pre><code>Column 'mater.dbo.worker_task_completion.FK_task_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
  </code></pre>
  
  <p>Tables</p>
  
  <pre><code>CREATE TABLE [dbo].[tasks]
  (
      [task_id] [int] IDENTITY(1,1) NOT NULL,
      [name] [nvarchar](50) NOT NULL,
      [icon] [nvarchar](max) NULL,
      [isActive] [int] NOT NULL,
      [time] [int] NOT NULL,
      CONSTRAINT [PK_tasks] PRIMARY KEY CLUSTERED 
  (
      [task_id] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  
  
  
  CREATE TABLE [dbo].[worker_task_completion]
  (
      [FK_worker_id] [int] NOT NULL,
      [FK_task_id] [int] NOT NULL,
      [update_date] [datetime] NOT NULL,
      CONSTRAINT [PK_worker_task_completion] PRIMARY KEY CLUSTERED 
  (
      [FK_worker_id] ASC,
      [FK_task_id] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  </code></pre>
  
  <p>Query </p>
  
  <pre><code>SELECT top 100 wtc.FK_worker_id, 
                  tasks = Stuff((SELECT ', ' + dbo.tasks.NAME 
                                  FROM   dbo.tasks
                                 WHERE  dbo.tasks.task_id = 
                                       wtc.FK_task_id
                                 FOR xml path ('')), 1, 1, '')
          FROM   dbo.worker_task_completion AS wtc 
                  LEFT JOIN dbo.tasks AS tc 
                         ON tc.task_id = wtc.fk_task_id
          -- WHERE  wtc.FK_worker_id IN ()
  GROUP  BY wtc.FK_worker_id
  </code></pre>