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 |
|
sqldev6363
Yak Posting Veteran
54 Posts |
Posted - 2011-01-06 : 23:36:01
|
| Hi,this is the my result table GID ID Cap1 001 Y1 002 N1 003 Y2 010 Y2 011 N3 012 Y3 013 N3 014 Y4 101 Y4 102 Nand in the final result set i need the output like thisGID ID Count percentage Cap1 001 3 100*1/3 Y1 002 3 100*1/3 N1 003 3 100*1/3 Y2 010 2 100*3/2 Y2 011 2 100*3/2 N3 012 3 100*6/3 Y3 013 3 100*6/3 N3 014 3 100*6/3 Y4 101 2 100*2/2 Y4 102 2 100*2/2 NHere in the count column it should depend on how many IDs are present for each GID and the in percentage that 100*(this number i am getting from other field, that i will take care)/(count of IDs for each GID)...Example: For one GID if contains 5 IDs then count should be 5..this is the logic i need how to get this ..i am new to this SQL can any one help me out to get the query.dev |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-06 : 23:46:45
|
| [SNIP]@Here in the count column it should depend on how many IDs are present for each GID[SNIP]for this you an use the following query Select GID,count(ID) From TableName Group by GID |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-01-06 : 23:54:41
|
if you are using SQL 2005/2008the_Count = count(*) over (partition by GID) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2011-01-07 : 00:05:04
|
| check this one...SELECT GID,ID,COUNT(GID)AS COUNT,'100*1/3' AS PERCENTAGE,CAP FROM TEST11GROUP BY GID,ID,CAP |
 |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2011-01-07 : 00:53:56
|
PLEASE REPLACE THE CODE WITH THIS CODE.SELECT GID,ID,count(*) over (partition by GID)as COUNT,'100*1/3' AS PERCENTAGE,CAP FROM TEST11GROUP BY GID,ID,CAPquote: Originally posted by ahmeds08 check this one...SELECT GID,ID,COUNT(GID)AS COUNT,'100*1/3' AS PERCENTAGE,CAP FROM TEST11GROUP BY GID,ID,CAP
|
 |
|
|
|
|
|
|
|