Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello all,
i am facing a problem to built by query
1:-I have 2 columns (a)displayorder (b)posteddate (c)ImageName
User can upload many pics for a displayorder with a posteddate when the particular date will come that banner should display EG

DispalyeOrder     PostedDate         ImageName
-------------     -----------        -------
1                     9/1/2013       Img1
2                     9/1/2013       Img2  
2                     10/1/2013      Img2New  
3                     9/1/2013       Img3 


Today img1=>img2=>Img3 will display
Tomorrow img1=>Img2New=>Img3 Will display

Please help me.
Thanks
Raj
Posted
Updated 9-Jan-13 1:37am
v2

SQL
CREATE TABLE #Test
(
	DisplayOrder int,
	PostedDate date,
	ImageName nvarchar(50)
)

declare @DateToSelect datetime

set @DateToSelect = '9/1/2013'

--Uncomment this line to see it working for the 10th.
--set @DateToSelect = '10/1/2013'

insert into #Test values (1, '9/1/2013', 'Img1')
insert into #Test values (2, '9/1/2013', 'Img1')
insert into #Test values (2, '10/1/2013', 'Img1New')
insert into #Test values (3, '9/1/2013', 'Img1')

select 
	#Test.DisplayOrder, 
	#Test.PostedDate, 
	#Test.ImageName 
from 
	#Test
join
(
	select
	displayOrder, 
	Max(PostedDate) as 'PostedDate'
	from
	#Test
	where PostedDate <= @DateToSelect
	group by displayOrder
) as SQ on (SQ.DisplayOrder = #Test.DisplayOrder) AND (SQ.PostedDate = #Test.PostedDate)


DROP TABLE #Test
 
Share this answer
 
Comments
nrgjack 9-Jan-13 8:00am    
my five to you.

that's the way to have the posted name "linked" to the max posteddate.
Raj tilak Bose 9-Jan-13 8:18am    
Thanks
SQL
SELECT  Displayorder
       ,max(PostedDate) as PostedDate
       ,ImageName
FROM    MyTable
WHERE   PostedDate < SYSDATE
GROUP BY DisplayOrder
        ,ImageName

SYSDATE is Oracle specific, exchange it to whatever is appropriate in your database.

Ignore my solution, it's incorrect, use Francois Viljoens or Aarti Meswanias instead.
 
Share this answer
 
v4
Comments
Raj tilak Bose 9-Jan-13 8:18am    
Thanks
This way...
SQL
select * from
(
select row_Number() over(partition by DispalyOrder order by PostedDate desc) as Id, DispalyOrder,ImageName,PostedDate from a
where PostedDate <='2013-01-11'
) as a
where Id=1

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Raj tilak Bose 9-Jan-13 8:18am    
Thanks
Aarti Meswania 9-Jan-13 8:20am    
Welcome! :)
Glad to help you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900