English 中文(简体)
组合和时间差异
原标题:Complicated grouping and time difference
  • 时间:2011-11-22 18:07:17
  •  标签:
  • sql
  • t-sql

So I have a log table that is putting in data like:

LogId   SiteNumber  Unit        IDNumber    LogCode     EnteredDateTime            ChangeMode           HowEntered
-----   ----------  ----        --------    -------     ----------------           ----------           -----------
851     1           16 - 0      23502       BDISCHSET   2011-11-12 11:48:08.890    Discharging Soon     SERIES
866     1           16 - 0      NULL        BDISCHRED   2011-11-12 21:45:11.657    Discharged           SERIES  
113     2           2001 - 0    12384       BDISCHSET   2011-10-28 09:27:08.773    Discharging Soon     SERIES
125     2           2001 - 0    NULL        BDISCHRED   2011-10-28 10:38:08.060    Discharged           SERIES
119     2           2002 - 0    12394       BDISCHSET   2011-10-28 10:01:12.443    Discharging Soon     SERIES
139     2           2002 - 0    NULL        BDISCHRED   2011-10-28 14:01:11.120    Discharged           SERIES
776     2           2002 - 0    12331       BDISCHSET   2011-11-10 09:08:09.443    Discharging Soon     SERIES
783     2           2002 - 0    NULL        BDISCHRED   2011-11-10 11:08:08.537    Discharged           SERIES

我需要做的是把每人的记录分类,并计算出遣散所需的时间。

For example: Unit 2002 - 0 has two different people to group, it would be:

个人12394 页: 1

排出2小时

任何帮助都将受到高度赞赏。

最佳回答

正如其他人在评论中指出的那样,我认为,我们回顾的设计/结构是巨大的。 如果你能够保证各行各行各行各行各行各行各行,即每两行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行,第二行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行。

;WITH OrderedTable AS
(
    SELECT  Unit
            , IDNumber
            , EnteredDateTime
            , ROW_Number() OVER (Partition BY Unit ORDER BY Unit) RN 
    FROM    @t -- YOUR TABLE NAME GOES HERE
)
SELECT  t1.Unit
        , t1.IDNumber
        , t1.EnteredDateTime as TimeIn
        , t2.EnteredDateTime as TimeOut
        , DATEDIFF(hour, t1.EnteredDateTime, t2.EnteredDateTime ) TimeElapsedInHours
FROM    OrderedTable t1
JOIN    OrderedTable t2 ON t1.Unit = t2.Unit AND t2.RN = t1.RN + 1
WHERE   t1.RN % 2 <> 0

但我想说清楚——我不知道这一解决办法是建立在你的实际数据基础上的。 然而,它将根据你提供的抽样数据,给你希望的结果。

即便由于你的数据模式,你也能够使用这一准确的方法,但我感到,你可能利用其中的一些东西来制定解决办法。 如果没有任何东西,这些内容将引人注意。

这里使用的测试是......

declare @t TABLE
(
    LogId   int,
    SiteNumber  int,
    Unit        varchar(50),
    IDNumber    int ,
    LogCode   varchar(50),
    EnteredDateTime  datetime,          
    ChangeMode    varchar(50),       
    HowEntered varchar(50)
)

insert into @t values (851, 1,  16 - 0 , 23502,  BDISCHSET ,  2011-11-12 11:48:08.890 ,  Discharging Soon ,  SERIES )
insert into @t values (866, 1,  16 - 0  , NULL,  BDISCHRED ,  2011-11-12 21:45:11.657 ,  Discharged ,  SERIES )
insert into @t values (113, 2,  2001 - 0 , 12384,  BDISCHSET ,  2011-10-28 09:27:08.773 ,  Discharging Soon ,  SERIES )
insert into @t values (125, 2,  2001 - 0 , NULL,  BDISCHRED ,  2011-10-28 10:38:08.060 ,  Discharged ,  SERIES )
insert into @t values (119, 2,  2002 - 0 , 12394,  BDISCHSET ,  2011-10-28 10:01:12.443 ,  Discharging Soon ,  SERIES )
insert into @t values (139, 2,  2002 - 0 , NULL,  BDISCHRED ,  2011-10-28 14:01:11.120 ,  Discharged ,  SERIES )
insert into @t values (776, 2,  2002 - 0 , 12331,  BDISCHSET ,  2011-11-10 09:08:09.443 ,  Discharging Soon ,  SERIES )
insert into @t values (783, 2,  2002 - 0 , NULL,  BDISCHRED ,  2011-11-10 11:08:08.537 ,  Discharged ,  SERIES )

;WITH OrderedTable AS
(
    SELECT  Unit
            , IDNumber
            , EnteredDateTime
            , ROW_Number() OVER (Partition BY Unit ORDER BY Unit) RN 
    FROM    @t -- YOUR TABLE NAME GOES HERE
)
SELECT  t1.Unit
        , t1.IDNumber
        , t1.EnteredDateTime as TimeIn
        , t2.EnteredDateTime as TimeOut
        , DATEDIFF(hour, t1.EnteredDateTime, t2.EnteredDateTime ) TimeElapsedInHours
FROM    OrderedTable t1
JOIN    OrderedTable t2 ON t1.Unit = t2.Unit AND t2.RN = t1.RN + 1
WHERE   t1.RN % 2 <> 0
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签