简体   繁体   English

SQL查询根据ID返回字段的总和

[英]SQL Query that Returns the Sum of Field depending on ID

In the top picture is what I have in the SQL Table. 在最上面的图片是我在SQL表中的内容。

I have this Query that gives me the result in the bottom gridview. 我有这个查询,使我在底部的网格视图中的结果。 The query will return in The TransTypeDesc Column the largest EstimatedTransTime. 该查询将在TransTypeDesc列中返回最大的EstimatedTransTime。 As you can see Car is the largest EstTransTime in Q1. 如您所见,Car是第一季度最大的EstTransTime。

The only thing that I dont have is the TotalEstimatedTransTime based on the DisplayNum. 我唯一没有的是基于DisplayNum的TotalEstimatedTransTime。 So for Q1 I should have 31 min. 所以对于Q1我应该有31分钟。

  SELECT t1.DisplayNum, t1.TransTypeDesc, t1.SubQueueId, t1.EstimatedTransTime 
  FROM vwQueueData AS t1 
  LEFT OUTER JOIN vwQueueData AS t2 ON t1.DisplayNum = t2.DisplayNum AND t1.EstimatedTransTime < t2.EstimatedTransTime 
  WHERE (t2.DisplayNum IS NULL) AND (t1.Called IS NULL) AND (t1.SubQ = @SubQ) 

在此处输入图片说明

HERE IS THE SQL Fiddle : http://sqlfiddle.com/#!3/a8983/1 SQL小提琴在这里: http ://sqlfiddle.com/#!3/a8983/1

You need to do something as per below: 您需要执行以下操作:

SELECT t1.DisplayNum, t1.TransTypeDesc, t1.SubQueueId, SUM(t1.EstimatedTransTime) 
FROM vwQueueData AS t1 
LEFT OUTER JOIN vwQueueData AS t2 
     ON t1.DisplayNum = t2.DisplayNum AND 
        t1.EstimatedTransTime < t2.EstimatedTransTime 
WHERE (t2.DisplayNum IS NULL) AND 
      (t1.Called IS NULL) AND (t1.SubQ = @SubQ)
GROUP BY t1.DisplayNum, t1.TransTypeDesc, t1.SubQueueId

Basically, add the SUM function to the 'EstimatedTransTime' column. 基本上,将SUM函数添加到“ EstimatedTransTime”列中。 Then perform a group by on the DisplayNum column in order to get the desired output. 然后在DisplayNum列上执行分组依据,以获得所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM