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 |
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2011-10-14 : 14:53:26
|
| I want to add a column of a percentage of the total counts for each record. How do I do this. Here is the code so farSELECT COUNT(dbo.OffenderType_Lookup.ID) AS Count, dbo.OffenderType_Lookup.Description AS Type, 'Active' AS StatusFROM dbo.Offender INNER JOIN dbo.NCIC_OffenderStatus ON dbo.Offender.OffenderStatus = dbo.NCIC_OffenderStatus.ID LEFT OUTER JOIN dbo.OffenderType_Lookup ON dbo.Offender.OffenderType = dbo.OffenderType_Lookup.IDWHERE (dbo.NCIC_OffenderStatus.Description LIKE N'Active%')GROUP BY dbo.OffenderType_Lookup.ID, dbo.OffenderType_Lookup.DescriptionORDER BY typeDaveHelixpoint Web Developmenthttp://www.helixpoint.com |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2011-10-14 : 20:00:57
|
| ;with Subtotals as (SELECT COUNT(dbo.OffenderType_Lookup.ID) AS Count, dbo.OffenderType_Lookup.Description AS Type, 'Active' AS StatusFROM dbo.Offender INNER JOINdbo.NCIC_OffenderStatus ON dbo.Offender.OffenderStatus = dbo.NCIC_OffenderStatus.ID LEFT OUTER JOINdbo.OffenderType_Lookup ON dbo.Offender.OffenderType = dbo.OffenderType_Lookup.IDWHERE (dbo.NCIC_OffenderStatus.Description LIKE N'Active%')GROUP BY dbo.OffenderType_Lookup.ID, dbo.OffenderType_Lookup.Description),Totals as (select sum(Count) Totalfrom Subtotals)select s.Count, 100.0 * s.Count / t.Total as PctCount, s.Type, s.Statusfrom SubTotals scross joinTotals torder by s.type(Untested)=======================================Faced with the choice between changing one's mind and proving that there is no need to do so, almost everyone gets busy on the proof. -John Kenneth Galbraith |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-15 : 02:10:43
|
| [code]SELECT COUNT(dbo.OffenderType_Lookup.ID) AS Count, dbo.OffenderType_Lookup.Description AS Type, 'Active' AS Status,COUNT(dbo.OffenderType_Lookup.ID)*100.0/COUNT(dbo.OffenderType_Lookup.ID) OVER () AS [% Total Count]FROM dbo.Offender INNER JOINdbo.NCIC_OffenderStatus ON dbo.Offender.OffenderStatus = dbo.NCIC_OffenderStatus.ID LEFT OUTER JOINdbo.OffenderType_Lookup ON dbo.Offender.OffenderType = dbo.OffenderType_Lookup.IDWHERE (dbo.NCIC_OffenderStatus.Description LIKE N'Active%')GROUP BY dbo.OffenderType_Lookup.ID, dbo.OffenderType_Lookup.DescriptionORDER BY type[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|