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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mathmatically dividing two queries

Status
Not open for further replies.

pdbowling

Programmer
Mar 28, 2003
267
US
I guess this is a 2 part question.

Can you divide two scalar queries by one another?

This obviously does not do it.
Code:
SELECT COUNT(*)
  FROM myTable
 WHERE myFlag=1 /
SELECT COUNT(*)
  FROM myTable

Secondly, what would be a good method of getting the percentage of flagged records if the division is just bad form?

I am using SQL Server 2008 Management Studio.


Thank you.
Patrick
 
Code:
SELECT SUM(CASE WHEN myFlag=1 THEN 1 ELSE 0 END) / COUNT(*)
FROM MyTable

Borislav Borissov
VFP9 SP2, SQL Server
 
[...]getting the percentage [...]
Code:
SELECT SUM(CASE WHEN myFlag=1 THEN 1 ELSE 0 END)[red][b]*1.0[/b][/red] / COUNT(*)
FROM MyTable

soi là, soi carré
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top