Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one table in sqlserver in name of Fee_Assign_to_Students. This table represent value like

cls_SecId | S_Adm_No | FHeadId | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar
     1    |   1001   |    4    | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100
     1    |   100    |    5    | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100
     1    |   1001   |    5    | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100



Now I want repsent above mentioned table like

cls_SecId | S_Adm_No| Installment |  Amount                         |  Payable_Date
     1    |   1001  | Quarter-1   | sum of 3 columns (Apr+May+Jun)  |  PayableDate will be mannual as 15.04.2015
     1    |   1001  | Quarter-2   | sum of 3 columns (Jul+Aug+Sep)  |  PayableDate will be mannual as 15.07.2015
     1    |   1001  | Quarter-3   | sum of 3 columns (Oct+Nov+Dec)  |  PayableDate will be mannual as 15.10.2015
     1    |   1001  | Quarter-4   | sum of 3 columns (Jan+Feb+mar)  |  PayableDate will be mannual as 15.01.2016


What I have tried:

...........................................................
Posted
Updated 20-Aug-16 22:23pm
v5

Try this:
DECLARE @Fee_Assign_to_Students TABLE(cls_SecId INT, S_Adm_No INT, FHeadId INT, Apr INT, May INT, Jun INT, Jul INT, Aug INT, Sep INT, Oct INT, Nov INT, Dec INT, Jan INT, Feb INT, Mar INT)

INSERT INTO @Fee_Assign_to_Students (cls_SecId, S_Adm_No, FHeadId, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, Jan, Feb, Mar)
VALUES(1, 1001, 4, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100),
(1, 1001, 5, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100),
(1, 1001, 5, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100)

SELECT cls_SecId, S_Adm_No, Installment, SUM(Amount) AS AMount, CASE
		WHEN  Installment = 'Q1' THEN '15.04.2015'
		WHEN  Installment = 'Q2' THEN '15.07.2015'
		WHEN  Installment = 'Q3' THEN '15.10.2015'
		WHEN  Installment = 'Q4' THEN '15.01.2015' END AS Payable_Date
FROM (
	SELECT cls_SecId, S_Adm_No, Apr + May + Jun AS Q1, Jul + Aug + Sep AS Q2, Oct + Nov + Dec AS Q3, Jan + Feb + Mar AS Q4
	FROM @Fee_Assign_to_Students
	) AS Pvt
UNPIVOT (Amount FOR Installment IN (Q1, Q2, Q3, Q4)) AS unPvt
GROUP BY cls_SecId, S_Adm_No, Installment

Result:
cls_SecId	S_Adm_No	Installment	Amount	Payable_Date
1			1001		Q1			900		15.04.2015
1			1001		Q2			900		15.07.2015
1			1001		Q3			900		15.10.2015
1			1001		Q4			900		15.01.2015


For further details, please see:
Using PIVOT and UNPIVOT[^]
CASE (Transact-SQL)[^]
 
Share this answer
 
v3
Comments
Member 12245539 26-Aug-16 1:32am    
Boss!
The amount would be
cls_SecId S_Adm_No Installment Amount Payable_Date
1 1001 Q1 900 15.04.2015
1 1001 Q2 900 15.07.2015
1 1001 Q3 900 15.10.2015
1 1001 Q4 900 15.01.2015

instead of
cls_SecId S_Adm_No Installment Amount Payable_Date
1 1001 Q1 300 15.04.2015
1 1001 Q2 300 15.07.2015
1 1001 Q3 300 15.10.2015
1 1001 Q4 300 15.01.2015

I you look closer into you answer then there are three rows. So

3 rows * 100=300 * 3 columns (Apr + May + Jun)= 900

Row will be increased as per requirements...

Please see it and clarify .....
Maciej Los 26-Aug-16 1:47am    
You're completely right!
Check updated solution ;)
Member 12245539 26-Aug-16 2:09am    
thank you!
it is working ...
Maciej Los 26-Aug-16 2:17am    
You're very welcome.
Cheers,
Maciej
 
Share this answer
 
Comments
Maciej Los 22-Aug-16 3:10am    
Daniel, OP don't need to use PIVOT. He needs to use UNPIVOT. So, your answer is wrong. My vote of 1.
Member 12245539 22-Aug-16 3:56am    
Sir
Through declare is it working fine.

But If I get value from table Fee_Assign_to_Students then the values come like



1 2016/001 Q1 600 15.04.2015
1 2016/001 Q1 1500 15.04.2015
1 2016/001 Q1 15000 15.04.2015
1 2016/001 Q2 0 15.07.2015
1 2016/001 Q3 0 15.10.2015
1 2016/001 Q4 0 15.01.2015
1 2016/018 Q1 600 15.04.2015
1 2016/018 Q1 1500 15.04.2015
1 2016/018 Q1 15000 15.04.2015
1 2016/018 Q2 0 15.07.2015
1 2016/018 Q3 0 15.10.2015
1 2016/018 Q4 0 15.01.2015

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