如何在SQL中使用筛选数据获取内部联接查询?

时间:2017-01-27 18:28:23

标签: sql-server-2012

我需要关于OP10首次进入的开始时间和最后进入的结束时间的数据。我们(OP10开始时间:上午10点和结束时间:上午11点10分)。与明智的OP20和OP30类似。为了避免多次报告数据(OP10),给我ITS启动&结束时间就足够了。

SQL表格结果:

dt = Convert.ToDateTime(ob.ExecuteQuery(sqlQuery, CommandType.Text).Tables[0].Rows[0]["YourColumnName"]); //You´ll have to check out where is the value you want and replace on the Rows[0]

需要输出如下格式:

+---------+------------+-----------------------------+-----------------------------+
|   Line     Operation |     StartDate               |   EndDate                   |
+---------+------------+-----------------------------+-----------------------------+
|   ACOE  | OP10       | 2017-01-27 10:00:00.000     |  2017-01-27  10:10:00.000   |
|   ACOE  | OP10       | 2017-01-27 11:00:00.000     |  2017-01-27  11:10:00.000   |
|   ACOE  | OP20       | 2017-01-27 11:10:00.000     |  2017-01-27  11:15:00.000   |
|   ACOE  | OP20       | 2017-01-27 11:20:00.000     |  2017-01-27  11:25:00.000   |
|   ACOE  | OP30       | 2017-01-27 12:10:00.000     |  2017-01-27  12:15:00.000   |
|   ACOE  | OP30       | 2017-01-27 12:20:00.000     |  2017-01-27  12:25:00.000   |
+---------+------------+-----------------------------+-----------------------------+

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT Line, Operation, StartDate = MIN(StartDate), EndDate=MAX(EndDate)
GROUP BY Line, Operation
ORDER BY Line, Operation

让我知道 侨