473,223 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,223 software developers and data experts.

creating a complex aggregate function

Hi
i have an ordered table of dates let's say:

1/1/2004
8/1/2004
15/1/2004
29/1/2004
5/2/2004
12/2/2004

I am searching for a way to have the minimum date and maximum date for dates seperated by one week whitout gaps between them in a string.
which will give the following output:
1/1/2004:15/1/2004;29/1/2004:12/2/2004;

I was thinking of doing this with an aggregate function.

So i thought about writing the following C code :

#include "postgres.h"
#include "utils/date.h"

int32 i;//initially equal to zero
text * charresult;
/*
text * concat(,){} // NOT IMPLEMENTED (HOW TO DO IT ??)
*/

PG_FUNCTION_INFO_V1(computechar);

Datum computechar(PG_FUNCTION_ARGS) {

DateADT d1=PG_GETARG_DATEADT(0);
DateADT d2=PG_GETARG_DATEADT(1);

int32 diff= (int32) (d2 - d1);
i++;

if(diff == 7*i)
PG_RETURN_DATEADT(d1);
else
{
charresult=concat(charresult,d1);
charresult=concat(charresult,":");
charresult=concat(charresult,d2);
charresult=concat(charresult,";");
PG_RETURN_DATEADT(d2);
}
}
PG_FUNCTION_INFO_V1(returncomputedchar);

Datum returncomputedchar (PG_FUNCTION_ARGS) {

PG_RETURN_TEXT_P(charresult);
}
And then i will create the aggregate as follows (after compiling ...) :

CREATE OR REPLACE FUNCTION computechar(date,date) returns date as '/home/nabifadel/tempo/groupingWeeks.so' LANGUAGE 'C' WITH (isStrict);
CREATE OR REPLACE FUNCTION returncomputedchar (date) returns text as '/home/nabifadel/tempo/groupingWeeks.so' LANGUAGE 'C' WITH (isStrict);

CREATE AGGREGATE groupe_weeks_agg( basetype = date, sfunc = computechar,stype = date, finalfunc = returncomputedchar);
The function 'computechar' will put the result in the variable 'charresult'that will be returned by the function 'returncomputedchar'.

My first question is : Could this work the way i am thinking of it ??

My second question is how to implement the function 'concat' that will return a text concatenation of dates and text.
(the question is basically how to transform a date into text).

It's the first time i try to implement an aggregate function and i need help + i haven't been using C language frequently the last few years (there may be errors) .

Thx.


Nov 23 '05 #1
1 2689
I forgot to mention that in fact i will have an ordered table of dates ( i mean by ordered i will do an order by before applying the aggregate) for different users.
That's why i need an aggregate ( i have to do a group by)

so i have something like this
01/01/2004 user1
08/01/2004 user1
15/01/2004 user1
29/01/2004 user1
05/02/2004 user1
12/02/2004 user1

25/12/2003 user2
01/01/2004 user2
15/01/2004 user2
22/01/2004 user2
29/01/2004 user2
05/02/2004 user2
12/02/2004 user2

which should produce the output:

01/01/2004:15/01/2004;29/01/2004:12/02/2004; user1
25/12/2003:01/01/2004;15/01/2004:12/02/2004; user2

I hope someone will help me in this issue.
----- Original Message -----
From: Najib Abi Fadel
To: generalpost
Cc: developPost
Sent: Monday, July 05, 2004 12:23 PM
Subject: [GENERAL] creating a complex aggregate function
Hi
i have an ordered table of dates let's say:

1/1/2004
8/1/2004
15/1/2004
29/1/2004
5/2/2004
12/2/2004

I am searching for a way to have the minimum date and maximum date for dates seperated by one week whitout gaps between them in a string.
which will give the following output:
1/1/2004:15/1/2004;29/1/2004:12/2/2004;

I was thinking of doing this with an aggregate function.

So i thought about writing the following C code :

#include "postgres.h"
#include "utils/date.h"

int32 i;//initially equal to zero
text * charresult;
/*
text * concat(,){} // NOT IMPLEMENTED (HOW TO DO IT ??)
*/

PG_FUNCTION_INFO_V1(computechar);

Datum computechar(PG_FUNCTION_ARGS) {

DateADT d1=PG_GETARG_DATEADT(0);
DateADT d2=PG_GETARG_DATEADT(1);

int32 diff= (int32) (d2 - d1);
i++;

if(diff == 7*i)
PG_RETURN_DATEADT(d1);
else
{
charresult=concat(charresult,d1);
charresult=concat(charresult,":");
charresult=concat(charresult,d2);
charresult=concat(charresult,";");
PG_RETURN_DATEADT(d2);
}
}
PG_FUNCTION_INFO_V1(returncomputedchar);

Datum returncomputedchar (PG_FUNCTION_ARGS) {

PG_RETURN_TEXT_P(charresult);
}
And then i will create the aggregate as follows (after compiling ...) :

CREATE OR REPLACE FUNCTION computechar(date,date) returns date as '/home/nabifadel/tempo/groupingWeeks.so' LANGUAGE 'C' WITH (isStrict);
CREATE OR REPLACE FUNCTION returncomputedchar (date) returns text as '/home/nabifadel/tempo/groupingWeeks.so' LANGUAGE 'C' WITH (isStrict);

CREATE AGGREGATE groupe_weeks_agg( basetype = date, sfunc = computechar,stype = date, finalfunc = returncomputedchar);
The function 'computechar' will put the result in the variable 'charresult' that will be returned by the function 'returncomputedchar'.

My first question is : Could this work the way i am thinking of it ??

My second question is how to implement the function 'concat' that will return a text concatenation of dates and text.
(the question is basically how to transform a date into text).

It's the first time i try to implement an aggregate function and i need help + i haven't been using C language frequently the last few years (there may be errors) .

Thx.


Nov 23 '05 #2

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

Similar topics

10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
1
by: R.A.M. | last post by:
Hello, I am learning SQL Server 2005. I have (correctly) written in .NET assembly DemoSQLServer with aggregate function AvgNoMinMax in class Demo and I have added assembly to database...
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
1
by: abdlah | last post by:
I have these tables in a database: Agent(AgentPhone*, AgentName) Constituency(ConstituencyNo*,ConstituencyName, RegionCode, ConstituencyDistrict) Party(PartyCode*, PartyName)...
4
jhardman
by: jhardman | last post by:
I have a table that looks like this: location group agentsReady contractsCompleted location1 group1 12 14 location1 group2 12 6 location2 group1 25 12 location2 group2 25 20...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.