SQL查询-连接,添加和最低

时间:2019-02-28 08:50:08

标签: sql vb.net

不太确定如何解决这个问题,所以我将问题摆在那里,我可以执行常规的select语句和分组,但这似乎有点超出我的领域。在同一日期加入所有相同的产品ID,添加总时间,选择最短的最佳时间,添加所有周期数,等待操作员和生产时间。

我的select语句是这样的:

sql = "SELECT Product_ID, Date_Time, ulTotBoardCycleTime, ulBestBoardCycleTime, ulBoardCycleCount, ulWaitingForOperator, ulProductionTime FROM [i_import_general_timers] WHERE DATE_TIME >= @startdata2 AND DATE_TIME < @enddata2 AND ulBoardCycleCount > 0 ORDER BY Product_ID DESC"

What im trying to achieve

谢谢, 皮特

1 个答案:

答案 0 :(得分:2)

您需要对分组依据使用正确的聚合函数。

SELECT Product_ID, Date_Time, sum(ulTotBoardCycleTime) as TotalTime, min(ulBestBoardCycleTime) as BestTime, sum(ulBoardCycleCount) as CycleCount, sum(ulWaitingForOperator) as WaitingFor, sum(ulProductionTime) as ProductionTime
    FROM [i_import_general_timers] 
    WHERE DATE_TIME >= @startdata2 AND DATE_TIME < @enddata2 AND ulBoardCycleCount > 0 
    GROUP by Product_ID, Date_Time
    ORDER BY Product_ID DESC