Ask the Expert

Break down dollars into number of bills

I want to divide the salary according to currence notes just like this:
 

empno    sal  50$  100$ 500$ 
------   ---- ---- ---- ---- 
  1001   200   0    2    0   
  1002   500   0    2    1      
  1003   950   1    4    1      
  1004   350   1    3    0
Please help me.

    Requires Free Membership to View

Below I've included two separate ways to solve this problem. They use entirely different approaches, but they produce the same results. Of course, either one could be expanded to include smaller bills, even coins. Although they both may be a little tough to follow, close inspection should divulge a pattern for extension.

select empno, salary, 
       trunc(salary/500) as "$500", 
       trunc( ( salary - (trunc(salary/500)*500) ) /100) as "$100",
       trunc( ( salary - (trunc(salary/500)*500) ? 
                trunc( ( salary - (trunc(salary/500)*500) ) /100)*100 ) /50) as "$50"
  from emp

select empno, salary, 
       fivehun as "$500", 
       onehun as "$100", 
       trunc(whatsleft/50) as "$50"
  from ( select empno, salary, 
                fivehun, 
                trunc(whatsleft/100) as onehun, 
                whatsleft-(trunc(whatsleft/100)*100) as whatsleft
           from ( select empno, salary, 
                         trunc(salary/500) as fivehun, 
                         salary-(trunc(salary/500)*500) as whatsleft 
                    from emp
                )
       )

For More Information


This was first published in October 2002

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: