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


how to display previous month data along with current month data


how to display previous month data along with current month data

Author
Message
ramya.praba
ramya.praba
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 39
table Mst_Region
----------------


Region_Id RegionName
----------------------
1 central
2 South
3 north

Table Voc_Trans_Details
-----------------------

Trans_id Region_id Month_value Answer Year_value
___________________________________________________________

1 1 7 53.333 2010
2 1 6 21.89 2010
3 3 7 75.98 2010


select B.RegionName,
avg(D.Answer) as Answer
from Mst_Region B,
Voc_Trans_Details D where D.Month_Value=7 and Region_id=1 group by RegionName

My OUTPUT is
____________

RegionName Answer
__________________
Central 53.333


I want OUTPUT like
------------------

RegionName PreviousmnthAnswer Answer
______________________________________
Central 21.89 53.333
k_arumugam82
k_arumugam82
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 19
Hi Ramya,
I hope the below query should do the magic.

Select Current.RegionName, Previous.Answer, Current.Answer From
(select B.RegionName,
avg(D.Answer) as Answer
from Mst_Region B,
Voc_Trans_Details D where D.Month_Value=7 and Region_id=1 group by RegionName) Current JOIN (select A.RegionName,
avg(C.Answer) as Answer
from Mst_Region A,
Voc_Trans_Details C where C.Month_Value=6 and Region_id=1 group by RegionName) Previous ON Current.RegionName = Previous.RegionName

Pls contact me at k_arumugam82@yahoo.co.in, if you have any other queries.
Ciber2010
Ciber2010
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 196
Hi,

Am Venky i executed that query which u posted,but its showing error for that Current and Prevoius words,I didnt get that ,can u please explain me those.

Thanks in Advance.
Ciber2010
Ciber2010
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 196
Hi Ramya,

Just try Below query ,this is same as k_arumugam82 had sent,but bit modifications.


Select Curren.RegionName, Previous.Answer, Curren.Answer From
(select B.RegionName,
avg(D.Answer) as Answer
from Mst_Region B,
Voc_Trans_Details D where D.Month_Value=7 and D.Region_id=1 group by RegionName)as Curren JOIN
(select A.RegionName,
avg(C.Answer) as Answer
from Mst_Region A,
Voc_Trans_Details C where C.Month_Value=6 and A.Region_Id=1 group by RegionName) as Previous ON Curren.RegionName = Previous.RegionName
ramya.praba
ramya.praba
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 39
If the month is january then how can we display previous month?
tfifield
tfifield
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1069 Visits: 2890
What you want to do is fairly easy if you use a calendar table. Here is an article I published on the subject last July and it has an example of returning prior periods to the current period (in this case the periods are months).
http://www.sqlservercentral.com/articles/T-SQL/70482/
Todd Fifield
@SQLFRNDZ
@SQLFRNDZ
Say Hey Kid
Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)

Group: General Forum Members
Points: 665 Visits: 1197
If the month is january then how can we display previous month?


use datediff(dd,date,value) function


--SQLFRNDZ

vax09
vax09
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 370
Use 2 CTE and in First display January and in other -1
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