I am kind of stuck in querying my timelogs table,
the following are the sample data:
TimelogId EmployeeId RecordDate RecordTime Type
--------- ---------- ---------- ---------- ----
1 4 2016-07-01 07:18:37 1
2 4 2016-07-01 12:03:14 2
5 4 2016-07-01 12:08:02 1
6 4 2016-07-01 18:02:03 2
7 6 2016-07-19 07:24:15 1
8 5 2016-07-19 07:26:03 1
9 6 2016-07-19 12:15:26 2
10 5 2016-07-19 12:35:17 2
13 5 2016-07-19 12:36:14 1
16 6 2016-07-19 12:45:45 1
17 6 2016-07-19 17:10:22 2
18 5 2016-07-19 18:43:09 2
The required output:
Date EmployeeId Time_In Time_Out Time_In Time_Out
------- ---------- -------- -------- ------- -------
2016-07-01 4 07:18:37 12:03:14 12:08:03 18:02:03
2016-07-19 6 07:24:15 12:15:26 12:45:45 17:10:22
2016-07-19 5 07:26:03 12:35:17 12:36:14 18:43:08
Where Type (1) = time in and Type (2) = time out.
Employees are required to logout at 12nn then log back in again after a couple of minutes.
I tried using pivot based from the previous questions that i read here. Though this is the first time i tried to use pivot in tables.
select
*
FROM
(
select
EmployeeID,
RecordDate,
RecordTime,
Type
from tblTimeLogs
) d
PIVOT(
Max(RecordTime) <---- I think the problem is right around here
FOR Type in ([1],[2]) <----- plus i can't seem to rename this column names maybe i'll read a little further on this.
) piv;
Output of query:
EmployeeID RecordDate 1 2
---------- ---------- ------ ------
4 2016-07-01 12:08:02 18:02:03
5 2016-07-19 12:36:14 18:43:09
6 2016-07-19 12:45:45 17:10:22
is this possible? Thanks. :D
Edit:
as suggested by vercelli, another scenario like for example the user forgot that he/she already timed in just a couple of minutes ago so she timed in again.
e.g.
TimelogId EmployeeId RecordDate RecordTime Type
--------- ---------- ---------- ---------- ----
1 4 2016-07-01 07:18:37 1
2 4 2016-07-01 12:03:14 2
5 4 2016-07-01 12:08:02 1
6 4 2016-07-01 18:02:03 2
7 6 2016-07-19 07:24:15 1
8 5 2016-07-19 07:26:03 1
9 6 2016-07-19 12:15:26 2
10 5 2016-07-19 12:35:17 2
13 5 2016-07-19 12:36:14 1
16 6 2016-07-19 12:45:45 1
17 6 2016-07-19 17:10:22 2
18 5 2016-07-19 18:43:09 2
19 5 2016-07-20 08:13:35 1 <--- Time in
20 5 2016-07-20 08:14:35 1 <--- Timed In again
21 5 2016-07-20 12:15:12 2 <--- Time Out
I tried using the query of Mr. Vercelli:
select
EmployeeID as Emp, RecordDate, [1] as Time_In1, [2] as Time_Out1, [3] as Time_In2, [4] as Time_out2
FROM
(
select
EmployeeID,
RecordDate,
RecordTime,
ROW_NUMBER () over (partition by EmployeeId, RecordDate order by RecordTime, type) as rn
from tblTimeLogs
) d
PIVOT(
max(RecordTime)
FOR rn in ([1],[2],[3],[4])
) as piv;
Output of the new query:
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_Out2
---- ---------- --------- --------- -------- ---------
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
5 2016-07-20 08:13:35 08:14:35 12:15:12 Null <--- the problem is right around this portion
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
Expected Output:
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_Out2
---- ---------- --------- --------- -------- ---------
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
5 2016-07-20 08:13:35 12:15:12 08:14:35 Null <--- the second time in would fall on the second 5th column (Time_In2) since the **Type** value is = 1
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
Thanks for the help guys :D. I'm learning new things from this problem.
I think this is what you are looking for. Demo:
select
EmployeeID as Emp, RecordDate, [1] as Time_In1, [2] as Time_Out1, [3] as Time_In2, [4] as Time_out2
FROM
(
select
EmployeeID,
RecordDate,
RecordTime,
ROW_NUMBER () over (partition by EmployeeId, RecordDate order by RecordTime, type) as rn
from tblTimeLogs
) d
PIVOT(
max(RecordTime)
FOR rn in ([1],[2],[3],[4])
) as piv;
OUPUT
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_out2
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
DEMO HERE
---Table Creattion Scripts
create table #test
(
employeeid int,
recorddate date,
recordtime time,
typee int
)
insert into #test values( 4 , '2016-07-01', '07:18:37', 1)
insert into #test values( 4 , '2016-07-01', '12:03:14', 2)
insert into #test values( 4 , '2016-07-01', '12:08:02', 1)
insert into #test values( 4 , '2016-07-01', '18:02:03', 2)
Query
;with cte
as
(
select
employeeid,
max(recorddate) as recorddate,
min(recordtime) as timein,
max(recordtime) as Timein1 ,
lead(min(recordtime)) over (partition by employeeid order by min(recordtime)) as 'timeout1',
lead(max(recordtime)) over (partition by employeeid order by max(recordtime)) as 'timeout2'
from #test
group by
employeeid,typee
)
select employeeid,recorddate,timein,timeout1,timein1,timeout2
from cte
where timeout1 is not null and timeout2 is not null
Related
How To Arrange Table Data in Parent_Id And Child_Id Wise Via Sql-Server Query?
My Query
select CLevel_Id,Category_Name,Parent_Id,Child_Id,Level_Id from Category_Level
order by Parent_Id asc
Current Output
CLevel_Id Category_Name Parent_Id Child_Id Level_Id
12 Jewelry 1 0 1
14 Rings 2 1 2
15 Men-Rings 3 2 3
17 Women-Rings 4 2 3
18 Earrings 5 1 2
20 Women-Earings 6 5 3
1013 Metal-Fashion 7 3 4
1015 Diamond-Fashion 8 4 4
1016 Semi-Set 9 6 4
Expected Output
CLevel_Id Category_Name Parent_Id Child_Id Level_Id
12 Jewelry 1 0 1
14 Rings 2 1 2
15 Men-Rings 3 2 3
1013 Metal-Fashion 7 3 4
17 Women-Rings 4 2 3
1015 Diamond-Fashion 8 4 4
18 Earrings 5 1 2
20 Women-Earings 6 5 3
1016 Semi-Set 9 6 4
please help me
use a Recursive CTE.
Assuming you have max 9 per level. Using single digit as the seq level. If you have more than 9, you will need to use 2 digits like 01, 02 etc
; with
rcte as
(
-- Anchor member, seq = 1
select *, seq = convert(varchar(100), '1')
from Category_Level
where Child_Id = 0
union all
-- recursive member, concatenate to the seq
select c.*, seq = convert(varchar(100),
r.seq
+ convert(varchar(10), row_number() over (partition by r.seq
order by c.Child_Id)))
from Category_Level c
inner join rcte r on c.Child_Id = r.Parent_Id
)
select *
from rcte
order by seq
/* RESULT
CLevel_Id Category_Name Parent_Id Child_Id Level_Id seq
----------- -------------------- ----------- ----------- ----------- -------
12 Jewelry 1 0 1 1
14 Rings 2 1 2 11
15 Men-Rings 3 2 3 111
1013 Metal-Fashion 7 3 4 1111
17 Women-Rings 4 2 3 112
1015 Diamond-Fashion 8 4 4 1121
18 Earrings 5 1 2 12
20 Women-Earings 6 5 3 121
1016 Semi-Set 9 6 4 1211
(9 rows affected)
*/
I have datatable in the following format
Id LocationId UserName Startdate
1 10 xz 2017-02-21 09:05:20
2 10 xz 2017-02-21 09:15:20
3 10 xz 2017-02-21 09:25:20
4 10 xz 2017-02-21 09:35:20
5 11 xy 2017-02-21 09:45:20
6 11 xy 2017-02-21 09:55:20
7 11 xy 2017-02-21 10:05:20
8 11 xy 2017-02-21 10:15:20
9 10 xz 2017-02-21 10:15:20
10 10 xz 2017-02-21 10:25:20
I want to calculate starttime end time and differnce of time ,
and have show record as
Id locationId startdate starttime endtime timespan
1 10 2017-02-21 09:05 09:35:20 0hr 30min0sec
2 11 2017-02-21 09:05 09:35:20 0hr 30min0sec
3 10 2017-02-21 10:15 10:25 0hr 10min 0sec
If you mean SQL, I hope the following can help you:
WITH TEST AS(
SELECT *,
CASE WHEN LAG(LocationId,1) OVER(ORDER BY ID) != LocationId
OR LAG(LocationId,1) OVER(ORDER BY ID) IS NULL
THEN ID
ELSE 0 END FLAG
FROM [dbo].[DATATABLE]),`
`GroupData AS(
SELECT [LocationId],[UserName],[startdate],MAX(MI.FLAG) FLAG FROM TEST JOIN
(SELECT FLAG FROM TEST
WHERE FLAG != 0) MI ON TEST.ID >= MI.FLAG
GROUP BY [ID],[LocationId],[UserName],[startdate],TEST.FLAG)`
`SELECT LocationID,MIN([startdate]),MAX([startdate]) FROM GroupData
GROUP BY FLAG,LocationID
My problem is that I don't get the first value in the interval.
The values in the increasing interval in table 1 is Id1-Id3, Id4-Id5, Id9-Id13 and Id14-Id15, but
the result i Table 2 is Id2-Id3, Id5,Id10-Id13 and Id 15 missing Id1, Id4, Id9 and Id14.
I just can't figure out how to get the first value included in the query.
PSS Table containing the datasource:
ID Date Value
1 2012-04-20 0,166666666666667
2 2012-04-25 0,2
3 2012-04-28 0,235294117647059
4 2012-05-05 0,111111111111111
5 2012-05-07 0,416666666666667
6 2012-05-08 0,25
7 2012-05-09 0,166666666666667
8 2012-05-10 0,142857142857143
9 2012-05-11 0,125
10 2012-05-12 0,375
11 2012-05-13 0,5
12 2012-05-14 0,625
13 2012-05-15 0,75
14 2012-05-16 0,625
15 2012-05-17 0,75
And this query:
SELECT Id, Date, Value
FROM PSS p
WHERE p.Value >=
(SELECT Value
FROM PSS
WHERE Id = p.Id-1)
Resulting in tabel 2:
Id Date Value
2 2012-04-25 0,2
3 2012-04-28 0,235294117647059
5 2012-05-07 0,416666666666667
10 2012-05-12 0,375
11 2012-05-13 0,5
12 2012-05-14 0,625
13 2012-05-15 0,75
15 2012-05-17 0,75
Try this
select id, date, value
from PSS p1
where value >= (select p2.amount from PSS p2 where p2.id=p1.id - 1)
or value <= (select p3.amount from PSS p3 where P3.id=p1.id + 1)
I am running one query and getting following results
Select *
from
(Select ROW_NUMBER() over
(partition by [HOSP_CODE],[MRN]
order by [HOSP_CODE],MRN,ADM_DATETIME ) as rownumber,*
from Live.tempnewbornbundling) as a
order by [HOSP_CODE],MRN,ADM_DATETIME
DATA
Rownumber key MRn hospcode adm_date sep_date Sequence
--------- ------- ------ -------- ---------------- ---------------- --------
1 7099222 544607 3 22/07/2011 04:55 22/07/2011 10:44 First
2 7099223 544607 3 22/07/2011 10:45 25/07/2011 19:43 Middle
3 7099224 544607 3 25/07/2011 19:44 26/07/2011 11:29 Middle
4 7099225 544607 3 27/07/2011 12:30 27/07/2011 19:30 First
5 7099226 544607 3 27/07/2011 19:31 28/07/2011 19:31 Final
1 7099227 559282 3 03/07/2011 22:50 03/07/2011 23:51 First
2 7099228 559282 3 03/07/2011 23:52 04/07/2011 15:30 Middle
3 7099229 559282 3 04/07/2011 15:31 04/07/2011 17:59 Final
4 7099230 559282 3 05/07/2011 18:00 05/07/2011 18:05 First
5 7099231 559282 3 05/07/2011 18:06 09/07/2011 14:58 Final
How can I further make partitions and assign row numbers based on sequence values,
for example I want to restart rownumber each time there is first in same group HOSPcode, MRN
Rownumber key MRn hospcode adm_date sep_date Sequence New rownumber
--------- ------- ------ -------- ---------------- ---------------- -------- -------------
1 7099222 544607 3 22/07/2011 04:55 22/07/2011 10:44 First 1
2 7099223 544607 3 22/07/2011 10:45 25/07/2011 19:43 Middle 2
3 7099224 544607 3 25/07/2011 19:44 26/07/2011 11:29 Middle 3
4 7099225 544607 3 27/07/2011 12:30 27/07/2011 19:30 First 1
5 7099226 544607 3 27/07/2011 19:31 28/07/2011 19:31 Final 2
1 7099227 559282 3 03/07/2011 22:50 03/07/2011 23:51 First 1
2 7099228 559282 3 03/07/2011 23:52 04/07/2011 15:30 Middle 2
3 7099229 559282 3 04/07/2011 15:31 04/07/2011 17:59 Final 3
4 7099230 559282 3 05/07/2011 18:00 05/07/2011 18:05 First 1
5 7099231 559282 3 05/07/2011 18:06 09/07/2011 14:58 Final 2
You should be able to do this with a recursive CTE
You will want to do the recursion on the rownumber (that will make sure you end), and then just keep incrementing a new rownumber column everytime you recurse, resetting when you hit first. I believe the below answer should work. I would store the previous query in a temp table
WITH FinalValues (RowNumber, Key, MRN, HospCode, adm_date, sep_date,
sequence, NewRowNum)
AS
(
-- Anchor member definition
SELECT RowNumber, Key, MRN, HospCode, adm_date, sep_date,
sequence, 1 AS NewRowNum
FROM PreviousQuery
WHERE RowNumber = 1
UNION ALL
-- Recursive member definition
SELECT P.RowNumber, P.Key, P.MRN, P.HospCode, P.adm_date, P.sep_date,
P.sequence,
CASE WHEN P.sequence = 'First' THEN 1 ELSE NewRowNum + 1 END AS NewRowNum
FROM PreviousQuery AS P
INNER JOIN FinalValues
ON FinalValues.RowNumber = P.RowNumber + 1
)
-- Statement that executes the CTE
SELECT *
FROM FinalValues;
GO
i have an table called employees
empID name startdate Middate DEptID
1 kumar 2011-04-30 2005-09-13 1
2 kiran 2011-05-23 2006-08-13 1
3 kishor 2008-04-13 2009-04-23 6
4 anu 2009-09-03 2000-06-13 1
5 priya 2010-05-04 2010-04-13 6
6 sweth 2011-07-13 2007-12-16 1
7 manu 2011-08-15 2006-12-07 6
Is there any way i can update this table so that
startdate = Middate for all the employees
empID name startdate Middate DEptID
1 kumar 2005-09-13 2005-09-13 1
2 kiran 2006-08-13 2006-08-13 1
3 kishor 2009-04-23 2009-04-23 6
4 anu 2000-06-13 2000-06-13 1
5 priya 2010-04-13 2010-04-13 6
6 sweth 2007-12-16 2007-12-16 1
7 manu 2006-12-07 2006-12-07 6
pls let me know how can i solve this
thanks
prince
update employees set startdate=middate