Forum Moderators: open
I am attempting to expand upon a web based risk managment application with PHP and PGSQL.
I have two queries that generate tables in identical formats. One pulls from a table that logs all the current days trades. The other pulls its data from a table that logs all previous days carried positions. I am stuck now trying to figure out how to merge these into one table where the P_L of identical contracts is added together.
Here are my two queries:
SELECT "X"."Trader", "X"."Exchange","X"."Product", "X"."Contract", ("X"."TotalSPrice" - "X"."TotalBPrice") AS "P_L", "X"."Buys", "X"."Sells", "X"."Buys" - "X"."Sells" AS "Total_Pos"
FROM ( SELECT sum(CASE WHEN "Fills"."BuySell" = 'B'::bpchar THEN "Fills"."Tick_Price"::double precision * "Fills"."Qty"::double precision ELSE 0::double precision END) AS "TotalBPrice", sum( CASE WHEN "Fills"."BuySell" = 'S'::bpchar THEN "Fills"."Tick_Price"::double precision * "Fills"."Qty"::double precision ELSE 0::double precision END) AS "TotalSPrice", sum( CASE WHEN "Fills"."BuySell" = 'B'::bpchar THEN "Fills"."Qty" ELSE 0
END) AS "Buys", sum(CASE WHEN "Fills"."BuySell" = 'S'::bpchar THEN "Fills"."Qty" ELSE 0 END) AS "Sells", "Fills"."Trader", "Fills"."Contract", "Fills"."Product", "Fills"."Exchange" FROM "Fills"
WHERE "Fills"."ProdType"::text <> 'SPREAD'::text
GROUP BY "Fills"."Trader","Fills"."Exchange","Fills"."Product","Fills"."Contract"
ORDER BY "Fills"."Trader","Fills"."Exchange","Fills"."Product","Fills"."Contract") "X";
AND
SELECT "Y"."Trader", "Y"."Exchange","Y"."Product", "Y"."Contract", ("Y"."TotalSPrice" - "Y"."TotalBPrice") AS "P_L", "Y"."Buys", "Y"."Sells", "Y"."Buys" - "Y"."Sells" AS "Total_Pos"
FROM ( SELECT sum(CASE WHEN "Carried_Position"."BuySell" = 'B'::bpchar THEN "Carried_Position"."Price"::double precision * "Carried_Position"."Qty"::double precision ELSE 0::double precision END) AS "TotalBPrice", sum( CASE WHEN "Carried_Position"."BuySell" = 'S'::bpchar THEN "Carried_Position"."Price"::double precision * "Carried_Position"."Qty"::double precision ELSE 0::double precision END) AS "TotalSPrice", sum( CASE WHEN "Carried_Position"."BuySell" = 'B'::bpchar THEN "Carried_Position"."Qty" ELSE 0
END) AS "Buys", sum(CASE WHEN "Carried_Position"."BuySell" = 'S'::bpchar THEN "Carried_Position"."Qty" ELSE 0 END) AS "Sells", "Carried_Position"."Trader", "Carried_Position"."Contract", "Carried_Position"."Product", "Carried_Position"."Exchange" FROM "Carried_Position"
WHERE "Carried_Position"."Acct" <> '15062000'
GROUP BY "Carried_Position"."Trader","Carried_Position"."Exchange","Carried_Position"."Product","Carried_Position"."Contract"
ORDER BY "Carried_Position"."Trader","Carried_Position"."Exchange","Carried_Position"."Product","Carried_Position"."Contract") "Y";
They both generate tables that look like this :
Trader, Exchange, Product, Contract, P_L, Buys, Sells, Total_Pos
EAV,eCBOT,ZB,MAR08,-1422.84375,12,0,12
So I need to combine these queries in such a way that if the Trader, Exchange, Product, and Contract matches inboth tables sum the P_L, Buys, Sells, and Total_Pos
and if there is no match between the different rows just merge the tables. Any help in doing this would be greatly appreciatead!