Q

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, 
                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

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

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:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close