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 0Please 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
- Dozens more answers to tough SQL questions from Jason Law are available here.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
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.