Solved

Looking for a way to reference two rows in the same table in SQL statement and update data from one with the other

Posted on 2013-05-30
6
381 Views
Last Modified: 2013-05-31
I have a table created as such:
CREATE TABLE `full_daily_data` (
  `Symbol` varchar(9) NOT NULL,
  `Trade_Date` date NOT NULL DEFAULT '0000-00-00',
  `Day_Open` float DEFAULT NULL,
  `Day_High` float DEFAULT NULL,
  `Day_Low` float DEFAULT NULL,
  `Day_Close` float DEFAULT NULL,
  `Prior_Open` float DEFAULT NULL,
  `Prior_High` float DEFAULT NULL,
  `Prior_Low` float DEFAULT NULL,
  `Prior_Close` float DEFAULT NULL,
  PRIMARY KEY (`Symbol`,`Trade_Date`),
  KEY `TradeDate` (`Trade_Date`) ); 

Open in new window


I have it populated with data in these fields:
  `Symbol`
  `Trade_Date`
  `Day_Open`
  `Day_High`
  `Day_Low`
  `Day_Close`

What I want to do is have a SQL statement which will do one of two things:
1. With the data in symbol, then date order, take the data for the open, high, low and close from the row for one day and put them in the corresponding 'prior_' fields for the row in the corresponding following day (assuming that the symbol is the same).  Sounds like an aggresive if not impossible thing to do in just SQL... is it?
or, if not possible, then:
2. have a SQL statement that simply gets the data from one day for a symbol, and puts the open, high, low, close into the corresponding "Prior_" values in the row for the next day.

Any suggestions on this?
0
Comment
Question by:cashonly
6 Comments
 
LVL 16

Accepted Solution

by:
DcpKing earned 400 total points
ID: 39209721
No - not impossible at all

update fdd1
    set fdd1.Prior_Open = fdd2.Day_Open,
          fdd1.Prior_Close = fdd2.Day_Close
    from full_daily_data fdd1
    inner join full_daily_data fdd2
    on fdd2.Trade_Date = DATE_SUB(fdd1.Trade_Date, INTERVAL 1 DAY)

Open in new window


(I just did 2 of the five)

The idea is that you have two aliases to the same table, and you link one to the other one day out-of-step.

hth

Mike
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 39210800
I think you may need to consider the case when the trade date is Monday and the previous trade date was Friday. This can get further complicated when holiday's are introduced. If you have a late enough version, perhaps the lead/lag functions might be of help.
0
 

Author Closing Comment

by:cashonly
ID: 39211020
DCPKing, nice!

awking00, good point.

Need some kind of function to integrate in the holiday/weekend (also 9/11 type closures) that would replace the "1" in the INTERVAL 1 DAY clause

I'm going to award points on what you've donw so far and carry this discussion on to this question:
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_28144164.html
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 16

Expert Comment

by:DcpKing
ID: 39211560
To vary the interval ("interval 2 day", for example) :


@OffsetNumber = 2

PREPARE stmt FROM 

'   update fdd1
       set fdd1.Prior_Open = fdd2.Day_Open,
             fdd1.Prior_Close = fdd2.Day_Close
       from full_daily_data fdd1
       inner join full_daily_data fdd2
       on fdd2.Trade_Date = DATE_SUB(fdd1.Trade_Date, INTERVAL ? DAY);   '

EXECUTE stmt USING @OffsetNumber

Open in new window


Again, please excuse any syntax errors -  I don't have a MySQL machine with me!

hth

Mike
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39212183
there is a small issue in the join above, it must use fdd1.symbol = fdd2.symbol in addition to the date. The following works as a select, but the MySQL update syntax is a little different:
select
  fdd1.Symbol
, fdd1.Trade_Date
, fdd1.Day_Open
, fdd1.Day_High
, fdd1.Day_Low
, fdd1.Day_Close
, fdd2.Day_Open  pdo
, fdd2.Day_High  pdh
, fdd2.Day_Low   pdl
, fdd2.Day_Close pdc
from full_daily_data fdd1
inner join full_daily_data fdd2
on fdd1.symbol = fdd2.symbol
and fdd2.Trade_Date = DATE_SUB(fdd1.Trade_Date, INTERVAL 1 DAY)
;

## syntax for the update is like this:
update full_daily_data fdd1
inner join full_daily_data fdd2
on fdd1.symbol = fdd2.symbol
and fdd2.Trade_Date = DATE_SUB(fdd1.Trade_Date, INTERVAL 1 DAY)
set
  fdd1.Prior_Open  = fdd2.Day_Open
, fdd1.Prior_High  = fdd2.Day_High
, fdd1.Prior_Low   = fdd2.Day_Low
, fdd1.Prior_Close = fdd2.Day_Close
;

Open in new window

see: http://sqlfiddle.com/#!9/cc5f0/1
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 39212273
Thanks, PortletPaul, for spotting the mistake!  As soon as you pointed it out it became glaringly obvious !
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now