SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group or sum date column (ATTLOG)


Group or sum date column (ATTLOG)

Author
Message
x_jhofran_x
x_jhofran_x
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 70
FILE ATTLOG.txt

TARJETA | HORA | FECHA | RELOJ
30 | 02/08/2010 08:14 | 02/08/2010 | 1
30 | 02/08/2010 12:57 | 02/08/2010 | 1
30 | 02/08/2010 14:01 | 02/08/2010 | 1
30 | 02/08/2010 19:21 | 02/08/2010 | 1
35 | 02/08/2010 07:23 | 02/08/2010 | 1
35 | 02/08/2010 13:05 | 02/08/2010 | 1
16 | 02/08/2010 12:58 | 02/08/2010 | 1

RESULT IN SQL
TARJETA | HORA_INICIO | HORA_TERMINO | FECHA | RELOJ
30 | 02/08/2010 08:14 | 02/08/2010 12:57 | 02/08/2010 | 1
30 | 02/08/2010 14:01 | 02/08/2010 19:21 | 02/08/2010 | 1
35 | 02/08/2010 07:23 | 02/08/2010 13:05 | 02/08/2010 | 1
16 | 02/08/2010 12:58 | NULL | 02/08/2010 | 1


HOW CAN I DO IN SQL....?????
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16393 Visits: 19076
Hi,
You should help us to help you by delivering ddl and sample data in a consumable format. Because you're relatively new, I did it for you this time.
Here's a possible solution that you should test and understand before using it.


CREATE TABLE Checador(
tarjeta int,
hora datetime,
fecha date,
reloj int)
INSERT Checador VALUES
(30 , '02/08/2010 08:14' , '02/08/2010' , 1 ),
(30 , '02/08/2010 12:57' , '02/08/2010' , 1 ),
(30 , '02/08/2010 14:01' , '02/08/2010' , 1 ),
(30 , '02/08/2010 19:21' , '02/08/2010' , 1 ),
(35 , '02/08/2010 07:23' , '02/08/2010' , 1 ),
(35 , '02/08/2010 13:05' , '02/08/2010' , 1 ),
(16 , '02/08/2010 12:58' , '02/08/2010' , 1 );

WITH CTE AS(
SELECT tarjeta,
hora,
fecha,
reloj,
ROW_NUMBER() OVER( PARTITION BY tarjeta, fecha ORDER BY hora) % 2 AS inicio,
(ROW_NUMBER() OVER( PARTITION BY tarjeta, fecha ORDER BY hora) + 1) / 2 AS numero
FROM Checador)
SELECT tarjeta,
MAX( CASE WHEN inicio = 1 THEN hora END) hora_inicio,
MAX( CASE WHEN inicio = 0 THEN hora END) hora_termino,
fecha,
reloj
FROM CTE
GROUP BY tarjeta,
fecha,
reloj,
numero

DROP TABLE Checador




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
BriPan
BriPan
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 296
Same thing Using Join in CTE

declare @Checador TABLE (
i int identity (1,1),
TARJETA int,
HORA datetime,
FECHA date,
RELOJ int)
INSERT @Checador VALUES
(30 , '02/08/2010 08:14' , '02/08/2010' , 1 ),
(30 , '02/08/2010 12:57' , '02/08/2010' , 1 ),
(30 , '02/08/2010 14:01' , '02/08/2010' , 1 ),
(30 , '02/08/2010 19:21' , '02/08/2010' , 1 ),
(35 , '02/08/2010 07:23' , '02/08/2010' , 1 ),
(35 , '02/08/2010 13:05' , '02/08/2010' , 1 ),
(16 , '02/08/2010 12:58' , '02/08/2010' , 1 );

;with cte as
(
select ch.i,ch.TARJETA ,ch.HORA as HORA_INICIO ,ch1.HORA as HORA_TERMINO ,ch.FECHA ,ch.RELOJ
from @Checador ch
left join @Checador ch1
on ch.i+1=ch1.i
)

select TARJETA,HORA_INICIO,HORA_TERMINO,FECHA,RELOJ
from cte where i%2<>0


Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16393 Visits: 19076
But you're assuming that an identity column is available and that it has no gaps.
You're assuming as well that the data is being inserted to the table in the correct order.
That's why I used the ROW_NUMBER().


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search