如何总结独特的线条?

时间:2019-04-23 12:19:59

标签: sql tsql sum unique

由于所有订单都是成批的-我使用不同的ID,所以我想计算用来收集每个订单的时间。

如果我使用Sum(DateDiff(MINUTE, pick.pick_start_time, pick.pick_end_time As [Pick Total Time],它将输出70。这应该是14,因为它对每个订单都显示相同的时间,显然是因为它在ID 1下是1批。

以下示例: enter image description here 有没有一种方法可以计算唯一的开始时间,结束时间,因此只能输出14。

2 个答案:

答案 0 :(得分:2)

一种解决方案可能只是计算最小开始时间和最大结束时间之间的时间差:

SELECT ID
      ,DateDiff(MINUTE,MIN(pick_start_time),MAX(pick_end_time)) AS [Pick Total]
  FROM yourtable
 GROUP BY ID 

答案 1 :(得分:0)

似乎无法使用

添加了完整的代码,以使我更想做什么

Select
Distinct(pick.pick_start_time) as Start time
picker.pic_name As Picker,
Count(distinct(order_header.oh_order_number) As [Order QT],
Sum(pick_line.pickl_quantity) As [Total items],
SUM(DateDiff(MINUTE,MIN(pick_start_time),MAX(pick_end_time)) AS [Pick Total]

From delivery_header 
Inner Join
  (((pick_line 
    Inner Join
        pick On pick_line.pickl_pick_id = pick.pick_id) 
    Inner Join
      picker On pick.pick_pic_id = picker.pic_id) 
Inner Join
    (pack 
    Inner Join
      pack_line On pack.pack_id = pack_line.pl_pack_id) On pick_line.pickl_id =
      pack_line.pl_pickl_id) On delivery_header.dh_pack_id = pack.pack_id
    Inner Join
  order_header On order_header.oh_id = delivery_header.dh_oh_id


Where pick.pick_end_time >= @DateFrom And pick.pick_end_time <= @DateTo And
  (pick.pick_pic_id = @picker Or @AllPickers = 1)

Group By picker.pic_name
Order By picker.pic_name