Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help with simple syntax

Author  Topic 

lloydphilip
Starting Member

2 Posts

Posted - 2008-02-24 : 16:47:31
I'm new to SQL I don't know how to write code for the following problem:



Attached is the pic of my table. What I would like to do is compare the Open column of 2008-02-22 with the Close column of 2008-02-21.

Then I would like to know if the Open of 2008-02-22 is greater or less than the Close of 2008-02-21.
- If it greater, I would like to know if the Low of 2008-02-22 is ever less than or equal to the close of 2008-02-21.
- But if it is Less, then I would like to know if the High of 2008-02-22 is greater than or equal to the close of 2008-02-21.

The other issues is that the date have to be dynamic of course. I used 2008-02-22 and 2008-02-21 as examples. Ideally it would be the Open Column of Row 1 compared to the Close column of Row 2.

Sorry if I did not explain it properly, this new way of thinking is kinda foreign to me and I may sound like an idiot. Any help would be much appreciated.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-02-24 : 17:42:32
Assuming you have sql 2005, here is a way compare a "current" row with a "previous" row:

----------------------------------------------------------------
--set up a table variable similar to your actual table
declare @t table
(date datetime
,[open] money
,high money
,low money
,[close] money)

insert @t
select '2008-01-22', 1344.22, 1354.3, 1327.04, 1353.11 union all
select '2008-01-21', 1362.21, 1367.94, 1339.34, 1342.53 union all
select '2008-01-20', 1348.39, 1363.71, 1336.55, 1360.03
----------------------------------------------------------------

--see the raw table data
select * from @t

--Create a CTE (common table expression) with a sequencial column [rn]
;with cte as
(
select date
,[open]
,high
,low
,[close]
,row_number() over (order by [date] desc) rn
from @t
)
--select from our CTE using a SELF JOIN so that we can associate a CURRENT row with a PREVIOUS row.
select curr.[date] as [CurrDate]
,prev.[date] as [PrevDate]
--,curr.[open] as [currOpen
--,prev.[open] as [prevOpen
,CurrOpenGreaterThanPrevClose = case when curr.[Open] > prev.[close] then 1 else 0 end
,CurrLowLessThanPrevClose = case when curr.[low] <= prev.[close] then 1 else 0 end
,CurrHighLessThanPrevClose = case when curr.[high] >= prev.[close] then 1 else 0 end
from cte curr
join cte prev
on prev.rn-1 = curr.rn


output:
date open high low close
----------------------- --------------------- --------------------- --------------------- ---------------------
2008-01-22 00:00:00.000 1344.22 1354.30 1327.04 1353.11
2008-01-21 00:00:00.000 1362.21 1367.94 1339.34 1342.53
2008-01-20 00:00:00.000 1348.39 1363.71 1336.55 1360.03



date CurrOpenGreaterThanPrevClose CurrLowLessThanPrevClose CurrHighLessThanPrevClose
----------------------- ---------------------------- ------------------------ -------------------------
2008-01-22 00:00:00.000 1 1 1
2008-01-21 00:00:00.000 1 1 1



Be One with the Optimizer
TG
Go to Top of Page

tprupsis
Yak Posting Veteran

88 Posts

Posted - 2008-02-24 : 17:48:45
You can join the table to itself and then just compare the values. For example, the following query will give you all values for the date (Q1) and the prior day (Q2).

SELECT Q1.*, Q2.*
FROM Quotes Q1 INNER JOIN Quotes Q2 ON Q1.Date = Q2.Date + 1
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-02-24 : 17:54:17
Plenty of questions right back at you.
are the open & close dates unique?
If not, what would you do in the case where you have multiple 'previous' days?
Is it always one day behind or do you want the previous day?
The first day will not have previous. What happens there?

Basically you will want something like

select * from
tab as today inner join tab as yesterday
on
yesterday.close=(select max(yesterday.close) from tab t3 where t2.close < today.open)

which gives you today & the previous day stuck together so you can apply the rest of your logic.
Go to Top of Page

tprupsis
Yak Posting Veteran

88 Posts

Posted - 2008-02-24 : 18:08:52
Good catch, both of you. I hadn't thought of weekends and other dates when there wouldn't be data.
Go to Top of Page

lloydphilip
Starting Member

2 Posts

Posted - 2008-02-24 : 20:12:44
You guys are quick. I posted this question on other forums and I could just seem tumble weeds rolling by. I'm actually using MySQL since it was free. However I would like to thank you all for the responses. In case you hadn't guessed it, these are quotes of the S&P index.

Regarding the follow up questions;

1. Yes days are unique for this table.
2. It's not always one day behind since these are stock quotes and will skip Sat and Sunday plus all the major holidays.
3. Regarding the first day, I don't know. I guess I could add logic to limit the number of results. Since I have quotes from 1951, I can omit a few years and still maintain a very good sample size.
Go to Top of Page
   

- Advertisement -