English 中文(简体)
采用Maclog 时间发现,时间好,时间差。
原标题:Using Mac log time find out good hours and bad hours in sql

这是我的投入表:entergraph here。 我需要用罐头问询获得机器好时和坏时。 我正在审理此事。

 select  machinename,
               min(case when status =  Good  then logtime end) as good
        ,       max(case when status =  Bad  then logtime end) as bad
        ,       cast(min(logtime ) as date) as date
        from    Machine_proxy_report
        group by
                machinename,cast(log_time as date);

我需要获得这样的产出:

Machinename status logtime
Mac1 good 2024-03-07 01:10:00
Mac1 good 2024-03-07 02:15:00
Mac1 good 2024-03-07 03:20:00
Mac1 bad 2024-03-07 04:10:00
Mac1 good 2024-03-07 05:10:00
Mac1 good 2024-03-07 11:10:00
Mac1 bad 2024-03-07 15:10:00
Mac1 good 2024-03-07 16:10:00
Mac1 good 2024-03-07 23:10:00
最佳回答

此处采用窗口功能 优先于(>,datediff(PIVOT

<>说明: 没有跨过天数的逻辑(即午夜)。 如果需要,则不清楚。

<><>Example>

Select *
 From  (
        Select Date  = convert(date,logtime)
              ,MachineName
              ,Status
              ,Hours = datediff(hour,logtime,lead(logtime,1) over (partition by MachineName order by LogTime))
         From YourTable
       ) src
 Pivot ( sum(Hours) for Status in ([Good],[Bad]) ) pvt
 

Results

Date        MachineName Good    Bad
2024-07-03  Mac1        20      2
问题回答

以预期结果为例,你没有显示<条码>。 例如,你按日期分组。 举例来说,按日期进行分组,并计算整个午夜的幅度。

段 次 页 次

( 2024-03-07 23:10:00 )->( 2024-03-07 23:59:59 ) date 2024-03-07
( 2024-03-08 00:00:00 )->( 2024-03-08 03:10:00 ) date 2024-03-08

See example

select Machine,logDate
  ,sum(case when Status= good  then elong end) GoodLong
  ,sum(case when Status= bad  then elong end) BadLong
  ,round(cast(sum(case when Status= good  then elong end) as float)/3600.0,4) GoodLongH
  ,round(cast(sum(case when Status= bad  then elong end) as float)/3600.0,4) BadLongH
from(
  select *,cast(logtime as date) LogDate
    ,datediff(s,logtime,lead(logtime,1,logtime)over(order by logtime)) elong
 from( -- for over-midnight periods
   select Machine,  Status,n
    ,case  when n=1 and logDate<>nextDate then 
       cast(nextDate as datetime)
     else logTime
     end LogTime 
    ,case when n=0 and logDate<>nextDate then dateadd(s,-1,cast(nextDate as datetime))
     else nextTime
     end nextTime
   from(
     select *
       ,cast(logtime as date) logDate
       ,lead(logtime,1,logtime)over(order by logtime) nextTime
       ,cast(lead(logtime,1,logtime)over(order by logtime) as date) nextDate
     from Logwork) a
     left join (select 0 n union all select 1 )t(n)
        on (n=0  ) or (n=1 and logDate<>nextDate )
   )b
 )с
group by Machine,LogDate

每天24小时

insert into LogWork values
 ( Mac1 , good , 2024-03-07 1:10:05 )
,( Mac1 , good , 2024-03-07 2:15:00 ) 
,( Mac1 , good , 2024-03-07 3:20:00 ) 
,( Mac1 , bad , 2024-03-07 4:10:00 ) 
,( Mac1 , good , 2024-03-07 5:10:00 ) 
,( Mac1 , good , 2024-03-07 11:10:00 ) 
,( Mac1 , bad , 2024-03-07 15:10:00 ) 
,( Mac1 , good , 2024-03-07 16:10:00 ) 
,( Mac1 , good , 2024-03-07 23:10:00 )
,( Mac1 , bad , 2024-03-08 03:10:00 )
,( Mac1 , good , 2024-03-08 05:10:00 )
;

产出

Machine logDate GoodLong BadLong GoodLongH BadLongH
Mac1 2024-03-07 74995 7200 20.8319 2
Mac1 2024-03-08 11400 7200 3.1667 2

计算th夜产出的顺序

Machine Status n LogTime nextTime LogDate elong
Mac1 good 0 2024-03-07 01:10:05.000 2024-03-07 02:15:00.000 2024-03-07 3895
Mac1 good 0 2024-03-07 02:15:00.000 2024-03-07 03:20:00.000 2024-03-07 3900
Mac1 good 0 2024-03-07 03:20:00.000 2024-03-07 04:10:00.000 2024-03-07 3000
Mac1 bad 0 2024-03-07 04:10:00.000 2024-03-07 05:10:00.000 2024-03-07 3600
Mac1 good 0 2024-03-07 05:10:00.000 2024-03-07 11:10:00.000 2024-03-07 21600
Mac1 good 0 2024-03-07 11:10:00.000 2024-03-07 15:10:00.000 2024-03-07 14400
Mac1 bad 0 2024-03-07 15:10:00.000 2024-03-07 16:10:00.000 2024-03-07 3600
Mac1 good 0 2024-03-07 16:10:00.000 2024-03-07 23:10:00.000 2024-03-07 25200
Mac1 good 0 2024-03-07 23:10:00.000 2024-03-07 23:59:59.000 2024-03-07 3000
Mac1 good 1 2024-03-08 00:00:00.000 2024-03-08 03:10:00.000 2024-03-08 11400
Mac1 bad 0 2024-03-08 03:10:00.000 2024-03-08 05:10:00.000 2024-03-08 7200
Mac1 good 0 2024-03-08 05:10:00.000 2024-03-08 05:10:00.000 2024-03-08 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: ...

热门标签