473,770 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"slicing" records

Hello

I have a table with objects' descriptions:

id | length
---------+--------
object1 | 40
object2 | 66
object3 | 12
object4 | 107
object5 | 220

But I need to export data to a legacy system that doesn't handle lengths
greater than 50 (don't ask me why...). Instead, it expects the data in this
format:

id | length | fragment | offst
---------+--------+----------+-------
object1 | 40 | whole | 0
object2 | 50 | start | 0
object2 | 16 | end | 50
object3 | 12 | whole | 0
object4 | 50 | start | 0
object4 | 50 | middle | 50
object4 | 7 | end | 100
object5 | 50 | start | 0
object5 | 50 | middle | 50
object5 | 50 | middle | 100
object5 | 50 | middle | 150
object5 | 20 | end | 200

So when length becomes greater, it is break up in as many pieces as
necessary, each of max allowed length except the last one, in such a way
that the sum of partial lengths equals the original one.

Now I couldn't manage to get a query capable of doing this. If anybody has
an idea, I'll be very much appreciated.

TIA,
cl.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #1
3 1933
On Saturday 11 October 2003 06:00, Claudio Lapidus wrote:
Hello

I have a table with objects' descriptions:

id | length
---------+--------
object1 | 40
object2 | 66
object3 | 12
object4 | 107
object5 | 220

But I need to export data to a legacy system that doesn't handle lengths
greater than 50 (don't ask me why...). Instead, it expects the data in this
format:

id | length | fragment | offst
---------+--------+----------+-------
object1 | 40 | whole | 0
object2 | 50 | start | 0
object2 | 16 | end | 50
object3 | 12 | whole | 0
object4 | 50 | start | 0
object4 | 50 | middle | 50
object4 | 7 | end | 100
object5 | 50 | start | 0
object5 | 50 | middle | 50
object5 | 50 | middle | 100
object5 | 50 | middle | 150
object5 | 20 | end | 200


Simplest way is probably to write either a plpgsql function within PG or a
perl script outside it to split up the data.

If doing it within PG, you might find Stephan Szabo's article on set-returning
functions useful (http://techdocs.postgresql.org)

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
Claudio Lapidus wrote:
Hello

I have a table with objects' descriptions:

id | length
---------+--------
object1 | 40
object2 | 66
object3 | 12
object4 | 107
object5 | 220

But I need to export data to a legacy system that doesn't handle lengths
greater than 50 (don't ask me why...). Instead, it expects the data in this
format:
Oh, it's one of these _don't ask me why_ things ... well, then "what is
the target legacy system?" ... hehe.

id | length | fragment | offst
---------+--------+----------+-------
object1 | 40 | whole | 0
object2 | 50 | start | 0
object2 | 16 | end | 50
object3 | 12 | whole | 0
object4 | 50 | start | 0
object4 | 50 | middle | 50
object4 | 7 | end | 100
object5 | 50 | start | 0
object5 | 50 | middle | 50
object5 | 50 | middle | 100
object5 | 50 | middle | 150
object5 | 20 | end | 200


If there is a total upper maximum for the object length and it's not way
too obscenely large, then you can create a view that get's you this:

select id, length(data), data from t1;
id | length | data
----+--------+-------------------------------------------------
1 | 6 | 123456
2 | 10 | 1234567890
3 | 15 | 123456789012345
4 | 20 | 123456789012345 67890
5 | 27 | 123456789012345 678901234567
6 | 47 | 123456789012345 678901234567890 123456789012345 67
(6 rows)

select * from t1_sliced order by id, fragoffset;
id | fragoffset | fraglength | fragtype | fragdata
----+------------+------------+----------+------------
1 | 0 | 6 | whole | 123456
2 | 0 | 10 | whole | 1234567890
3 | 0 | 10 | start | 1234567890
3 | 10 | 5 | end | 12345
4 | 0 | 10 | start | 1234567890
4 | 10 | 10 | end | 1234567890
5 | 0 | 10 | start | 1234567890
5 | 10 | 10 | middle | 1234567890
5 | 20 | 7 | end | 1234567
6 | 0 | 10 | start | 1234567890
6 | 10 | 10 | middle | 1234567890
6 | 20 | 10 | middle | 1234567890
6 | 30 | 10 | middle | 1234567890
6 | 40 | 7 | end | 1234567
(14 rows)
See attached sample script. I didn't know if you really wanted this
fancy "whole|start|mi ddle|end" string or if that was supposed to be the
data of the fragment itself. Please notice that the view in the sample
is "configured " for data sized up to 100 characters.
Jan

--
#============== =============== =============== =============== ===========#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#============== =============== =============== ====== Ja******@Yahoo. com #

drop view t1_sliced;
drop table t1;
drop sequence t1_id_seq;
drop table slice_config;
drop function slice_length (integer, integer, integer);
drop function slice_type (integer, integer, integer);

create table t1 (
id serial primary key,
data text
);

insert into t1 (data) values ('123456');
insert into t1 (data) values ('1234567890');
insert into t1 (data) values ('1234567890123 45');
insert into t1 (data) values ('1234567890123 4567890');
insert into t1 (data) values ('1234567890123 45678901234567' );
insert into t1 (data) values ('1234567890123 456789012345678 901234567890123 4567');

create table slice_config (
s_off integer primary key,
s_len integer
);
insert into slice_config (s_off, s_len) values (0, 10);
insert into slice_config (s_off, s_len) values (10, 10);
insert into slice_config (s_off, s_len) values (20, 10);
insert into slice_config (s_off, s_len) values (30, 10);
insert into slice_config (s_off, s_len) values (40, 10);
insert into slice_config (s_off, s_len) values (50, 10);
insert into slice_config (s_off, s_len) values (60, 10);
insert into slice_config (s_off, s_len) values (70, 10);
insert into slice_config (s_off, s_len) values (80, 10);
insert into slice_config (s_off, s_len) values (90, 10);

create function slice_length (integer, integer, integer) returns integer
as '
declare
data_size alias for $1;
slice_off alias for $2;
slice_len alias for $3;
frag_len integer;
begin
frag_len = data_size - slice_off;
if frag_len > slice_len then
return slice_len;
end if;
return frag_len;
end;
' language plpgsql;

create function slice_type (integer, integer, integer) returns text
as '
declare
data_size alias for $1;
slice_off alias for $2;
slice_len alias for $3;
begin
if slice_off = 0 then
if data_size <= slice_len then
return ''whole'';
end if;
return ''start'';
end if;
if data_size <= slice_off + slice_len then
return ''end'';
end if;
return ''middle'';
end;
' language plpgsql;

create view t1_sliced as
select T.id, C.s_off as fragoffset,
slice_length (length(T.data) , C.s_off, C.s_len) as fraglength,
slice_type (length(T.data) , C.s_off, C.s_len) as fragtype,
substr (T.data, C.s_off + 1, C.s_len) as fragdata
from t1 T, slice_config C
where C.s_off = 0 or length(T.data) > C.s_off;

select id, length(data), data from t1;

select * from t1_sliced order by id, fragoffset;
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #3
Jan Wieck wrote:
Oh, it's one of these _don't ask me why_ things ... well, then "what is
the target legacy system?" ... hehe.
Of course, "don't ask me why" is my own way of saying "I don't know why!"
:-)
If there is a total upper maximum for the object length and it's not way
too obscenely large, then you can create a view that get's you this:
[snip] See attached sample script. I didn't know if you really wanted this
fancy "whole|start|mi ddle|end" string or if that was supposed to be the
data of the fragment itself. Please notice that the view in the sample
is "configured " for data sized up to 100 characters.


No, the destination system actually needs the labels as a flag of the
fragment position or if it's a fragment at all (i.e. not 'whole'). Actually,
your view/functions seem to almost fit my original need, I think they'll
just need minor touch up. Thanks a lot Jan, really nice code.

cheers
cl.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3251
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files if fname > 6 print fname Thanks!!!
19
10642
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to do readline(). Is there a way to do this? for example: while 1: line=stdin.peek_nextline()
33
9176
by: Jim Hill | last post by:
I've done some Googling around on this and it seems like creating a here document is a bit tricky with Python. Trivial via triple-quoted strings if there's no need for variable interpolation but requiring a long, long formatted arglist via (%s,%s,%s,ad infinitum) if there is. So my question is: Is there a way to produce a very long multiline string of output with variables' values inserted without having to resort to this wacky """v...
0
992
by: Neville C. Dempsey | last post by:
#!/bin/env python import bsddb test=bsddb.btopen("test.tbl") for m in "JFMATQPHSOND": test="Profit for month "+m+" $1B" def subyear_report(record_selection): for data in record_selection.iteritems(): print data
17
1597
by: Victor Bazarov | last post by:
Interesting article. However, it starts with an example which I find rather misleading. I hope the author (Christopher Diggins) wouldn't mind if I post a quote from the article. I know he probably reads or even writes to c.l.c++.m sometimes. Christopher gives this code (yes, it's not C++): interface IFuBar { contract: void FuBar();
5
1782
by: Daniel Crespo | last post by:
Is there a built-in method for transforming (1,None,"Hello!") to 1,None,"Hello!"? Thanks
7
1945
by: Dave Benjamin | last post by:
There's been a lot of discussion lately regarding Ruby and the notion of a "humane" interface to objects like arrays and maps, as opposed to "minimalist" ones. I believe the article that started the debates was this one by Martin Fowler: http://www.developertesting.com/archives/month200512/20051218-HumaneInterfaceOfMinimalInterface.html And this one was posted in response by Bruce Eckel: ...
1
5905
by: Stef Mientki | last post by:
Does anyone know the equivalent of the MatLab "diff" function. The "diff" functions calculates the difference between 2 succeeding elements of an array. I need to detect (fast) the falling edge of a binary signal. There's derivate function in Python diff, but when you use an binary (true/false) input, it also detects rising edges. Probably a stupid question,
1
3475
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } // A virtual destructor
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.