pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Cross tabulations
3+ messages / 2 participants
[nested] [flat]

* Cross tabulations
@ 2004-10-20 04:43  Muhyiddin A.M Hayat <middink@indo.net.id>
  0 siblings, 1 reply; 3+ messages in thread

From: Muhyiddin A.M Hayat @ 2004-10-20 04:43 UTC (permalink / raw)
  To: pgsql-sql

Dear all,

I need to do something similar to a cross tabulation, but without any
aggregation.

I have below table

 id | employee_id |   state   |     check_time
----+-------------+-----------+--------------------
 21 |           1 | In        | 2004-10-12 21:37:13
 22 |           1 | Break Out | 2004-10-12 21:37:31
 23 |           1 | Break In  | 2004-10-12 21:37:42
 24 |           1 | Out       | 2004-10-12 21:37:50
 25 |           1 | In        | 2004-10-13 19:20:36
 26 |           1 | In        | 2004-10-14 01:33:48
 27 |           1 | Break Out | 2004-10-14 01:59:15
 28 |           1 | Break In  | 2004-10-14 03:15:45
 29 |           1 | Out       | 2004-10-14 03:17:23
 30 |           3 | In        | 2004-10-14 03:17:43
 31 |           3 | Break Out | 2004-10-14 19:32:34
 32 |           2 | In        | 2004-10-14 20:34:15
 33 |           3 | In        | 2004-10-15 02:01:28
 34 |           3 | Break Out | 2004-10-15 02:02:07
 35 |           3 | In        | 2004-10-16 02:06:43
 36 |           1 | In        | 2004-10-16 02:07:33
 37 |           1 | Break Out | 2004-10-16 02:09:09
 38 |           1 | Break In  | 2004-10-16 04:10:21
 39 |           1 | Out       | 2004-10-16 04:12:27
 40 |           3 | Break Out | 2004-10-16 21:38:22


I need something like this:

   date    | employee_id |    in    | break_out | break_id |   out
-----------+-------------+----------+-----------+----------+----------
2004-10-12 |           1 | 21:37:13 | 21:37:31  | 21:37:42 |21:37:50
2004-10-14 |           1 | 01:33:48 | 01:59:15  | 03:15:45 |03:17:23
2004-10-14 |           3 | 03:17:43 | 19:32:34  | 03:15:45 |03:17:23   
........
........
........

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Cross tabulations
@ 2004-10-20 06:35  Greg Stark <gsstark@mit.edu>
  parent: Muhyiddin A.M Hayat <middink@indo.net.id>
  0 siblings, 1 reply; 3+ messages in thread

From: Greg Stark @ 2004-10-20 06:35 UTC (permalink / raw)
  To: Muhyiddin A.M Hayat <middink@indo.net.id>; +Cc: pgsql-sql

"Muhyiddin A.M Hayat" <middink@indo.net.id> writes:

> Dear all,
> 
> I need to do something similar to a cross tabulation, but without any
> aggregation.

join your table to itself four times:

select * 
  from (select check_time::date as date, employee_id, check_time-check_time::date as in from test where state = 'In') as a 
  join (select check_time::date as date, employee_id, check_time-check_time::date as break_out from test where state = 'Break Out') as b using (employee_id,date)
  join (select check_time::date as date, employee_id, check_time-check_time::date as break_in from test where state = 'Break In') as d using (employee_id,date)
  join (select check_time::date as date, employee_id, check_time-check_time::date as out from test where state = 'Out') as e using (employee_id,date) ;

Note that this will do strange things if you don't have precisely four records
for each employee.

Alternatively use subqueries:

select date, employee_id,
       (select check_time-check_time::date from test where employee_id = x.employee_id and check_time::date = date and state = 'In') as in,
       (select check_time-check_time::date from test where employee_id = x.employee_id and check_time::date = date and state = 'Break Out') as break_out,
       (select check_time-check_time::date from test where employee_id = x.employee_id and check_time::date = date and state = 'Break In') as break_in,
       (select check_time-check_time::date from test where employee_id = x.employee_id and check_time::date = date and state = 'Out') as out
  from (select distinct employee_id, check_time::date as date from test) as x; 

This will at least behave fine if there are missing records and will give an
error if there are multiple records instead of doing strange things.

Neither of these will be particularly pretty on the performance front.

-- 
greg




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Cross tabulations
@ 2004-10-25 08:49  Muhyiddin A.M Hayat <middink@indo.net.id>
  parent: Greg Stark <gsstark@mit.edu>
  0 siblings, 0 replies; 3+ messages in thread

From: Muhyiddin A.M Hayat @ 2004-10-25 08:49 UTC (permalink / raw)
  To: Greg Stark <gsstark@mit.edu>; +Cc: pgsql-sql

Dear,

Thanks, that query is work, so.
So, i would like to calculate total work time




select date, employee_id,
       (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'In') as in,
       (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'Break Out') as
break_out,
       (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'Break In') as
break_in,
       (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'Out') as out
  from (select distinct employee_id, check_time::date as date from test) as
x;


out - in = work_time






----- Original Message ----- 
From: "Greg Stark" <gsstark@mit.edu>
To: "Muhyiddin A.M Hayat" <middink@indo.net.id>
Cc: <pgsql-sql@postgresql.org>
Sent: Wednesday, October 20, 2004 2:35 PM
Subject: Re: [SQL] Cross tabulations


> "Muhyiddin A.M Hayat" <middink@indo.net.id> writes:
>
> > Dear all,
> >
> > I need to do something similar to a cross tabulation, but without any
> > aggregation.
>
> join your table to itself four times:
>
> select *
>   from (select check_time::date as date, employee_id,
check_time-check_time::date as in from test where state = 'In') as a
>   join (select check_time::date as date, employee_id,
check_time-check_time::date as break_out from test where state = 'Break
Out') as b using (employee_id,date)
>   join (select check_time::date as date, employee_id,
check_time-check_time::date as break_in from test where state = 'Break In')
as d using (employee_id,date)
>   join (select check_time::date as date, employee_id,
check_time-check_time::date as out from test where state = 'Out') as e using
(employee_id,date) ;
>
> Note that this will do strange things if you don't have precisely four
records
> for each employee.
>
> Alternatively use subqueries:
>
> select date, employee_id,
>        (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'In') as in,
>        (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'Break Out') as
break_out,
>        (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'Break In') as
break_in,
>        (select check_time-check_time::date from test where employee_id =
x.employee_id and check_time::date = date and state = 'Out') as out
>   from (select distinct employee_id, check_time::date as date from test)
as x;
>
> This will at least behave fine if there are missing records and will give
an
> error if there are multiple records instead of doing strange things.
>
> Neither of these will be particularly pretty on the performance front.
>
> -- 
> greg
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-10-25 08:49 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-10-20 04:43 Cross tabulations Muhyiddin A.M Hayat <middink@indo.net.id>
2004-10-20 06:35 ` Greg Stark <gsstark@mit.edu>
2004-10-25 08:49   ` Muhyiddin A.M Hayat <middink@indo.net.id>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox