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

Calculate Percentage Column 2

Status
Not open for further replies.

larrydavid

Programmer
Jul 22, 2010
174
US
Hello,

IN SS2005, I am trying to calculate a percentage column based on counts from two tables:
Code:
SELECT	   [Dispute Rate] = (SELECT count(*) FROM TEST1 WHERE RECEIVED_DATE > 1/1/2011) / (SELECT count(*) FROM TEST2) * 100, 
FROM         TEST t INNER JOIN
             TEST2 t2 ON t.CLAIMNO = t2.CLAIMNO INNER JOIN
WHERE	    TEST1.PAIDDATE > '20110101'

With the above query I am returning 0. I've been through several forums and my understanding is I need an outer query, for instance with a derived table or cte. Then convert the result to varchar and add a '%' to the result. Am I on the right track? If someone could show me an example I would so greatly appreciate it.

Thanks,
Larry
 
Code:
SELECT Tst1.Cnt / Tst2.Cnt AS [Dispute Rate]
FROM (SELECT COUNT(*)*1.0 AS Cnt 
             FROM TEST1
      WHERE RECEIVED_DATE > '200110101') tst1
CROSS JOIN (SELECT COUNT(*)*1.0 AS Cnt 
             FROM TEST2) tst2



Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Boris,

Many thanks. Now I have to figure out how to implement this within my overall query.

Best Regards,
Larry
 
Sometimes, with odd calculations, it's just easier to join this value in as a derived query.


SELECT..., prcnt.MyPercentage
FROM...(Your EXISTING query)
LEFT JOIN
[tab]( percentages query) as prcnt ON
?.ID = prcnt.ID

I haz all the letters: SME, BA, QA, PM, DEV, DBA, UAT, SE, HD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top