Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Insert values in a table based on another table

Author  Topic 

Manushi
Starting Member

1 Post

Posted - 2011-03-28 : 09:54:28
Hi,

I need to write a Stored Procedure.
I have a table with some data now I need to insert relevant data into another table according to some condition

Example:

Table 1

Name Class Math Physics English
Alok V 60 50 45
Bobby V 78 87 86
Chandini VI 56 76 56
Dolly VII 87 56 66

Based on this condition

Insert Values into Table2
If class =V
(Table2.Physics=Select sum(Table1.Physics) from Table1 where Class like ‘V’
Table2.Maths=0 and table2.English=0)

If class =VI
(Table2.Maths=Select sum(Table1.Maths) from Table1 where Class like ‘VI’
Table2.Physics=0 and table2.English=0)

If class =VI
(Table2.English =Select sum(Table1.English) from Table1 where Class like ‘VII’
Table2.Physics=0 and table2.Maths =0)
]

 

Table 2

Class Math Physics English
V 0 137 0
VI 56 0 0
VII 0 0 66


Kindly Help me out

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-03-28 : 10:00:10



Declare @t1 table (
Name varchar(10),
Class varchar(10),
Math int,
Physics int,
English int
)
Insert Into @t1

Select 'Alok', 'V', 60, 50, 45 Union All
Select 'Bobby', 'V', 78, 87, 86 Union All
Select 'Chandini', 'VI', 56, 76, 56 Union All
Select 'Dolly', 'VII', 87, 56, 66


Select
Class,
Physics = sum(case Class when 'V' then Physics else 0 end),
Math = sum(case Class when 'VI' then Math else 0 end),
English = sum(case Class when 'VII' then English else 0 end)
From @t1
Group By Class


Corey

snSQL on previous signature "...Oh and by the way Seventhnight, your signature is so wrong! On so many levels, morally, gramatically and there is a typo!"
Go to Top of Page
   

- Advertisement -