Re: Aggregate over a linked list

Поиск
Список
Период
Сортировка
От Venky Kandaswamy
Тема Re: Aggregate over a linked list
Дата
Msg-id 776CCF725798BE4ABEC2521B9AEDBB35431A3055@BY2PRD0511MB429.namprd05.prod.outlook.com
обсуждение исходный текст
Ответ на Aggregate over a linked list  (M Lubratt <mplubratt@gmail.com>)
Список pgsql-sql
Did you try:

select substring(contract from 1 for 1), min(entry_date), max(entry_date), sum(profit)
from contract_table
group by 1;

________________________________________

Venky Kandaswamy

Principal Engineer, Adchemy Inc.

925-200-7124


From: pgsql-sql-owner@postgresql.org [pgsql-sql-owner@postgresql.org] on behalf of M Lubratt [mplubratt@gmail.com]
Sent: Thursday, January 17, 2013 3:19 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] Aggregate over a linked list

Hello!

I trade futures contracts and I have a PostgreSQL 9.2 database that keeps track of all of my trading activity.  The table includes columns for the futures contract, the entry and exit dates and the profit for that particular trade.  Now, futures contracts expire, so within a trade being indicated by my rules, I may need to "roll" contracts to the next contract to avoid contract expiration.  Therefore I can end up with multiple actual trades in my table that are linked by the dates.

e.g.

If the contract is SH12 (S = soybeans and H12 = March 2012 expiration)

contract      entry_date       exit_date          profit
-----------------------------------------------------------------------
SH12          2012-1-10        2012-2-27         500.00
SM12         2012-2-27        2012-3-30         200.00

While these are the actual exchange executed trades, I'd like to reduce this into a single row like (linked by the "S" and then exit_date = entry_date):

contract      entry_date       exit_date          profit
-----------------------------------------------------------------------
S               2012-1-10        2012-3-30         700.00

I've gone round and round in my head, google, SQL Cookbook, etc. trying to figure out how to do this.  Can anyone provide any pointers on how to do this?

Thanks and best regards!
Mark

В списке pgsql-sql по дате отправления:

Предыдущее
От: M Lubratt
Дата:
Сообщение: Aggregate over a linked list
Следующее
От: "wln"
Дата:
Сообщение: