Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Transferring data from one table to another

Author  Topic 

pnasz
Posting Yak Master

101 Posts

Posted - 2013-04-16 : 03:19:29
ID Date TimeIn TimeOut
123 15/04/2013 08:00 11:55
123 15/04/2013 08:10 11:58
123 15/04/2013 16:00 22:05
123 15/04/2013 16:03 22:10

What to transfer data from the above table in the format
Given Below


ID Date In[Shift1] Out[Shift1] In[Shift2] Out[Shift2]
123 15/04/2013 08:00 11:58 16:00 22:10

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-16 : 03:37:35
--This??
SELECT ID
,[Date]
, MIN(CASE WHEN TimeIn BETWEEN '08:00:00' AND '12:00:00' THEN TimeIn END) Shift1In
, MAX(CASE WHEN TimeOut BETWEEN '08:00:00' AND '12:00:00' THEN [TimeOut] END) Shift1Out
, MIN(CASE WHEN TimeIn BETWEEN '12:00:00' AND '23:59:59' THEN TimeIn END) Shift2In
, MAX(CASE WHEN TimeOut BETWEEN '12:00:00' AND '23:59:59' THEN [TimeOut] END) Shift2Out
FROM @Shifts
GROUP BY ID, [Date]


--
Chandu
Go to Top of Page

pnasz
Posting Yak Master

101 Posts

Posted - 2013-04-16 : 07:56:25
ID Date TimeIn
123 15/04/2013 08:00
123 15/04/2013 08:10
123 15/04/2013 10:00
123 15/04/2013 12:00
123 15/04/2013 16:30
123 15/04/2013 17:00
123 15/04/2013 18:00
123 15/04/2013 20:00
123 15/04/2013 22:30

What will be if the table in this format and there are two shift
Shift 1=9:00-12:00
SHift 2=16:30-22:30

How do i tranfer the data from the above table in format given below.

ID Date IN-SHIFT1 OUT-SHIFT1 IN-SHIFT2 OUT-SHIFT2
123 15/04/2013 8:00 12:00 16:30 22:30
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-16 : 08:14:14
[code]DECLARE @Shifts TABLE(ID INT, [Date] DATE, TimeIn TIME)
insert into @Shifts
SELECT 123, '15/04/2013', '08:00:00' union all
SELECT 123, '15/04/2013', '08:10:00' union all
SELECT 123, '15/04/2013', '10:00:00' union all
SELECT 123, '15/04/2013', '12:00:00' union all
SELECT 123, '15/04/2013', '16:30:00' union all
SELECT 123, '15/04/2013', '17:00:00' union all
SELECT 123, '15/04/2013', '18:00:00' union all
SELECT 123, '15/04/2013', '20:00:00' union all
SELECT 123, '15/04/2013', '22:30:00'
/*What will be if the table in this format and there are two shift
Shift 1=9:00-12:00
SHift 2=16:30-22:30
How do i tranfer the data from the above table in format given below.
ID Date IN-SHIFT1 OUT-SHIFT1 IN-SHIFT2 OUT-SHIFT2
123 15/04/2013 8:00 12:00 16:30 22:30 */
SELECT ID
,[Date]
, MIN(CASE WHEN TimeIn <= '12:00:00' THEN TimeIn END) Shift1In
, MAX(CASE WHEN TimeIn <= '12:00:00' THEN TimeIn END) Shift1Out
, MIN(CASE WHEN TimeIn BETWEEN '16:30:00' AND '22:30:00' THEN TimeIn END) Shift2In
, MAX(CASE WHEN TimeIn BETWEEN '16:30:00' AND '22:30:00' THEN TimeIn END) Shift2Out
FROM @Shifts
GROUP BY ID, [Date][/code]

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-16 : 10:45:27
i think this is safer considering the first sample data too


SELECT ID
,[Date]
, MIN(CASE WHEN TimeIn <= '12:00:00' THEN TimeIn END) Shift1In
, MAX(CASE WHEN TimeOut <= '12:00:00' THEN TimeOut END) Shift1Out
, MIN(CASE WHEN TimeIn > '12:00:00' THEN TimeIn END) Shift2In
, MAX(CASE WHEN TimeOut > '12:00:00' THEN TimeOut END) Shift2Out
FROM @Shifts
GROUP BY ID, [Date]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -