SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with select (join)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jcarrallo
Starting Member

Spain
35 Posts

Posted - 07/25/2014 :  11:43:22  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

421 Posts

Posted - 07/25/2014 :  12:09:58  Show Profile  Reply with Quote
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

USA
938 Posts

Posted - 07/25/2014 :  13:11:28  Show Profile  Reply with Quote
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

Spain
35 Posts

Posted - 07/25/2014 :  14:23:13  Show Profile  Reply with Quote
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

USA
938 Posts

Posted - 07/25/2014 :  14:29:52  Show Profile  Reply with Quote
Glad to help!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000