按组SQL查询从上一行中减去日期

时间:2019-10-02 20:53:01

标签: sql-server

数据

id      date        
2380    10/30/12 09:00:00 
2380    10/30/12 09:05:00   
2380    10/30/12 09:10:00   
2380    10/30/12 09:15:00    
2381    10/30/12 10:00:00   
2381    10/30/12 10:05:00  
2381    10/30/12 10:10:00   
2381    10/30/12 10:15:00   
2382    10/30/12 11:00:00
2382    10/30/12 11:05:00
2382    10/30/12 10:10:00
2382    10/30/12 10:15:00

我想要以下解决方案

id      date                 duration        
2380    10/30/12 09:00:00    00:00:00 
2380    10/30/12 09:05:00    00:05:00   
2380    10/30/12 09:10:00    00:10:00
2380    10/30/12 09:15:00    00:15:00
2381    10/30/12 10:00:00    00:00:00
2381    10/30/12 10:05:00    00:05:00
2381    10/30/12 10:10:00    00:10:00
2381    10/30/12 10:15:00    00:15:00
2382    10/30/12 11:00:00    00:00:00
2382    10/30/12 11:05:00    00:05:00
2382    10/30/12 10:10:00    00:10:00
2382    10/30/12 10:15:00    00:10:00

我试图理解以下线程的逻辑,但很难理解。

Substract date from previous row by group (using R)

select id, date, date - (select min(date) from date group by id) as duration 
from date

我最近得到的是一个ID。

2 个答案:

答案 0 :(得分:3)

尝试下面的示例,希望这就是您要查找的输出,

private TableRow createItemRow() {

    TableRow row = new TableRow(getContext());
    row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

    //Create linearlayout wrapper
    LinearLayout wrapperLayout = new LinearLayout(getContext());
    wrapperLayout.setOrientation(LinearLayout.HORIZONTAL); 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(60, TableLayout.LayoutParams.MATCH_PARENT)

    //Create a + button and set it's id to ("+" + StockItem.Id)
    Button butPlus = new Button(getContext());
    butPlus.setText("+");
    wrapperLayout.addView(butPlus);//<-------------------------Button is too big for the wrapper, even if the layout parameters are set to match parent

    //add the next buttons the same way then add it to the wrapper
    //after all the buttons are added to the wrapper, add the wrapper to the row
    row.addView(wrapperLayout);

    //adding the other view elements (working correctly) to the row
    ...
    return row;

}

答案 1 :(得分:0)

我的方法的关键是为每个ID(称为 ReferenceDate )找到最小日期值。然后,我将主表加入其中,并使用DATEDIFF()函数进行日期数学运算,并使用{<3}}函数使用样式将结果转换为 hh:mi:ss > 108 。这是CONVERT()

IF OBJECT_ID('tempdb.dbo.#MyTable', 'U') IS NOT NULL
    DROP TABLE #MyTable;

CREATE TABLE #MyTable
(
    id INTEGER NOT NULL
  , date DATETIME NOT NULL
);
INSERT INTO #MyTable (id, date) VALUES (2380, '10/30/12 09:00:00');
INSERT INTO #MyTable (id, date) VALUES (2380, '10/30/12 09:05:00');
INSERT INTO #MyTable (id, date) VALUES (2380, '10/30/12 09:10:00');
INSERT INTO #MyTable (id, date) VALUES (2380, '10/30/12 09:15:00');
INSERT INTO #MyTable (id, date) VALUES (2381, '10/30/12 10:00:00');
INSERT INTO #MyTable (id, date) VALUES (2381, '10/30/12 10:05:00');
INSERT INTO #MyTable (id, date) VALUES (2381, '10/30/12 10:10:00');
INSERT INTO #MyTable (id, date) VALUES (2381, '10/30/12 10:15:00');
INSERT INTO #MyTable (id, date) VALUES (2382, '10/30/12 11:00:00');
INSERT INTO #MyTable (id, date) VALUES (2382, '10/30/12 11:05:00');
INSERT INTO #MyTable (id, date) VALUES (2382, '10/30/12 10:10:00');
INSERT INTO #MyTable (id, date) VALUES (2382, '10/30/12 10:15:00');
INSERT INTO #MyTable (id, date) VALUES (2382, '10/30/12 12:15:00');
INSERT INTO #MyTable (id, date) VALUES (2382, '10/30/12 10:15:30');

SELECT     a.*
         , CONVERT(NVARCHAR(8), a.date - b.ReferenceDate, 108) AS duration
FROM       #MyTable AS a
INNER JOIN (
    SELECT id, MIN(date) AS ReferenceDate 
    FROM #MyTable GROUP BY id) AS b ON a.id = b.id;