OrderBy clause sorting issue? - sql-server

I am trying to order by Date in SQL Server I am facing a weird issue. There are 2 cases I should explain to help you understand my issue .
Case 1 :
select [MonthName]
from prod.[dim date]
where F_Year = 2014
order by [Date]
My output:
june june june july july july july august
// Here I get duplicates but order by is working as expected
Case 2 : tried to remove duplicates by using "distinct"
select distinct [MonthName]
from prod.[dim date]
where F_Year = 2014
order by [Date]
My output:
August july june
// Order By not working as expected (ordering alphabetical wise ) .
Any workaround is appreciated

Firstly your query seems to be working off a table with 3 fields representing possibly a single date or maybe 2 - hard to tell without the structure but I can see : F_Year, [Date] & [MonthName]. So if these actually represent a single date, then revert to that single date and use formatting to determine F_Year and MonthName.
Secondly check out these posts for a probable answer to your question:
Sort by Date in SQL
Convert Month Number to Month Name Function in SQL

Related

SQL Server: How to do WHERE statement to show only December 2015..?

I'm new to SQL, learning the basics. I searched, but did not find the exact answer I needed that would work.
I have a table that shows customers who have purchased products and the datetime they were purchased. The column is called SaleDate. The format (for example) of the datetime looks like this:
2015-08-21 00:00:00.000
2014-03-17 00:00:00.000
I need to use a query to only show the products purchased during December of 2015. I don't think the SELECT or FROM statements are relevant to my question. Here are the two WHERE clauses I tried. Neither of these worked.
WHERE SaleDate = '2015-12%'
WHERE SaleDate = '%Dec-2015%'
You can use BETWEEN:
WHERE SaleDate BETWEEN '2015-12-01 00:00:00' AND '2016-01-01 00:00:00'
Use YEAR and MONTH functions
... WHERE YEAR(SaleDate) = 2015 AND MONTH(SaleDate) = 12
Don't use BETWEEN because you must to express the dates as literals, and that will be a problem considering that your SaleDate column could come with the time part in the future...

Is there a way to create a date column from other columns?

As the title says, can I create a new column from two fields? For example, I created a new column from the master values table to represent the months, and I have another column with the year. Can I combine them to create a date with whatever day? What I have is on the left, what I want is on the right:
Month Year NewDate
---- ---- -------
3 2015 2015-03-01
4 2015 2015-04-01
Whenever I tried to cast the month as datetime, it would look like this:
1900-01-03
This is probably a dumb question, but I created a timeline that lists out the months, and the trouble lies whenever the end date parameter chosen is less than the start date parameter, such as December 2015 to January 2016. I created the report in SSRS and the parameters aren't quite working correctly. Thanks guys!
Also, here's a snapshot of what the timeline looks like:
SELECT
DATEFROMPARTS ( [Year], [Month], 1 ) AS NewDate
FROM
YourTable
;
select dateadd(mm, t.Month -1, dateadd(yy,t.Year-1900,'19000101'))
from table1 t

SSIS - Modify date column from the 15th to the 1st of the month

SELECT * FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
--AND
--SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
Hello folks. Does anyone know how to correctly write this statement to
display the FIRST of each month? I have an Excel file that I am importing
into a Server using MS SSIS (VisualStudio 2008). The dates are monthly, but the last few months were the 15th June, May, April etc. My intent is to make
all of them show 1st of the month. All the months before January 2015 have been on the 1st of the month.
The SQL Query above is what I wrote in the Excel source Editor.
Thank You
This should always give you the first of the current Month
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)
to pull back the first of the month for dates in your table something like this should do it
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,[MonthlyDt]),0) FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
SELECT *, DATEADD('d',-DAY([MonthlyDT])+1, [Date]) AS [MonthD]
FROM [MarkTSK]
WHERE [MonthlyDT] IS NOT NULL
Thank you everyone. The SCRIPT above worked for me. All the dates that were not on the 1st of the month, now shows as the 1st.
Use DatePart to extract the Month and Year adding in the day manually.
Something like:
SELECT Convert(date, DatePart('yyyy', [MonthlyDt]) + DatePart('mm', [MonthlyDt]) + '01' ) FROM [MarkTSK]
WHERE [MarkTSK] IS NOT NULL
(Disclaimer: untested code)

Calculate payments on a month by month bases from date opened SQL 2008

I'm currently trying to replicate an old report that used to produce a rolling sum of collections. However it wasn't a standard month on month. Here is screen shot of the excel based report.
The blue section is based on a simple query and gives the dataset used to start(EXAMPLE):
SELECT COUNT(AccountNo) AS Number, SUM(Balance) AS Value, DATENAME(MM,DateOpened) AS Month, DATEPART(Y,DateOpened) AS Year FROM tblAccounts
GROUP BY DATENAME(MM,DateOpened), DATEPART(Y,DateOpened)
The tables are very basic :
AccountNo | Balance | DateOpened
12345 | 1245.55 | 01/01/2015
I'm struggling to get it to work out the months on a rolling basis, so Month 1 for Apr 2011 will be the first month for those files (payments in April), month 2 would be payments in May for the accounts opened in April (I hope that is clear).
So this means Month 1 for April would be April, and Month 1 for Nov would be Nov. Payments are stored in a table tblPayments
AccountNo | DatePayment | PaymentValue
12345 | 02/02/2015 | 15.99
Please ask if I haven't been clear enough
Assuming you have a column called "DatePayment", you should simply do something like this:
SELECT COUNT(AccountNo) AS Number, SUM(Balance) AS Value,
DATENAME(MM,DateOpened) AS Month, DATEPART(Y,DateOpened) AS Year,
DATEDIFF(MONTH, DateOpened, DatePayment) AS MonthN
FROM [...]
GROUP BY DATENAME(MM,DateOpened), DATEPART(Y,DateOpened),
DATEDIFF(MONTH, DateOpened, DatePayment)
The DATEDIFF simply counts the months between the date the account was opened and the date of the payment. Note that you might want to change the DateOpened to always be the 1st of the month in the DATEDIFF calculation.
In the FROM [...] part of your query, you will need a join between your Payments-table and the table holding your accounts, in order to be able to compare DateOpened with DatePayment. You should join them on the AccountNo-column. This looks something like this:
FROM Accounts INNER JOIN Payments ON Accounts.AccountNo = Payments.AccountNo
After doing this, you will need to make sure that all references to columns that exist in both tables are fully qualified. This means that COUNT(AccountNo) should be changed to COUNT(Accounts.AccountNo), etc.

Display a yearly summary by month in SQL

I have a table that has a date/time field and am trying to figure out how to run a report so that I can view the sum of the amount between each month separately in the 2012 year.
The table has 2 fields, Amount and TimeStamp, and I'm trying to return a report like this:
January, 2012: $221.20
February, 2012: $150.20
etc etc.
Anyone have any ideas how to accomplish this easily in SQL Server? I want to avoid writing a seperate query for each individual month.
This can be solved by using the MONTH and YEAR Functions on SQL.
SELECT
SUM(Amount) as [Amount]
,MONTH(TimeStamp) as [Month]
,YEAR(TimeStamp) as [Year]
FROM
[MyTable]
GROUP BY
MONTH(TimeStamp)
,YEAR(TimeStamp)

Resources