SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


comparision based on Dates


comparision based on Dates

Author
Message
sharonsql2013
sharonsql2013
Mr or Mrs. 500
Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)

Group: General Forum Members
Points: 594 Visits: 1265
I need to compare the ACC_Backlog data for the last two months. The table has monthwise data from May 2013 until May 2014. I am only looking for last two months comparision.
So, if ACC_Backlog of may 2014 is > april then it should display "1" or "Higher" else 0 or "lower"

Here is the existing query, Can anyone advise?

Select Month_Year
,datepart(month, cast(replace(Month_Year, '-', '') as datetime)) as MyMonth
, datepart(year, cast(replace(Month_Year, '-', '') as datetime)) as MyYear
,Group
,ACC_Opened
,ACC_Closed
,ACC_Backlog
,Time_Stamp
from ACC_ByMonth
WHERE Group = 'Investments'
-- ORDER By MyYear desc , MyMonth desc
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26046 Visits: 17528
sharonsql2013 (6/26/2014)
I need to compare the ACC_Backlog data for the last two months. The table has monthwise data from May 2013 until May 2014. I am only looking for last two months comparision.
So, if ACC_Backlog of may 2014 is > april then it should display "1" or "Higher" else 0 or "lower"

Here is the existing query, Can anyone advise?

Select Month_Year
,datepart(month, cast(replace(Month_Year, '-', '') as datetime)) as MyMonth
, datepart(year, cast(replace(Month_Year, '-', '') as datetime)) as MyYear
,Group
,ACC_Opened
,ACC_Closed
,ACC_Backlog
,Time_Stamp
from ACC_ByMonth
WHERE Group = 'Investments'
-- ORDER By MyYear desc , MyMonth desc


How about some ddl and sample data? Can you change the table so that Month_Year is a date or datetime instead of character data? It will save you tons of pain in the long run.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
sharonsql2013
sharonsql2013
Mr or Mrs. 500
Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)

Group: General Forum Members
Points: 594 Visits: 1265
Wont be able to get sample data , but this is how the data looks like


Month_Year MyMonth MyYear Group A ACC_Opened ACC_Closed ACC_Backlog Timestamp

Apr-2014 4 2014 Investments 128 133 12 2014-06-11 09:57:55.477
Aug-2013 8 2013 Investments 132 90 9 2014-06-11 09:57:55.477
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26046 Visits: 17528
sharonsql2013 (6/26/2014)
Wont be able to get sample data , but this is how the data looks like


Month_Year MyMonth MyYear Group A ACC_Opened ACC_Closed ACC_Backlog Timestamp

Apr-2014 4 2014 Investments 128 133 12 2014-06-11 09:57:55.477
Aug-2013 8 2013 Investments 132 90 9 2014-06-11 09:57:55.477


What about some ddl? That means create table script. Actually what you posted IS sample data. It would be a lot easier if you can turn that into insert statement for the table that you will also post for us.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
sharonsql2013
sharonsql2013
Mr or Mrs. 500
Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)

Group: General Forum Members
Points: 594 Visits: 1265
Here it is :

CREATE TABLE #mytable
(
Month_Year Varchar(10),
MyMonth DATETIME,
MyYear DATETIME,
ACC_Opened Varchar(120),
ACC_Opened INT,
ACC_Closed INT,
ACC_Backlog INT,
TimeStamp Datetime
)
sharonsql2013
sharonsql2013
Mr or Mrs. 500
Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)

Group: General Forum Members
Points: 594 Visits: 1265
I should have added this too:

Insert INTO #mytable
(Month_Year ,MyMonth,MyYear,ACC_Opened ,ACC_Opened ,ACC_Closed ,ACC_Backlog ,TimeStamp )
Select 'Apr-2014', '4', '2014','Investments','128','133','12', '2014-06-11 09:57:55.477'
UNion All
Select 'Aug-2013', '8', '2013','Investments','118','143','10', '2014-06-11 09:57:55.477'
UNion All
Select 'May-2014', '5', '2014','Investments','113','123','15', '2014-06-11 09:57:55.477'
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26046 Visits: 17528
sharonsql2013 (6/26/2014)
Here it is :

CREATE TABLE #mytable
(
Month_Year Varchar(10),
MyMonth DATETIME,
MyYear DATETIME,
ACC_Opened Varchar(120),
ACC_Opened INT,
ACC_Closed INT,
ACC_Backlog INT,
TimeStamp Datetime
)



Umm...you should probably test stuff before you post it. This is not going to work as is because you have two columns with the same name.

Also, If you could post what you expect as the result from your sample data it would really help.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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