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 |
|
JasonJanofsky
Starting Member
5 Posts |
Posted - 2007-01-28 : 01:01:41
|
| Hi,I am trying to create a report from a table like the following:ID PolicyNumber ProdID____ _____________ ________1 20 12 Null 13 25 24 30 25 Null 2Basically I am trying to get a result set that looks similar to this:ProdID TotalPolicys TotalPolicys with PolicyNumber is Null______ ____________ ____________1 2 12 3 2I will be running a percentage on the two numbers at some point, so the end product would be like:ProdID Ratio______ _______1 .502 .75I have thought of using some kind of loop to iterate though each prodID to get the ratios into another table but I have a feeling one of you awesome SQL guys might have a way to get this done. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-28 : 01:22:19
|
[code]declare @table table( id int, PolicyNumber int, ProdID int)insert into @tableselect 1, 20, 1 union allselect 2, NULL, 1 union allselect 3, 25, 2 union allselect 4, 30, 2 union allselect 5, NULL, 2select ProdID, TotalPolicys = count(*), [Total Policy with PolicyNumber is not null] = count(PolicyNumber), [Total Policy with PolicyNumber is null] = count(*) - count(PolicyNumber), Ratio = count(PolicyNumber) * 1.0 / count(*)from @tablegroup by ProdID[/code] KH |
 |
|
|
|
|
|
|
|