|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 13, 2011 5:55 AM
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, February 06, 2013 2:18 AM
Points: 4,
Visits: 16
|
|
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.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 30, 2012 3:03 AM
Points: 8,
Visits: 194
|
|
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.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 30, 2012 3:03 AM
Points: 8,
Visits: 194
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 13, 2011 5:55 AM
Points: 9,
Visits: 39
|
|
| If the month is january then how can we display previous month?
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, April 05, 2013 8:10 PM
Points: 958,
Visits: 2,873
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:03 PM
Points: 412,
Visits: 862
|
|
If the month is january then how can we display previous month?
use datediff(dd,date,value) function
--SQLFRNDZ
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Yesterday @ 7:26 AM
Points: 24,
Visits: 293
|
|
| Use 2 CTE and in First display January and in other -1
|
|
|
|