English 中文(简体)
在T-SQL中将两个日期时间字段组合成两个分开的列
原标题:Combining two datetime fields into two separate columns in T-SQL

我正在尝试创建一个视图,显示具有开始和结束时间的事件。此视图应从现有表中收集,该表仅具有事件时间字段和事件类型字段。

因此,当前的事件表格看起来像这样:

EventTime       | BooleanField
------------------------------
1/1/2010 6:00AM        1
1/2/2010 6:00AM        0
1/3/2010 6:00AM        1
1/4/2010 6:00AM        1
1/5/2010 6:00AM        0

由此产生的结果应当如此看待。

StartTime       | EndTime
-----------------------------
1/1/2010 6:00AM   1/2/2010 6:00AM
1/3/2010 6:00AM   1/5/2010 6:00AM

因此,该视图应显示布尔字段为真的时期。

在SQL Server 2008中是否有简单解决方案实现此功能?

谢谢你的帮助!

最佳回答

你可以尝试像这样做(完整示例

DECLARE @EventTable TABLE(
        EventTime DATETIME,
        BooleanField INT
)

INSERT INTO @EventTable (EventTime,BooleanField) SELECT  1/1/2010 6:00AM ,1 
INSERT INTO @EventTable (EventTime,BooleanField) SELECT  1/2/2010 6:00AM ,0 
INSERT INTO @EventTable (EventTime,BooleanField) SELECT  1/3/2010 6:00AM ,1 
INSERT INTO @EventTable (EventTime,BooleanField) SELECT  1/4/2010 6:00AM ,1 
INSERT INTO @EventTable (EventTime,BooleanField) SELECT  1/5/2010 6:00AM ,0

;WITH Dates AS (
        SELECT  *,
                (SELECT MIN(EventTime) FROM @EventTable WHERE EventTime > e.EventTime AND BooleanField = 0) EndDate
        FROM    @EventTable e
        WHERE   BooleanField = 1
)
SELECT  MIN(EventTime) StartDate,
        EndDate
FROM    Dates
GROUP BY EndDate
问题回答

请试试这个:

SELECT MIN(iq.StartTime), iq.EndTime
FROM 
(
    SELECT e1.EventTime AS StartTime, MIN(e2.EventTime) AS EndTime
    FROM EventTable e1
    LEFT JOIN EventTable e2 ON e2.BooleanField = 0 AND e2.EventTime > e1.EventTime
    WHERE e1.BooleanField = 1
    GROUP BY e1.EventTime 
) iq
GROUP BY iq.EndTime

这将确保您获得每个EndTime的最宽可能区间。我非常确定不可能将其简化为单个SELECT查询,因为需要两个GROUP BY子句 - 即使考虑其他示例(这些示例使用两个不同的聚合函数隐式分组,因此等效于此示例)。

怎么样? (Zhěn me yàng?)

  Select s.EventTime StartTime,
         e.EventTime EndTime
  From EventTable s
     Left Join EventTable e 
         On s.BooleanFiels = 1
            And e.BooleanField = 0
            And e.EventTime =
               (Select Min(EventTime) 
                From EventTable 
                Where e.BooleanField = 0
                   And EventTime > s.EventTime)
  Order By s.EventTime




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签