Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my data 

ACTUAL	BUDGET	TYPE
90	100	A
125	130	A		
100	110	B
100	222	B
150	150	B
 And i want like this 

A_BUDG	B_BUDG A_ACT B_ACT
100	110	90	100
130	222	125	100
	150	        150

here A_BUDG FOR A BUDGET AND A_ACT FOR A ACTUAL
Please give me suggestion.

What I have tried:

this is my sql qry for first one
SELECT R.* FROM
(
select D.ACTUAL,D.BUDGET,'A' AS TYPE from  RENT_T_PARKING_HDR H
left join RENT_T_PARKING_DTL D ON H.VOUCHERNO=D.VOUCHERNO
WHERE TIMEFROM BETWEEN 3 AND 4 AND TIMETO BETWEEN  3 AND 4

UNION

select D.ACTUAL,D.BUDGET,'B' AS TYPE from  RENT_T_PARKING_HDR H
left join RENT_T_PARKING_DTL D ON H.VOUCHERNO=D.VOUCHERNO
WHERE TIMEFROM BETWEEN 1 AND 2 AND TIMETO BETWEEN  1 AND 2

)R GROUP BY R.TYPE,R.ACTUAL, R.BUDGET ORDER BY TYPE


and need help for desired output
Posted
Updated 19-Dec-18 1:41am
v3
Comments
Herman<T>.Instance 19-Dec-18 4:49am    
There are many solutions to be found i the Article sections of CP:

Like mine: https://www.codeproject.com/Articles/201320/Dynamic-Pivoting-with-Cubes-and-eventhandlers-in-S

1 solution

SQL server has a PIVOT function which "flattens" out rows of data into columns. The syntax for the statement has at least one unpivoted columns and then multiple columns which are pivoted based on that (unpivoted column).

@digimanus has an article on how to create a Dynamic Pivot which utilizes a data CUBE for more advanced queries which may be needed with some datasets.

For your particular question, I cannot determine what the pivot point is; so I cannot provide a code-answer. The "data" you have provided seems to be incomplete; you are showing one data table but the queries you have are referencing two separate tables.

References
MSDN: Using PIVOT and UNPIVOT - SQL Server | Microsoft Docs[^]

Digimanus article: Dynamic Pivoting with Cubes and eventhandlers in SQL Server 2005, 2008 and 2008 R2[^]
 
Share this answer
 
Comments
SujataJK 19-Dec-18 8:04am    
Thanks @MadMyche

Actually in my case seen is that i have to compare report data between 2 date
like 1) Select Actual,Budget from table where dt between dt1 and dt2
--This is my A
And 2) Select Actual,Budget from table where dt between dt1 and dt2
-- This is my B
Now requirement is like - to compare this 2 dataset (A & B)
And calculate variace in amount between (A's Actual - B' Actual) and also calculate variance in Percentage

eg
A Buget A_Actual B_Actual Variance
100 120 100 20
150 100 150 -50
MadMyche 19-Dec-18 9:22am    
The schema and data for both tables would be helpful to visualize how this fits together

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900