我要在行中获取所有输入/输出事件。我的表如下所示;
Trnevents:
<script>
document.getElementById("submit").onclick = function () { myFunction() };
function myFunction() {
$.get('@Url.Action("FromInput", "Home")', function(data) {
$('#output').replaceWith(data);
});
}
我在下面的查询中尝试过,但对该输出不满意
emp_id EVENTID DT
4176 3 2019-04-09 15:34:12.000
4176 1 2019-04-09 08:55:13.000
4176 3 2019-04-08 16:08:32.000
4176 2 2019-04-08 15:08:18.000
4176 1 2019-04-08 14:41:37.000
4176 0 2019-04-08 08:45:13.000
预期像:
select
t.emp_reader_id as empId,B.emp_name, cast(max(DT) as date) as Belongs_to,B.areaname as POINTID,
max(iif(EVENTID = 0, DT, null)) as F1, max(iif(EVENTID = 1, DT, null)) as F2, max(iif(EVENTID = 2, DT, null)) as F3, max(iif(EVENTID = 3, DT, null)) as F4,
format(dateadd(ss,Datediff(second,max(iif(EVENTID = 0, DT, null)),max(iif(EVENTID = 3, DT, null))),0),'HH:mm') as Worked from (
select
*, grp = sum(iif(EVENTID = 0, 3, 0) ) over (partition by emp_reader_id order by DT)
from Trnevents ) t inner join employee B on t.emp_reader_id=B.emp_reader_id where b.emp_reader_id=4176
group by t.emp_reader_id,t.grp,B.emp_name,t.Belongs_to,B.areaname
order by emp_reader_id,t.Belongs_to,F1,F2 asc
我需要如上所述的输出。谢谢!
Eventid:
0作为F1, 1作为F2, 2作为F3, 3作为F4
答案 0 :(得分:1)
下面的代码段应该有帮助:
SELECT emp_id,day,
TO_CHaR(to_date(F1,'HH24:MI'),'HH24:MI') F1,
TO_CHaR(to_date(F2,'HH24:MI'),'HH24:MI') F2,
TO_CHaR(to_date(F3,'HH24:MI'),'HH24:MI') F3,
TO_CHaR(to_date(F4,'HH24:MI'),'HH24:MI') F4 FROM (
SELECT emp_id,event_id,to_char(DT,'DD-MON-YYYY') day ,to_number(replace(to_char(DT,'HH24:MI'),':','.'),'99.99') dt_time from sample_event a
)
PIVOT (
avg(dt_time)
FOR event_id in (
0 F1,1 F2,2 F3,3 F4 )
)
ORDER BY DAY asc;
-- output
EMP_ID DAY F1 F2 F3 F4
4176 08-APR-2019 08:45 14:41 15:08 16:08
4176 09-APR-2019 - 08:55 - 15:34
答案 1 :(得分:1)
以下内容是在oracle数据库上。因此,不要对“ from dual”感到困惑,而以开头只是简单地模拟输入数据。同样在oracle trunc(date_time)上返回仅日期部分。首先创建mydata的是模拟输入。接下来,通过按日期获取绝对最小值/最大值获取f1f4。 Next通过说我们只想要日期与f1和f4不匹配的那些行来计算f2和f4。根据您的规则,emp + dat有2或4行。然后,final将原始源与这2个源连接起来,得到f1,f2,f3,f4。区别在于驱动程序是原始的
with mydata as (
select '4176' emp_id, '3' eventid, to_date('2019-04-09 15:34:12', 'YYYY-MM-DD HH24:MI:SS') dt from dual union all
select '4176' emp_id, '1' eventid, to_date('2019-04-09 08:55:13', 'YYYY-MM-DD HH24:MI:SS') dt from dual union all
select '4176' emp_id, '3' eventid, to_date('2019-04-08 14:41:37', 'YYYY-MM-DD HH24:MI:SS') dt from dual union all
select '4176' emp_id, '2' eventid, to_date('2019-04-08 15:08:18', 'YYYY-MM-DD HH24:MI:SS') dt from dual union all
select '4176' emp_id, '1' eventid, to_date('2019-04-08 14:41:37', 'YYYY-MM-DD HH24:MI:SS') dt from dual union all
select '4176' emp_id, '0' eventid, to_date('2019-04-08 08:45:13', 'YYYY-MM-DD HH24:MI:SS') dt from dual
),
getf1f4 as (
select emp_id, trunc(dt) dt, count(*) num_rows, min(dt) f1, max(dt) f4
from mydata
group by emp_id, trunc(dt)
),
getf2f3 as (
select ee.emp_id, getf1f4.dt,
min(ee.dt) f2,
max(ee.dt) f3
from mydata ee join getf1f4 on ee.emp_id = getf1f4.emp_id and trunc(ee.dt) = getf1f4.dt
where ee.dt not in ( getf1f4.f1, getf1f4.f4)
group by ee.emp_id, getf1f4.dt
)
select distinct
ee.emp_id, getf1f4.dt, getf1f4.f1, getf2f3.f2, getf2f3.f3, getf1f4.f4
from mydata ee
join getf1f4 on ee.emp_id = getf1f4.emp_id and trunc(ee.dt) = getf1f4.dt
left outer join getf2f3 on ee.emp_id = getf2f3.emp_id and trunc(ee.dt) = getf2f3.dt
答案 2 :(得分:1)
@dolu bolu
可以从以下查询中引用和更改所请求的sql查询:
select emp_id, day, f1, f2, f3, f4 from
(select a.emp_id,to_char(a.DT,'DD-MON-YYYY') day,
(select to_char(b.DT,'HH24:MI') from sample_event b where b.emp_id=a.emp_id and to_char(b.DT,'DD-MON-YYYY')=to_char(a.DT,'DD-MON-YYYY') and b.event_id=0 ) F1,
(select to_char(b.DT,'HH24:MI') from sample_event b where b.emp_id=a.emp_id and to_char(b.DT,'DD-MON-YYYY')=to_char(a.DT,'DD-MON-YYYY') and b.event_id=1 ) F2,
(select to_char(b.DT,'HH24:MI') from sample_event b where b.emp_id=a.emp_id and to_char(b.DT,'DD-MON-YYYY')=to_char(a.DT,'DD-MON-YYYY') and b.event_id=2 ) F3,
(select to_char(b.DT,'HH24:MI') from sample_event b where b.emp_id=a.emp_id and to_char(b.DT,'DD-MON-YYYY')=to_char(a.DT,'DD-MON-YYYY') and b.event_id=3 ) F4,
(select max(b.DT) from sample_event b where b.emp_id=a.emp_id and to_char(b.DT,'DD-MON-YYYY')=to_char(a.DT,'DD-MON-YYYY') ) max_time,
(select min(b.DT) from sample_event b where b.emp_id=a.emp_id and to_char(b.DT,'DD-MON-YYYY')=to_char(a.DT,'DD-MON-YYYY') ) min_time
from sample_event a group by a.emp_id,to_char(a.DT,'DD-MON-YYYY'))
order by day;
您可以使用datediff函数获取max_time和min_time之间的小时数