4

I have a resources table, one of the fields is a date field with the Data Type of date. I want to have to following output:

Current month records (say May - year is not important)

Then the following (again, assuming May is the current month)

  • June Records
  • July Records
  • August Records
  • September Records
  • October Records
  • November Records
  • December Records
  • January Records
  • February Records
  • March Records
  • April Records

Come June, June is the current month and then the order would be:

  • July Records
  • August Records
  • ...

Here is my SQL...I don't know how to ORDER the output to achieve the desired order (5,6,7,8,9,10,11,12,1,2,3,4):

SELECT
  resource_id,
  resource_title,
  resource_summary,
  resource_category,
  resource_status,
  resource_date,
  DATEPART(month, resource_date) AS resource_month,
  DATEPART(day, resource_date) AS resource_day
FROM dbo.resources
WHERE (resource_category = N'Quotes')
  AND (resource_status <> N'Draft')

I found this possible solution for MySQL:

I need unusual ordering mysql results

but I'm missing something on my end.

Community
  • 1
  • 1
Brett
  • 887
  • 4
  • 14
  • 25

6 Answers6

4
ORDER BY
  (MONTH(resource_date) - MONTH(GETDATE()) + 12) % 12,
  DATEADD(year, YEAR(GETDATE()) - YEAR(resource_date), resource_date),
  YEAR(resource_date)

The first term sets the primary order by the month of resource_date (the current month will be first, the previous one, last). The second term orders the timestamps within a month regardless of the year of the date. If your dates do not contain time parts or if the time parts are absolutely irrelevant, you could replace it with DAY(resource_date). Finally, the last term takes the year into account for otherwise identical dates (could also be simply resource_date).

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • Okay, this is the first suggestion I tried. In MS SQL, it is listing the records correctly. When I output the results to a web page, the order is not correct. Here is a sample: http://www.americanspeaker.com/beta/members/sample.asp – Brett Jun 01 '11 at 13:28
  • Here is the SQL in MS SQL SELECT TOP (100) PERCENT resource_id, resource_title, resource_summary, resource_source, resource_date, resource_category, resource_status FROM dbo.resources WHERE (resource_category = N'Quotes') AND (NOT (resource_status = N'Draft')) ORDER BY (MONTH(resource_date) - MONTH(GETDATE()) + 12) % 12, DAY(resource_date) – Brett Jun 01 '11 at 13:28
  • Here is the code that pulls from the DB Dim rs_quotes Dim rs_quotes_cmd Dim rs_quotes_numRows Set rs_quotes_cmd = Server.CreateObject ("ADODB.Command") rs_quotes_cmd.ActiveConnection = MM_americanspeaker_STRING rs_quotes_cmd.CommandText = "SELECT * FROM dbo.qry_resources_quotes" rs_quotes_cmd.Prepared = true Set rs_quotes = rs_quotes_cmd.Execute rs_quotes_numRows = 0 – Brett Jun 01 '11 at 13:28
  • The ORDER BY should be applied to the final query, if you want guaranteed order. You must not rely on the ORDER BY clause in the definition of `dbo.qry_resources_quotes` (which, I assume, is defined by the previous query, the `SELECT TOP (100) PERCENT...` one). – Andriy M Jun 01 '11 at 13:35
  • Works! Awesome. I've run into that ORDER BY issue in the past. Is this a bug in MS SQL or something I'm not understanding about SQL and MS SQL Server? – Brett Jun 01 '11 at 14:03
  • From the MSDN article about [`CREATE VIEW`](http://msdn.microsoft.com/en-us/library/ms187956.aspx): `Note. The ORDER BY clause is used only to determine the rows that are returned by the TOP clause in the view definition. The ORDER BY clause does not guarantee ordered results when the view is queried, unless ORDER BY is also specified in the query itself.` – Andriy M Jun 01 '11 at 14:19
0

Will it work for you?
ORDER BY
CASE DATEPART(month, resource_date)
WHEN 5 THEN 0
WHEN 6 THEN 1
... etc
END

a1ex07
  • 36,826
  • 12
  • 90
  • 103
0

I think something like this might be what you're looking for:

SELECT
  resource_id,
  resource_title,
  resource_summary,
  resource_category,
  resource_status,
  resource_date
FROM
  dbo.resources
WHERE
  resource_date >= DATE_FORMAT(NOW() ,'%Y-%m-01') AND
  resource_date < DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 YEAR) ,'%Y-%m-01')
ORDER BY
  resource_date;
King Skippus
  • 3,801
  • 1
  • 24
  • 24
0

How 'bout ORDER BY (DATEPART(month,resource_date) - (DATEPART(month,getdate() -1)) % 12)

So in May (month 5), you order by the month in the row -6 (mod 12). So, June (month 6) would be 0, July (7) would be 1.

In June, July would be 0, etc.

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
0

You should be able to adapt the MySQL solution by using DATEPART in place of DATE_FORMAT:

SELECT resource_id, resource_title, resource_summary, resource_category, resource_status, resource_date, DATEPART(month, resource_date) AS resource_month, DATEPART(day, resource_date) AS resource_day
FROM dbo.resources
WHERE (resource_category = N'Quotes') AND (resource_status <> N'Draft')
ORDER BY DATEPART(month, resource_date) < DATEPART(month, GETDATE()),
         DATEPART(month, resource_date)

I don't have SQL Server handy so I'm not sure if it will be happy with a boolean in the ORDER BY clause though. If it doesn't like the boolean ORDER BY, then a CASE should do the trick:

ORDER BY
    CASE WHEN DATEPART(month, resource_date) < DATEPART(month, GETDATE())
        THEN 0
        ELSE 1
    END,
    DATEPART(month, resource_date)
mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

I assume that there is a year within "resource_date" - isn't it? In this case you can simply filter and order by

WHERE resource_date >= getdate()
  AND resource_date < DATEADD(year,1,getdate())
ORDER BY resource_date;

If there is no year (or more exactly: different unknown years) you can do this:

ORDER BY
    CASE
    WHEN DATEADD(year,-year(resource_date),resource_date) <
         DATEADD(year,-year(getdate()),getdate())
    THEN 1
    ELSE 0
    END ASC,
    DATEADD(year,-year(resource_date),resource_date);

Hope it helped ...

Manfred Sorg
  • 1,820
  • 1
  • 11
  • 11