Kasuni Abegunawardana

Kasuni Abegunawardana

  • NA
  • 211
  • 121.6k

SQL Query for union all the months

Jun 25 2018 1:00 AM
Hi all,
 
I need to get data to all months for the selected year like this picture
 
 
this is what i hv done. please send me an answer
  1. Declare @Temp Table  
  2. (  
  3. CompanyID int,  
  4. ID nvarchar(250) NOT NULL,  
  5. Name nvarchar(250) ,  
  6. ParentAccID nvarchar(250),  
  7. ParentAccName nvarchar(250),  
  8. ParentDisplayID nvarchar(250),  
  9. DisplayID nvarchar (250),  
  10. Actual decimal(18,2),  
  11. Budget decimal(18,2),  
  12. Classification nvarchar(250),  
  13. AccLevel int,  
  14. AccOrder int  
  15. );  
  16. INSERT INTO @Temp  
  17. select  
  18. 4011111111115156122,NEWID(),'GROSS PROFIT','','GP','7-0000','7-0000',0,0,'GrossProfit',1,3  
  19. union  
  20. select  
  21. 4011111111115156122,NEWID(),'OPERATING PROFIT',null,'OP','10-000','10-000',0,0,'OperatingProfit',1,5  
  22. union  
  23. select  
  24. 4011111111115156122,NEWID(),'NET PROFIT / LOSS','','P/L','11-000','11-000',0,0,'NetProfit/Loss',1,8  
  25. INSERT INTO @Temp  
  26. select  
  27. [CompanyID],  
  28. [UID],  
  29. [Name],  
  30. [ParentAccountId],  
  31. [ParentAccountName],  
  32. [ParentDisplayID],  
  33. [DisplayID],  
  34. 0,  
  35. 0,  
  36. [Classification],  
  37. [AccountLevel],  
  38. (case when Classification = 'Income' then 1 when Classification='CostOFsales' then 2 when Classification='Expense' then 4 when Classification='OtherIncome' then 6 when Classification='OtherExpense' then 7 else 0 endas AccOrder  
  39. from Account111  
  40. where CompanyID=4011111111115156122  
  41. AND Classification in('Income','Expense','CostOfSales','OtherIncome','OtherExpense')  
  42. -- Actual for Month  
  43. update Tupdate set  
  44. Actual = isnull((select sum(Activity) from AccountReg where CompanyID = 4011111111115156122 and Year = 2012 and Month=1 and convert(varchar(250),UID) = Tupdate.ID) ,0)  
  45. from @Temp as Tupdate  
  46. ---- Budget for Month  
  47. update Tupdate set  
  48. Budget = isnull((select sum(Amount) from AccountBud where CompanyID = 4011111111115156122 and Year = 2012 and month=1 and convert(varchar(250),AccountUID) = Tupdate.ID) ,0)  
  49. from @Temp as Tupdate  
  50. update Tupdate set  
  51. Actual = ((select Actual from @Temp where DisplayID = '4-0000' AND CompanyID = 4011111111115156122 ) - (select Actual from @Temp where DisplayID = '5-0000' and CompanyID = 4011111111115156122)),  
  52. budget = ((select Budget from @Temp where DisplayID = '4-0000' AND CompanyID = 4011111111115156122 ) - (select Budget from @Temp where DisplayID = '5-0000' and CompanyID = 4011111111115156122))  
  53. from  
  54. @Temp as Tupdate where DisplayID = '7-0000' and CompanyID = 4011111111115156122  
  55. update Tupdate set  
  56. Actual = ((select Actual from @Temp where DisplayID = '7-0000' AND CompanyID = 4011111111115156122 ) - (select Actual from @Temp where DisplayID = '6-0000' and CompanyID = 4011111111115156122)),  
  57. budget = ((select Budget from @Temp where DisplayID = '7-0000' AND CompanyID = 4011111111115156122 ) - (select Budget from @Temp where DisplayID = '6-0000' and CompanyID = 4011111111115156122))  
  58. from  
  59. @Temp as Tupdate where DisplayID='10-000' and CompanyID = 4011111111115156122  
  60. update Tupdate set  
  61. Actual = ((select Actual from @Temp where DisplayID = '4-0000' AND CompanyID = 4011111111115156122 ) - (select Actual from @Temp where DisplayID = '5-0000' and CompanyID = 4011111111115156122) - (select Actual from @Temp where DisplayID = '6-0000' and CompanyID = 4011111111115156122) + (select Actual from @Temp where DisplayID = '8-0000' and CompanyID = 4011111111115156122)- (select Actual from @Temp where DisplayID = '9-0000' and CompanyID = 4011111111115156122) ),  
  62. budget = ((select Budget from @Temp where DisplayID = '4-0000' AND CompanyID = 4011111111115156122 ) - (select Budget from @Temp where DisplayID = '5-0000' and CompanyID = 4011111111115156122) - (select Budget from @Temp where DisplayID = '6-0000' and CompanyID = 4011111111115156122) + (select Budget from @Temp where DisplayID = '8-0000' and CompanyID = 4011111111115156122)- (select Budget from @Temp where DisplayID = '9-0000' and CompanyID = 4011111111115156122))  
  63. from  
  64. @Temp as Tupdate where DisplayID='11-000' and CompanyID = 4011111111115156122  
  65. select * from @Temp order by AccOrder, AccLevel,ID  
thank you

Answers (4)