Ask the Expert

Combining two SQL statements and showing result as percentage

I have taken over a stalled project and now I am struggling to understand SQL while developing a Web application. The problem is the method of combining result sets has eluded me. I am pulling my hair out trying to work it out. Basically, I have two select statements that I need to combine. I have a statement for 'Actual Fees Billed':
 SELECT SUM(ld_period1) as July FROM LS_Lawyer_Data WHERE (ld_year='2004') and (ld_type=1) AND ld_status='A' AND (ld_lawyerid='::ly_id::')
And another statement for 'Potential fees':
 SELECT SUM((LT_TIME.tm_Units/12)* (LD_Lawyer.ly_stdrate*12) ) as fees FROM LT_TIME, ld_lawyer WHERE (LD_lawyer.ly_id=LT_TIME.tm_lyid) AND (LT_TIME.tm_lyid='::ly_id::') AND (LD_lawyer.ly_id='::ly_id::') AND (LT_TIME.tm_Date >= '7/01/2003') AND (LT_TIME.tm_Date <= '7/31/2003') AND (LT_TIME.tm_Units > 0) AND (LT_TIME.tm_Amount <> 0) AND (LT_TIME.tm_txtype=1) AND (LT_TIME.tm_status<>2) Group by LD_Lawyer.ly_stdrate
I have to divide the results of the first one by the results of the second one... then multiply it by 100 and show it as a percentage. I don't know the basic technique of combining results let alone how to write the appropriate query in this instance. Any assistance will be greatly appreciated.

    Requires Free Membership to View

Try:

 Select (ResultA.July/ResultB.fees) * 100 From (SELECT SUM(ld_period1) as July FROM LS_Lawyer_Data WHERE (ld_year='2004') and (ld_type=1) AND ld_status='A' AND (ld_lawyerid='::ly_id::')) AS ResultA, (SELECT SUM((LT_TIME.tm_Units/12)* (LD_Lawyer.ly_stdrate*12) ) as fees FROM LT_TIME, ld_lawyer WHERE (LD_lawyer.ly_id=LT_TIME.tm_lyid) AND (LT_TIME.tm_lyid='::ly_id::') AND (LD_lawyer.ly_id='::ly_id::') AND (LT_TIME.tm_Date >= '7/01/2003') AND (LT_TIME.tm_Date <= '7/31/2003') AND (LT_TIME.tm_Units > 0) AND (LT_TIME.tm_Amount <> 0) AND (LT_TIME.tm_txtype=1) AND (LT_TIME.tm_status<>2) Group by LD_Lawyer.ly_stdrate) AS ResultB

 

For More Information

This was first published in March 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: