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.
| Author |
Topic |
|
gauravsh27
Starting Member
2 Posts |
Posted - 2009-04-13 : 14:29:09
|
| Hi! everyoneI got this requirement and not able to find a suitable method to complete this...My table Table1 has three columnsID - Primary KeyCentreId - Foreign key of some other tableNAMEPercentageRecords are like this in the table1 1 abc NULL2 2 lmn NULL3 2 opq NULL4 3 xyz1 NULL5 3 xyz2 NULL6 3 xyz3 NULLI want to show output like this1 1 able 100%2 2 lmn 50%3 2 opq 50%4 3 xyz1 33%5 3 xyz2 33%6 3 xyz3 33%Suppose if there are 3 records of a particular foreign key i shd gv each record percentage as 33%and if there are 4 records of a particular foreign key i shd gv each record percentage as 25% and so onI want to write this with a single select statementany help would be highly appreciatedThanks in advance...and suppose Table1 Contains sm records with percentage already defined my query wud leave those records the way they aresuppose the records are like this1 10 adads NULL2 20 sadasd 50%2 20 sdfsdf 50%Output shd be1 10 adads 100%2 20 sadasd 50%2 20 sdfsdf 50%Looking to hear from u all... |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-04-13 : 14:37:25
|
| This??declare @t table (priid int,forid int, nme varchar(10),perc int)insert @t select 1,1,'abc',null union allselect 2,2,'def',null union allselect 3,2,'ijk',null union allselect 4,3,'lmn',null union allselect 5,3,'opq',null union allselect 6,3,'rst',null union allselect 7,4,'tst',90 update a set a.perc = 100 / b.forcntfrom @t a inner join (select forid,count(forid) as forcnt from @t group by forid) bon a.forid = b.forid and a.perc is nullselect * from @tResult-------priid forid nme perc----------- ----------- ---------- -----------1 1 abc 1002 2 def 503 2 ijk 504 3 lmn 335 3 opq 336 3 rst 337 4 tst 90 |
 |
|
|
gauravsh27
Starting Member
2 Posts |
Posted - 2009-04-13 : 15:06:31
|
Thanks vijay... Its Rocking!!!quote: Originally posted by vijayisonly This??declare @t table (priid int,forid int, nme varchar(10),perc int)insert @t select 1,1,'abc',null union allselect 2,2,'def',null union allselect 3,2,'ijk',null union allselect 4,3,'lmn',null union allselect 5,3,'opq',null union allselect 6,3,'rst',null union allselect 7,4,'tst',90 update a set a.perc = 100 / b.forcntfrom @t a inner join (select forid,count(forid) as forcnt from @t group by forid) bon a.forid = b.forid and a.perc is nullselect * from @tResult-------priid forid nme perc----------- ----------- ---------- -----------1 1 abc 1002 2 def 503 2 ijk 504 3 lmn 335 3 opq 336 3 rst 337 4 tst 90
|
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-04-13 : 15:25:55
|
welcome |
 |
|
|
|
|
|
|
|