Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Order by desc for specific date


Order by desc for specific date

Author
Message
mynkdby
mynkdby
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 99
There is a table "Marketshare" which has fields say "startdate" and "rate". i need to sort the records of this table and have to take the top 1 latest record on 1 july of each year.
Was trying with Order by along with datepart() didn't able understand how to proceed.

MarketShare<table>

StartDate || Rate
-------------------
22/06/2014 234
28/06/2014 789
29/06/2014 987
02/07/2014 890

I need the first record which has start date before 1 july.

Result :
StartDate || Rate
---------------------
29/06/2014 987
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21049 Visits: 18258
Please post your query with sample data and we can assist.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14929 Visits: 38914
something like this help a little?

ORDER BY
CASE
WHEN datepart(mm,startdate) = 7
AND datepart(dd,startdate) = 1
THEN 1
ELSE 99
END



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

mynkdby
mynkdby
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 99
Hi ,


MarketShare<table>

StartDate || Rate
-------------------
22/06/2014 234
28/06/2014 789
29/06/2014 987
02/07/2014 890

i need latest start date record on july 1

Result :
StartDate || Rate
---------------------
29/06/2014 987


thanks in advance
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8474 Visits: 18082
Something like this?


DECLARE @date = '20140701'

SELECT TOP 1 *
FROM MarketShare
WHERE StartDate <= @date
ORDER BY StartDate DESC




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
mynkdby
mynkdby
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 99
Actually i am using this as a sub query inside my view, Declaring a date won't work because it should work for coming year records too..
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21049 Visits: 18258
Here's a quick and dirty script that seems to get what you want.


SET DATEFORMAT DMY
DECLARE @MarketShare TABLE (startdate DATE, rate INT)

INSERT INTO @MarketShare
( startdate, rate )
VALUES (
'22/06/2014', 234),(
'28/06/2014', 789),(
'29/06/2014', 987),(
'02/07/2014', 890)

SELECT TOP 1 *
   FROM @MarketShare
   WHERE startdate <= '1/7/2014'
   ORDER BY startdate DESC





Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

mynkdby
mynkdby
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 99
I achieved it proceeding like this

select startdate, case when startdate>=convert(datetime,'1 July '+convert(varchar,datepart(yyyy,GETDATE())))
   then '1 Jan 1900' else startdate end Datecalc from MARKETSHARE order by Datecalc desc

anyway thanks all of you for providing me the idea to proceed
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search