Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help - difference between last and current year


Query Help - difference between last and current year

Author
Message
NoraG
NoraG
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 916
hi
Thanx a lot, I can see the variance now, but every count is shown in a different column I want everything YTD2009 / YTD2010 / and Variance in same Column, As I have to use it in a group column, Can you make it all in same column ....
Thanks again


---------------------------------------------------

Thanks

Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
Not to be a kill joy, but don't you think you should look at the code provided and see if you can make the necessary changes to format the output to your requirements.

We don't mind helping, but, you need to remember that most of us are volunteers giving of our own time and effort to help you. You really shouldn't treat as free contract labor.

If you have problems making the changes, show us what you have done and we will be glad to assist you.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
NoraG
NoraG
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 916
ok let me try, anyways thanx a lot


---------------------------------------------------

Thanks

NoraG
NoraG
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 916
Hey Thanks Jeffery ...:-):-)

I just used unpivot in the query you provided and got it the way I wanted in one column....Thanks again:-)



Declare @curYearStart datetime
,@prevYearStart datetime;

Set @curYearStart = dateadd(year, datediff(year, 0, getdate()), 0);
Set @prevYearStart = dateadd(year, -1, @curYearStart);





With currentYTD (Area, Source, Total)
As
(
Select Area
,Src
,count(Distinct Account_No)
From #dim1
Where Filter_Code Not In ('4', '9', 'Unknown')
And [Date] >= @curYearStart
And [Date] < dateadd(day, datediff(day, 0, getdate()), 0)
Group By Area,Src
), previousYTD (Area, Source, Total)
As (Select Area
,Src
,count(Distinct Account_No)
From #dim1
Where Filter_Code Not In ('4', '9', 'Unknown')
And [Date] >= @prevYearStart
And [Date] < @curYearStart
Group By Area
,Src
)

SELECT area,source,heading, cnt
FROM
(
Select curYTD.Area
,curYTD.Source
,curYTD.Total As 'YTD 2010'
,prevYTD.Total As 'YTD 2009'
,(curYTD.Total - prevYTD.Total) As Variance
From currentYTD curYTD
Join previousYTD prevYTD On prevYTD.Area = curYTD.Area
And prevYTD.Source = curYTD.Source

) p

UNPIVOT
(cnt FOR heading IN
([YTD 2010],[YTD 2009],Variance)
)AS unpvt




---------------------------------------------------

Thanks

Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
Great. Now, keep an eye on this thread as you may find others that may show you another way to accomplish the task as well. This will give you something to compare against your solution.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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