June 26, 2014 at 8:59 am
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
June 26, 2014 at 9:13 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 26, 2014 at 9:19 am
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
June 26, 2014 at 9:39 am
sharonsql2013 (6/26/2014)
Wont be able to get sample data , but this is how the data looks likeMonth_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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 26, 2014 at 11:09 am
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
)
June 26, 2014 at 11:14 am
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'
June 26, 2014 at 12:44 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply