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
 Help with select (join)

Author  Topic 

jcarrallo
Starting Member

35 Posts

Posted - 2014-07-25 : 11:43:22
Hi,
I wonder if anyone can help with this basic select.
the table (price_1) is :
sph_symbol
sph_date
sph_value

And I want to select two values (sph_value) from the same table (price_1) but under different conditions (sph_date). And this for each symbol found in the table.

I have the select below but I get nothing as a result. Can you help, please?

select sph_symbol, a.sph_value,
b.sph_value
from Price_1 a
join Prices_1 b on b.sph_symbol = a.sph_symbol

where a.sph_date = DATEADD(DAY, -1, GETDATE())100
and b.sph_date = DATEADD(DAY, -5, GETDATE())


What am I doing wrong?
thanks,
Jay

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-07-25 : 12:09:58
quote:
Originally posted by jcarrallo
select sph_symbol, a.sph_value,
b.sph_value
from Price_1 a
join Prices_1 b on b.sph_symbol = a.sph_symbol

where a.sph_date = DATEADD(DAY, -1, GETDATE())100
and b.sph_date = DATEADD(DAY, -5, GETDATE())

Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2014-07-25 : 13:11:28
Take a look at this setup and show us some sample data and desired result:


declare @Price_1 table (sph_symbol varchar(10), sph_date datetime, sph_value int);
insert into @Price_1

select 'aaa', '2014-07-25', 1 union all
select 'aaa', '2014-07-24', 2 union all
select 'aaa', '2014-07-20', 3 union all
select 'bbb', '2014-07-25', 6 union all
select 'bbb', '2014-07-24', 4 union all
select 'bbb', '2014-07-20', 1;

select a.sph_symbol,
[MaxValueFromYesterday] = max(case when a.sph_date = dateadd(dd,datediff(dd,0,getdate()-1),0) then sph_value else null end),
[MaxValueFrom5DaysAgo] = max(case when a.sph_date = dateadd(dd,datediff(dd,0,getdate()-5),0) then sph_value else null end)
from @Price_1 a
group
by a.sph_symbol;


Nathan Skerl
Go to Top of Page

jcarrallo
Starting Member

35 Posts

Posted - 2014-07-25 : 14:23:13
Hi Nathan,
Your select gives me exactly the result the I was looking for.
Many thanks!!!!
jay
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2014-07-25 : 14:29:52
Glad to help!
Go to Top of Page
   

- Advertisement -