Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Percentage

Status
Not open for further replies.

jgoodman00

Programmer
Jan 23, 2001
1,510
I have the following query results & I need to calculate the perct column based on the total for a type (e.g. Bramble).
Type Priority Count Perct
Bramble A 25 62.5
Bramble B 15 37.5


Any suggestions?

James Goodman MCSE, MCDBA
 
can you post the query as it is now ?

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
I've made a couple of assumptions about your data, but I think this will do.
Code:
[Blue]SELECT[/Blue] M1.Type[Gray],[/Gray] Priority[Gray],[/Gray] [Fuchsia]SUM[/Fuchsia][Gray]([/Gray]Quantity[Gray])[/Gray] SubTotal[Gray],[/Gray] 
   [Blue]CASE[/Blue] [Blue]WHEN[/Blue] TotalQty[Gray]>[/Gray]0 [Blue]THEN[/Blue] SubTotal/TotalQty 
        [Blue]ELSE[/Blue] 0 [Blue]END[/Blue] Perct 
   [Blue]FROM[/Blue] MyTable M1 [Blue]GROUP[/Blue] [Blue]BY[/Blue] Type[Gray],[/Gray] Priority [Blue]INNER[/Blue] [Blue]JOIN[/Blue] 
      [Gray]([/Gray][Blue]SELECT[/Blue] Type[Gray],[/Gray] [Fuchsia]SUM[/Fuchsia][Gray]([/Gray]Quantity[Gray])[/Gray] TotalQty 
       [Blue]FROM[/Blue] MyTable [Blue]GROUP[/Blue] [Blue]BY[/Blue] Type[Gray])[/Gray] M2
   [Blue]ON[/Blue] M1.Type[Gray]=[/Gray]M2.Type
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top