Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Accepted Solution

by:
DcpKing earned 1600 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 32

Assisted Solution

by:awking00
awking00 earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 49

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

704 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