sql max / min查询和数据转换

时间:2009-08-10 20:17:26

标签: sql sql-server sql-server-2005

更新:更改一次,以显示每次发货的时间可能不会按顺序排列。

这是我的输入

create table test
(
    shipment_id int,
    stop_seq tinyint,
    time datetime
)

insert into test values (1,1,'2009-8-10 8:00:00')
insert into test values (1,2,'2009-8-10 9:00:00')
insert into test values (1,3,'2009-8-10 10:00:00')
insert into test values (2,1,'2009-8-10 13:00:00')
insert into test values (2,2,'2009-8-10 14:00:00')
insert into test values (2,3,'2009-8-10 20:00:00')
insert into test values (2,4,'2009-8-10 18:00:00')

我想要的输出低于

shipment_id  start    end
-----------  -----    ---
     1        8:00    10:00
     2        13:00   18:00

我需要从每个货件的min(stop)行和从max(stop)行开始/结束时的时间分别花时间。我知道这可以通过多个查询轻松完成,但我希望看一个选择查询是否可以做到这一点。

谢谢!

6 个答案:

答案 0 :(得分:4)

我认为你能够做到的唯一方法就是使用子查询。

SELECT shipment_id
    , (SELECT TOP 1 time 
        FROM test AS [b] 
        WHERE b.shipment_id = a.shipment_id 
        AND b.stop_seq = MIN(a.stop_seq)) AS [start]
    , (SELECT TOP 1 time 
        FROM test AS [b] 
        WHERE b.shipment_id = a.shipment_id 
        AND b.stop_seq = MAX(a.stop_seq)) AS [end]
FROM test AS [a]
GROUP BY shipment_id

您需要使用DATEPART功能来切断时间列以获得准确的输出。

答案 1 :(得分:1)

使用公用表表达式(CTE) - 这是有效的(至少在我的SQL Server 2008测试系统上):

WITH SeqMinMax(SeqID, MinID, MaxID) AS
(
    SELECT Shipment_ID, MIN(stop_seq), MAX(stop_seq)
    FROM test
    GROUP BY Shipment_ID
)
SELECT 
    SeqID 'Shipment_ID',
    (SELECT TIME FROM test 
       WHERE shipment_id = smm.seqid AND stop_seq = smm.minid) 'Start',
    (SELECT TIME FROM test 
       WHERE shipment_id = smm.seqid AND stop_seq = smm.maxid) 'End'
FROM seqminmax smm

SeqMinMax CTE为每个“shipment_id”选择最小和最大“stop_seq”值,然后查询的其余部分构建在这些值上,以从表“test”中检索相关时间。

SQL Server 2005支持CTE(并且是SQL:2003标准功能 - 实际上没有Microsoft“发明”)。

马克

答案 2 :(得分:0)

我认为你想要第一个时间而不是'min'时间,以及最后时间而不是'max'时间我是否正确?

答案 3 :(得分:0)

SELECT C.shipment_id, C.start, B2.time AS stop FROM
(    
   SELECT A.shipment_id, B1.time AS start, A.max_stop_seq FROM
   (
      SELECT shipment_id, MIN(stop_seq) as min_stop_seq, MAX(stop_seq) as max_stop_seq 
      FROM test
      GROUP BY shipment_id
   ) AS A

   INNER JOIN 

   (
      SELECT shipment_id, stop_seq, time FROM test
   ) AS B1

   ON A.shipment_id = B1.shipment_id AND A.min_stop_seq = B1.stop_seq
) AS C

INNER JOIN

(
   SELECT shipment_id, stop_seq, time FROM test
) AS B2

ON C.shipment_id = B2.shipment_id AND C.max_stop_seq = B2.stop_seq

答案 4 :(得分:0)

select t1.shipment_id, t1.time start, t2.time [end]
from (
    select shipment_id, min(stop_seq) min, max(stop_seq) max
    from test
    group by shipment_id
) a
inner join test t1 on a.shipment_id = t1.shipment_id and a.min = t1.stop_seq 
inner join test t2 on a.shipment_id = t2.shipment_id and a.max = t2.stop_seq 

答案 5 :(得分:0)

我建议你利用row_number和PIVOT。这可能看起来很混乱,但我认为它会表现良好,而且它更能适应各种假设。例如,它不假设最新的datetime值对应于给定货件的最大stop_seq值。

with test_ranked(shipment_id,stop_seq,time,rankup,rankdown) as (
  select
    shipment_id, stop_seq, time,
    row_number() over (
      partition by shipment_id
      order by stop_seq
    ),
    row_number() over (
      partition by shipment_id
      order by stop_seq desc
    )
  from test
), test_extreme_times(shipment_id,tag,time) as (
  select
    shipment_id, 'start', time
  from test_ranked where rankup = 1
  union all
  select
    shipment_id, 'end', time
  from test_ranked where rankdown = 1
)
  select
    shipment_id, [start], [end]
  from test_extreme_times
  pivot (max(time) for tag in ([start],[end])) P
  order by shipment_id;
  go

PIVOT并不是真的需要,但它很方便。但是,请注意PIVOT表达式中的MAX不会执行任何有用的操作。每个标签只有一个[时间]值,因此MIN也可以正常工作。语法需要在此位置使用聚合函数。

附录:以下是CptSkippy解决方案的改编版,如果您有货运表,可能比使用MIN和MAX更有效:

SELECT shipment_id
    , (SELECT TOP 1 time 
        FROM test AS [b] 
        WHERE b.shipment_id = a.shipment_id 
        ORDER BY stop_seq ASC) AS [start]
    , (SELECT TOP 1 time 
        FROM test AS [b] 
        WHERE b.shipment_id = a.shipment_id 
        ORDER BY stop_seq DESC) AS [end]
FROM shipments_table AS [a];
相关问题