473,508 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Types and SRF's

Hi,

I am trying to get my feet wet in SRF's

I had to define a type in order to get my first attempt
at a srf for an sql language function ie..

create type annual_report_type as
( category text,
jan numeric(9,2),
feb numeric(9,2),
mar numeric(9,2),
apr numeric(9,2),
may numeric(9,2),
jun numeric(9,2),
jul numeric(9,2),
aug numeric(9,2),
sep numeric(9,2),
oct numeric(9,2),
nov numeric(9,2),
dec numeric(9,2) ,
total numeric(9,2) )

and then use this type as

create or replace function annual_report(integer) returns setof
annual_report_type
as '
select a.category, (select sum(amount) from all_accounts where
category=a.category and
extract (month from date) = 1 and extract (year from date) = $1) as
jan,
(select sum(amount) from all_accounts where category=a.category and
extract (month from date) = 2 and extract (year from date) = $1) as
feb,
....
....
(select sum(amount) from all_accounts where category=a.category and
extract (year from date) = $1) as total
from all_accounts a
group by category
order by category
' language sql

The above seems to be working fine...
I would feel a bit more comfortable if I could recover the definition
of the type at a later time, I cannot seem to find the definition of
the type in pg_type (there is an entry but the definition does not seem
to
be visible).

It does not seem possible to replace "annual_report_type" in the
function
definition with just the type...All of the placements fail for me in
any case.

Any suggestions as to how I can remember the rowtype? or (embed the
definiton
of the type in the definition of the function without having to create
an explicit type?

Jerry
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
4 1155
Jerry LeVan <je*********@eku.edu> writes:
I would feel a bit more comfortable if I could recover the definition
of the type at a later time,


Try "\d annual_report_type" in psql.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Doh, I was using \dT and \dT+....

Thanks

Jerry
On Aug 31, 2004, at 3:14 PM, Tom Lane wrote:
Jerry LeVan <je*********@eku.edu> writes:
I would feel a bit more comfortable if I could recover the definition
of the type at a later time,


Try "\d annual_report_type" in psql.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3

Your query looks suspiciously complicated...
Why not process all 12 months in one shot with something like this :
- only one subquery
- no join
- date between can make an index scan on date

select category, sum(amount) as sum_amount, extract (month from date) as
month
from all_accounts where (date between beginning of the year and end of
the year)
group by category,month order by category,month )

Not what you wanted but probably massively faster.

Or you can do this (in approximate SQL):

create type annual_report_type as
( sums numeric(9,2)[12] );

create type my_type as ( month integer, amount numeric );

CREATE AGGREGATE my_sum
takes one input which is my_type and sums the amount into the month column
of annual_report_type

Then :
select category, my_sum( my_type(month,amount) as report, extract (month
from date) as month
from all_accounts where (date between beginning of the year and end of
the year)
group by category,month order by category,month )

Dunno if this would work, it would be nice I think.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4
Thank you for the response Pierre,

select category, sum(amount) as sum_amount, extract (month from date)
as month
from all_accounts where (extract(year from date)=2003)
group by category,month order by category,month

is certainly much faster than what I am doing but as you pointed out,
I want the table to have a column for each month ( and a grand total
as the last column).

I have not used arrays and aggregates, I will take a look....

Jerry

On Sep 1, 2004, at 3:03 AM, Pierre-Frédéric Caillaud wrote:

Your query looks suspiciously complicated...
Why not process all 12 months in one shot with something like this :
- only one subquery
- no join
- date between can make an index scan on date

select category, sum(amount) as sum_amount, extract (month from date)
as month
from all_accounts where (date between beginning of the year and end
of the year)
group by category,month order by category,month )

Not what you wanted but probably massively faster.

Or you can do this (in approximate SQL):

create type annual_report_type as
( sums numeric(9,2)[12] );

create type my_type as ( month integer, amount numeric );

CREATE AGGREGATE my_sum
takes one input which is my_type and sums the amount into the month
column of annual_report_type

Then :
select category, my_sum( my_type(month,amount) as report, extract
(month from date) as month
from all_accounts where (date between beginning of the year and end
of the year)
group by category,month order by category,month )

Dunno if this would work, it would be nice I think.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5

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

Similar topics

2
1678
by: Jeff Davis | last post by:
in section _37.7.1.2. RETURN NEXT_ of the docs, it says that "PL/pgSQL stores the entire result set before returning from the function". Is the same true for C, and if so, should we document it...
188
17200
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
2077
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
2
2047
by: Leonid | last post by:
Hello, I develop Web Server using ATL in VC++ .NET and I can access it from the same computer using IE and SRF file: 'http://localhost/ATLServerApp/ATLServerApp.srf'. How can I access this...
4
1548
by: Együd Csaba | last post by:
Hi All, I'd like to write an SRF but I'm block a strange error message WARNING: plpgsql: ERROR during compile of check_close2 near line 11 ERROR: return type mismatch in function returning...
58
3404
by: jacob navia | last post by:
Hi people I have been working again in my tutorial, and I have finished the "types" chapter. If you feel like "jacob bashing" this is the occasion! I am asking for criticisms, or for things I may...
3
2372
by: sophia.agnes | last post by:
Dear all, what are the major expression types in c? i have seen the following types of expressions 1) constant expressions 2) integral expressions 3) float expressions 4) pointer...
55
3912
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
1
3358
by: diegoM | last post by:
Hello, I try to use the srf API and I have a link error. When I did : FuncCallContext *funcctx; ... if (SRF_IS_FIRSTCALL()){ funcctx = SRF_FIRSTCALL_INIT(); // If I remove this line, no link...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7324
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,...
0
7382
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...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
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,...
0
3193
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...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.