Problem solve Get help with specific problems with your technologies, process and projects.

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.

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, 
                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 last published in October 2002

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

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

Please create a username to comment.