Find Jobs
Hire Freelancers

Fix issue with Postgresql function

$30 USD

Cancelled
Posted over 11 years ago

$30 USD

Paid on delivery
Need help in fixing an issue with passing paramaters to a subquery. The function shown below works correctly when the parameters are hardcoded in, however when trying to pass the parameters to the crosstab subquery an error is returned the parameters are not available. I can run the where statement outside the cross table query, but this makes it impossibly slow and unusable. What I need to know is why and a solution to this if it is possible to do within Postrgesql. Postgresql Database is verision 9.1 CREATE OR REPLACE FUNCTION f_testfunction1(character varying, date, date) RETURNS SETOF type_charges AS $BODY$ DECLARE rec type_charges%ROWTYPE; DECLARE handling numeric (5,2) := 1.50; DECLARE banda numeric (5,2) := SELECT pick_rate from pick_costs WHERE pick_band = 'A' AND cust_ref = $1 LIMIT 1; DECLARE bandb numeric (5,2) := SELECT pick_rate from pick_costs WHERE pick_band = 'C' AND cust_ref = $1 LIMIT 1; DECLARE bandc numeric (5,2) := SELECT pick_rate from pick_costs WHERE pick_band = 'B' AND cust_ref = $1 LIMIT 1; BEGIN FOR rec IN SELECT ctselect.gdn_custref,gh.gdn_date, gh.gdn_address_name, handling, a, b, c, SUM((a*banda)+(COALESCE(b,0)*bandb)+(COALESCE(c,0)*bandc)+handling) AS ttlpickcost,gh.gdn_method,gh.gdn_postage_chargeout,gh.gdn_postage_chargeto FROM (SELECT * FROM crosstab($$ SELECT gdn_custref,skref.stockref_pick_charge_code, count(skref.stockref_pick_charge_code) AS ttlcountcode FROM tbl_pre_gdn_header gh LEFT JOIN tbl_pre_gdn_body gb ON gh.gdn_gdn = gb.gdn_gdn JOIN tbl_stockref skref ON skref.stockref_stockref = gb.gdn_stockref LEFT JOIN tbl_pick_costs pc ON pc.pick_band = skref.stockref_pick_charge_code AND pc.cust_ref = gh.gdn_custcode WHERE gh.gdn_custcode = $1 AND gh.gdn_date BETWEEN $2 AND $3 GROUP BY gh.gdn_gdn, gdn_custref,gdn_date, gdn_address_name, gdn_postage_chargeto, gdn_method,gdn_postage_actual_cost, skref.stockref_pick_charge_code, skref.stockref_pick_charge_code ORDER BY gdn_custref $$) AS ct(gdn_custref character varying,A bigint, B bigint, C bigint)) as ctselect LEFT JOIN tbl_pre_gdn_header gh ON ctselect.gdn_custref = gh.gdn_custref GROUP BY ctselect.gdn_custref,gh.gdn_date, gh.gdn_address_name,handling, a, b, c,gh.gdn_method,gh.gdn_postage_chargeout,gh.gdn_postage_chargeto ORDER BY ctselect.gdn_custref LOOP RETURN NEXT rec; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000;
Project ID: 4123705

About the project

2 proposals
Remote project
Active 11 yrs ago

Looking to make some money?

Benefits of bidding on Freelancer

Set your budget and timeframe
Get paid for your work
Outline your proposal
It's free to sign up and bid on jobs
2 freelancers are bidding on average $30 USD for this job
User Avatar
I would like to work on your project
$30 USD in 3 days
4.9 (111 reviews)
6.2
6.2
User Avatar
I have worked for Skype as a PostgreSQL developer for 2.5 years. This sort of thing is my specialty.
$30 USD in 1 day
0.0 (0 reviews)
0.0
0.0

About the client

Flag of AUSTRALIA
Sydney, Australia
4.9
116
Payment method verified
Member since Oct 16, 2010

Client Verification

Thanks! We’ve emailed you a link to claim your free credit.
Something went wrong while sending your email. Please try again.
Registered Users Total Jobs Posted
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Loading preview
Permission granted for Geolocation.
Your login session has expired and you have been logged out. Please log in again.