SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Transferring data from one table to another
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnasz
Yak Posting Veteran

95 Posts

Posted - 04/16/2013 :  03:19:29  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 04/16/2013 :  03:37:35  Show Profile  Reply with Quote
--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
Yak Posting Veteran

95 Posts

Posted - 04/16/2013 :  07:56:25  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 04/16/2013 :  08:14:14  Show Profile  Reply with Quote
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]


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 04/16/2013 :  10:45:27  Show Profile  Reply with Quote
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

Edited by - visakh16 on 04/16/2013 10:46:34
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000