Unix Technical Forum

cross tab needs in SQL

This is a discussion on cross tab needs in SQL within the Informix forums, part of the Database Server Software category; --> INFORMIX-OnLine Version 7.23.UC11 ISQL-7.20.UD1 HP-UX 11.0 Is there a way to do a cross tab output in ISQL? I ...


Go Back   Unix Technical Forum > Database Server Software > Informix

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-20-2008, 11:03 AM
Konikoff, Rob \(Contractor\)
 
Posts: n/a
Default cross tab needs in SQL

INFORMIX-OnLine Version 7.23.UC11
ISQL-7.20.UD1
HP-UX 11.0

Is there a way to do a cross tab output in ISQL?

I don't have a data cube function in 7.2, and I need one. The only
alternative I've found is to do cut and paste in a UNIX shell script
(can you spell slow?). I have tabular data and I need it to be cross
tab.

What I have:
ID|DAYS|CNT
01|0001|25
01|0004|20
02|0001|10
03|0020|10
03|0025|15

What I need:
ID|1 Day|4 Day |20 Day|25 Day|
01| 25 | 20 | | |
02| 10 | | | |
03| | | 10 | 15 |


Thanks.


Rob Konikoff


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-20-2008, 11:03 AM
bozon
 
Posts: n/a
Default Re: cross tab needs in SQL

If you were running a later version you could use the great multiset
stuff, but you are not so you are going to have to do it the old
fashioned straight SQL way (yuck):

Assuming original table looks like this:

create table date_test(
id integer,
days integer
) ;

where you have the following entries:

(1, 1) 25 times
(1, 4) 20 times
(2, 1) 10 times
(3, 20) 10 times
(3, 25) 15 times

select
t.id,
sum(
case days
when 1 then 1
else 0
end
) day1,
sum(
case days
when 4 then 1
else 0
end
) day4,
sum(
case days
when 20 then 1
else 0
end
) day20,
sum(
case days
when 25 then 1
else 0
end
) day25
from
date_test t
group by
id
;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-20-2008, 11:09 AM
bozon
 
Posts: n/a
Default Re: cross tab needs in SQL

This perl program should do the trick and it should be fast:
Of course substitute your path to perl after #! on first line. From vi
I always r! which perl
..
You call it like
crosstab.pl <filename>

#!/usr/bin/perl -w

use strict;

my %htCols ;
my %htData ;
my( $id, $days, $count);
while (<>) {
chomp;
($id, $days, $count) = split /\|/ ;
$htCols{ $days } = 1 ;
$htData{ $id }{ $days } = $count ;
}

my @colKeys = sort keys %htCols ;

foreach my $id (sort keys %htData) {
printf("$id|");
foreach my $col (@colKeys) {
if (exists($htData{$id}{$col}) ) {
printf("%s|",$htData{$id}{$col})
}
else {
print "|" ;
}
}
print "\n" ;
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:48 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com