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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sub select?

Author  Topic 

ljp099
Yak Posting Veteran

79 Posts

Posted - 2011-06-22 : 03:38:59
I need to perform a query of books to First select the most recently published book (datepublished (date)) where genre (genre (varchar) equals mystery, then from this most recently published date, I then need to select all books in the same genre for the last 12 months.

@mostrecent = Select datepublished From Books Where genre = 'mystery' Order By datepublished

Select * from Books where datepublished < @mostrecent AND datepublished > @mostrecent - 12 months And genre = 'mystery'

Can I join these into a single select? Right now Im using a loop statement in my C# code and it is killing the performance.

Thanks for any help.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-07-08 : 05:41:22
declare @datepublished =(select max(datepublished) From Books Where genre = 'mystery' )
Select * from Books where datepublished < @mostrecent AND datepublished > dateadd(month,-12,@datepublished) And genre = 'mystery'



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -