title: SELECT statement to find SUM of a column
body: <p>I am trying to create a query statement that will find sum of the TaskLength column. </p>
  
  <p>I've tried quite a number combinations placing the SQL <code>SUM</code> function in the query using comma separators, and <code>AND</code> statements. Without the <code>SUM TaskLength</code>, the query works fine.  </p>
  
  <p>This is being used in a <code>c# WPF</code> application but I'm able to test the query statements inside the Visual Studio, Server Explorer query window.   </p>
  
  <p>Here is the query I have tried:</p>
  
  <pre><code>SELECT TaskName, TaskLength FROM myTable, SUM TaskLength 
  WHERE TaskName='Maintenance' AND EventStartTime 
  BETWEEN '8/17/2019' AND '8/19/2019' 
  </code></pre>
  
  <p>This is my table:</p>
  
  <pre><code>TABLE myTable 
  ID    TaskName      EventStartTime          TaskLength
  318   START SHIFT   8/17/2019 11:51:00 AM   16                  
  319   Maintenance   8/17/2019 12:07:00 PM   10                  
  320   Part Prep     8/17/2019 12:17:00 PM   13                  
  321   Production    8/17/2019 12:30:00 PM   117                 
  322   Part Prep     8/17/2019 02:26:00 PM   45                  
  323   Downtime      8/17/2019 02:28:00 PM   65                  
  327   START SHIFT   8/17/2019 03:48:00 PM   00                  
  328   Maintenance   8/17/2019 03:48:00 PM   25                  
  329   Part Prep     8/17/2019 03:48:00 PM   35                  
  330   Production    8/17/2019 03:48:00 PM   120                 
  331   START SHIFT   8/17/2019 03:59:00 PM   10                  
  332   Maintenance   8/17/2019 03:59:00 PM   90                  
  </code></pre>
  
  <p>I am getting <code>Incorrect Syntax Near..</code> error. I expect when it works out correctly I will get a result of 125.</p>
  
  <p><em>Revised query with entire code block:</em></p>
  
  <pre><code>using (SqlConnection con = new SqlConnection(_connectionString))
  {
      string query = @"SELECT TaskName, SUM(TaskLength) as TaskLength FROM myTable " +
            "WHERE EventStartTime BETWEEN '2019/8/17' AND '2019/8/19' GROUP BY TaskName";
  
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            con.Open();
  
            object result = cmd.ExecuteScalar();  // returns 'Downtime'
            string totalMinutes = Convert.ToString(result);
  
        }
  }
  </code></pre>
  
  <p>I'm progressing along. The code executes without errors except the result returns "Downtime" from the TaskName column instead of 125 from SUM(TaskLength) that I expect.</p>
  
  <p>This query does work correctly in the Visual Studio SQL query window. </p>