Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

comparision based on Dates Expand / Collapse
Author
Message
Posted Thursday, June 26, 2014 8:59 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:47 AM
Points: 229, Visits: 701
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
Post #1586497
Posted Thursday, June 26, 2014 9:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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 Moden's 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)
Post #1586511
Posted Thursday, June 26, 2014 9:19 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:47 AM
Points: 229, Visits: 701
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
Post #1586517
Posted Thursday, June 26, 2014 9:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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 Moden's 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)
Post #1586530
Posted Thursday, June 26, 2014 11:09 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:47 AM
Points: 229, Visits: 701
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
)

Post #1586574
Posted Thursday, June 26, 2014 11:14 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:47 AM
Points: 229, Visits: 701
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'
Post #1586579
Posted Thursday, June 26, 2014 12:44 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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 Moden's 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)
Post #1586614
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse