我如何使用sql查询以下内容

时间:2016-06-22 16:02:11

标签: sql-server

我在一列中的数据表sampletime和另一列中的样本值包含如下

之类的数据
sampletime            value
----------------------------
2016-03-02 08:31:14     1 
2016-03-02 09:31:14     2
2016-03-02 12:31:14     3
2016-03-04 08:31:14     4
2016-03-04 09:31:14     5
2016-03-05 08:31:14     3

我每天需要两个最短的采样时间。我怎么组合?

查询

SELECT rn.sampletime AS stime 
FROM   rn_qos_data_0007 rn 
       INNER JOIN s_qos_data qos 
               ON qos.table_id = rn.table_id 
                  AND qos.qos = 'QOS_CPU_USAGE' 
                  AND Substring(qos.origin, 1, 4) = 'A0C3' 
                  AND qos.host = '10.98.48.100' 
WHERE  rn.sampletime BETWEEN '2016/01/01' AND '2016/06/22' 
GROUP  BY rn.sampletime 

1 个答案:

答案 0 :(得分:0)

您需要ROW_NUMBER窗口功能

Select * From
(
select row_number()over(partition by cast(sampletime as date) order by sampletime) RN,*
From ..
) A
Where RN <=2