Show Query Results in Month/Year Order - sql-server-2005

I have a query that sales and it shows the month and year (field names are salemonth & saleyear) of the sale. Example return-set would be
January 2014
February 2014
March 2014
December 2014
January 2015
Now obviously I can't set it that way in my straight query as if I try to order by salemonth ASC it woudl show December, February, january, january, March. or even if I order by year ASC it still would not show in the actual calendar month order. How can I sort this result set to show in the order of an actual calendar?
One caveat their may be 0 sales for the month (november for example) I would still want this month/year shown in the query but have a 0 shown. Is this achievable?

A quick idea and fix (not the prettiest of solutions):
SELECT *, CAST(([Month] + ' 1,' + [Year]) AS Datetime) AS OrderDate FROM [TABLE] ORDER BY OrderDate
..or if you prefer to hide the sorting column:
SELECT * FROM [TABLE] ORDER BY CAST(([Month] + ' 1,' + [Year]) AS Datetime)
This is assuming the values are stored as some type of varchar/string, otherwise you'd obviously need to cast a few more bits.

Related

Select query for filtering data from particular range of year and month

I want to display data from user selected year of March to next year of April(ex:if user selected 2017, then need to select data from from 2017 march to april 2018 ). What is the best query for filtering it. It should be order by from selected year.
SELECT DATEPART(mm,submitted_date),SUM(total_count),YEAR(submitted_date)
FROM store_details
WHERE YEAR(submitted_date) = '2017'
AND MONTH(submitted_date) >=3
Group By YEAR(submitted_date),DATEPART(mm,submitted_date)
Order By YEAR(submitted_date),DATEPART(mm,submitted_date)
Well, later versions (2012+) of SQL Server have a nice built in function called datefromparts. However, since your question is tagged with sql-server-2005 this doesn't help so much unless you can upgrade your database (and you really should think about it, since 2005 is no longer supported by Microsoft).
However, since you know you want all the data between March and April of next year, you can simply do this:
SELECT DATEPART(MONTH,submitted_date),SUM(total_count),YEAR(submitted_date)
FROM store_details
WHERE submitted_date >= CAST(CAST(#Year as char(4)) + '-03-01 00:00:00' As datetime)
WHERE submitted_date < CAST(CAST(#Year + 1 as char(4)) + '-04-01 00:00:00' As datetime)
Group By YEAR(submitted_date),DATEPART(MONTH,submitted_date)
Order By YEAR(submitted_date),DATEPART(MONTH,submitted_date)
(That is, assuming the user provides the year as an int value.)
If you already have the starting month and date in hand, then I don't see why you can't just add an upper bound to the date in your WHERE clause:
SELECT
DATEPART(mm, submitted_date),
SUM(total_count),
YEAR(submitted_date)
FROM store_details
WHERE
(YEAR(submitted_date) = '2017' AND MONTH(submitted_date) >= 3) AND
(YEAR(submitted_date) = '2018' AND MONTH(submitted_date) < 4)
GROUP BY
YEAR(submitted_date),
DATEPART(mm, submitted_date)
ORDER BY
YEAR(submitted_date),
DATEPART(mm, submitted_date)

SQL Month Year datatypes

Its really frustrating whenever I have to work with date/ datetime datatypes and SQL doesn't provide a good and easy way to work with it.
Currently I have this table RecordsDaily with a column Date with datatype date. I want to convert the Date column to month and take only distinct month values AND then sort according to month. Below is the query.
select distinct(CAST(DATEPART(year,Date) as varchar(10)) + ' ' + datename(MONTH,Date)) Month
from P98.dbo.RecordsDaily
where Date >= '2013/12/1'
order by Month
Obviously since I have made it varchar it doesn't consider it as date datatype and sorts it alphabetically as below.
Month
2013 December
2014 April
2014 February
2014 January
2014 March
2014 May
Any help to make it sort as per calendar.
UPDATE
Please note the output should be with Month and Year
Please try:
select Month from(
select distinct(CAST(DATEPART(year,Date) as varchar(10)) + ' ' + datename(MONTH,Date)) Month
,year(Date) yr, month(Date) mn
from P98.dbo.RecordsDaily
where Date >= '2013/12/1'
)x
order by yr, mn
try this !
select * from table order by cast(('01'+id) as datetime)asc
-- id is your column_name

How can I display a result of data in order by a month-year combine column in sql?

I want to display a below data in order by first take month and then year
count Date
------- ----------
5 Aug 2011
6 jan 2008
10 feb 2009
I want to result as a first display a 2008 year, then 2009 like wise ordering wise date and also should display month in order wise
Assumptions:
You are trying to count records grouping by year and month
You have a table named Table1 with a column named EventDate
You are using SQL Server (you didn't specify)
You don't mind having extra columns in the output (you can ignore the columns you don't need)
You want to order by count, then year, then month
Based on these assumptions, the following should give you what you want.
SELECT COUNT(*) AS Count,
CONVERT(CHAR(3), DATENAME(MONTH, EventDate))
+ ' ' + CONVERT(CHAR(4), YEAR(EventDate)) AS Date,
MIN(YEAR(EventDate)) AS Year, MIN(MONTH(EventDate)) AS Month
FROM Table1
GROUP BY CONVERT(CHAR(4), YEAR(EventDate)),
CONVERT(CHAR(3), DATENAME(MONTH, EventDate))
ORDER BY COUNT(*), Year, Month
Here is the solution..
select convert(char(3), Date, 0)+' '+RIGHT(CONVERT(varchar, YEAR(Date)), 4) as [Date]
,count(id) as Count from Tabletest
group by convert(char(3), Date, 0)+' '+ RIGHT(CONVERT(varchar, YEAR(Date)), 4),month(Date),YEAR(Date)
order by YEAR(Date),MONTH(Date)

SQLCE: How to Count Datepart

For a long time I am struggling with the following subject: I want to count datepart values. I use SQL Compact Edition 4.0 and have no idea on how to get the following:
select datepart(week, CreateDate) as Week, count(*) from tblOrders
where CreateDate>'12 April 2010' and CreateDate<'25 June 2011'
This does not work obviously, but to give you an idea what I want to get as the result is:
- 2 columns,
one called "week" - that would be a week number
in the second column - how many orders I had per week
Thanks in advance,
Pete
You'll need to add a Group By to make the query syntax correct.
select datepart(week, CreateDate) as Week, count(*)
from tblOrders where CreateDate>'12 April 2010' and CreateDate<'25 June 2011'
group by datepart(week, CreateDate)
Does that help?

SQL Query Problem

When I am doing SQL Query on the database then all the months that are there in database and all the values corresponding to that particular month will be summed up in the Amount Column.
Suppose this is a table
Month Category Amount Year
January Rent 12 2011
March Food 13 2011
January Gas 14 2011
May Enter 15 2011
March General 16 2011
So I written the query to sum all the values of a particular month by using this:-
"SELECT Month, SUM(Amount) AS OrderTotal FROM budget1 WHERE year="2011" GROUP BY month "
So I got the result as this:-
Month Amount
January 26
March 29
May 15
But I want is that it should show all the months from January to December and Value of 0 infront of those month which are not there in the database like this for above example.
Month Amount
January 26
February 0
March 29
April 0
May 15
June 0
July 0
August 0
September 0
October 0
November 0
December 0
Any help will be appreciated..!!
Create a table with all months Jan-Dec, call it Months. Just a single column with the names or add an extra integer for sort order (I usually call this the ordinal column), as follows:
create table months (
month varchar(20),
ordinal
);
insert into months values ('January', 1);
insert into months values ('February', 2);
insert into months values ('March', 3);
...
insert into months values ('December', 12);
The specific syntax may depend upon your database platform. Then, depending upon your database:
SELECT months.Month, SUM(Amount) AS OrderTotal
FROM months
left join budget1
on months.month = budget1.Month
WHERE year="2011" or year is null
GROUP BY months.month, months.ordinal
ORDER by month.ordinal
You'll need to convert SUM(Amount) to 0 when null. The specific function or approach to do this depends upon your database platform, or you can just do it in the code that is interpreting the results.
Build a month table, with your months and the sort order. Then left join your month column to the month column in your data table. That will get you the zeros.
So your table will look like
Month Sort
======================
January 1
February 2
March 3
etc.
You can create the table by using Create Table, following by Insert Scripts
CREATE TABLE #months (month VARCHAR(50), sort INT);
INSERT INTO #months VALUES ('January', 1);
INSERT INTO #months VALUES ('February', 2);
etc.
Then
SELECT m.Month, SUM(Amount) AS OrderTotal
FROM #months m LEFT OUTER JOIN budget1 on m.Month = budget1.Month
WHERE year=2011
GROUP BY m.Month
ORDER BY m.Sort