如何获得每10分钟数据sql查询的最高记录

时间:2016-03-20 08:02:19

标签: sql-server

 WITH Cte AS(
    SELECT *,
        rn = ROW_NUMBER() OVER(
                PARTITION BY cdt, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdt), 0)
                ORDER BY cdt DESC 
             )
    FROM brands
)
SELECT
    brandID, brandname, cdt, udt, brandstatus, addedby
FROM Cte
WHERE rn = 1 and cdt between '2013-11-01 00:00:00' and '2013-11-16 00:00:00'  ORDER BY brandId

我的表格如下:

ID  brandname     cdt                          udt  brandstatus addedby
1   khasim  2013-11-01 19:14:18.120 2013-11-15 19:14:18.123 1   1
2   khasim  2013-11-01 19:14:18.121 2013-11-15 19:14:18.123 1   1
3   khasim  2013-11-01 19:14:18.122 2013-11-15 19:14:18.123 1   1          
4   khasim  2013-11-01 19:14:18.123 2013-11-15 19:14:18.123 1   1
5   khasim  2013-11-02 19:17:57.700 2013-11-15 19:17:57.700 1   2
6   tanveer 2013-11-03 19:18:05.947 2013-11-15 19:18:05.947 1   2
7   abcdef  2013-11-04 20:50:06.783 2013-11-15 20:50:06.787 1   4

我希望获得x小时/天范围内的每n分钟数据。如果n = 10,它应该返回x = 5小时之间10分钟间隔的最高值。

enter image description here

2 个答案:

答案 0 :(得分:2)

对于n = 10

for i in 0...n - 1 {
    // do stuff, n is not changed
}

答案 1 :(得分:0)

您可以尝试使用像

这样的日期值进行分组

(适用编辑)

WITH cte as (
 SELECT ID, brandname, cdt, udt, brandstatus, addedby,
 DATEDIFF(MINUTE,'2000-01-01', cdt)/10 step
 FROM #tbl
)
SELECT ID, brandname, cdt, udt, brandstatus, addedby FROM cte a
WHERE ID=(SELECT TOP 1 ID FROM cte b 
          WHERE b.brandname=a.brandname
            AND b.step=a.step
          ORDER BY b.cdt DESC)

输出:

ID brandname cdt                 udt                 brandstatus addedby 
-- --------- ------------------- ------------------- ----------- ------- 
3  khasim    2013-11-01 19:14:18 2013-11-15 19:14:18 1           1       
5  khasim    2013-11-02 19:17:57 2013-11-15 19:17:57 1           2       
6  tanveer   2013-11-03 19:18:05 2013-11-15 19:18:05 1           2       
7  abcdef    2013-11-04 20:50:06 2013-11-15 20:50:06 1           4       

(2.编辑):

WITH cte as (
 SELECT ID, brandname, cdt, udt, brandstatus, addedby,
 DATEDIFF(MINUTE,'2000-01-01', cdt)/10 step
 FROM #tbl
)
SELECT ID, brandname, cdt, udt, brandstatus, addedby FROM cte a
WHERE ID=(SELECT TOP 1 ID FROM cte b 
          WHERE b.step=a.step
          ORDER BY b.cdt DESC)

输出:

ID brandname cdt           udt                       brandstatus addedby 
-- --------- ------------------- ------------------- ----------- ------- 
3  khasim    2013-11-01 19:14:18 2013-11-15 19:14:18 1           1       
5  khasim    2013-11-02 19:17:57 2013-11-15 19:17:57 1           2       
6  tanveer   2013-11-03 19:18:05 2013-11-15 19:18:05 1           2       
7  abcdef    2013-11-04 20:50:06 2013-11-15 20:50:06 1           4  
16天内

(3.编辑)最大cdt

WITH cte as (
 SELECT ID, brandname, cdt, udt, brandstatus, addedby,
 DATEDIFF(day,'2013-11-01', cdt)/16 step
 FROM #tbl
)
SELECT ID, brandname, cdt, udt, brandstatus, addedby, step FROM cte a
WHERE ID=(SELECT TOP 1 ID FROM cte b 
          WHERE b.step=a.step
          ORDER BY b.cdt DESC)