Oracle SQL: Replace numbers with variable-length sequences of asterisks - oracle

I have a table with employee names and their wages. Now I want to concatenate them in a column. So far so good.
But instead of the normal number (ex. $1200) it needs to show me in the query a star (*) for every $100.
The result should look like this:
Employees and their wage
Smith, **********
Jonson, ******************
Sanchez, *************
I have searched everywhere with every search term which came to my mind. Can it be that it is not possible in Oracle SQL Developer? I use database 11g.
I would be happy if someone could direct me to the right answer. Thanks in advance.

Given a salary value you can create a string of asterisks for every $100 dollars of salary with
RPAD('*', TRUNC(SALARY/100), '*')
SQLfiddle here
Best of luck.

Related

Oracle - Trigger That When Given One Value Will Fill Another Value in the Same Table?

I'm trying to assist my boyfriend with a project - I have the utmost respect for everyone on here, I hope my lack of knowledge is okay and this question makes sense: He needs a trigger that, when a number is entered in a table, will automatically generate a value in the same table?
For example, if you put $600 under Rent, it would auto-generate 10% of that under 'fee'.
thank you in advance for your assistance!
here is an example :
CREATE OR REPLACE TRIGGER YourTableName_BIU
BEFORE INSERT OR UPDATE OF Rent ON YourTableName
FOR EACH ROW
BEGIN
:new.fee := :new.Rent*10;
END;
Almost there. The problem is with the "* 10". That will give you a fee of 6000 for the 600 rent not the 60 desired. You just need to shift the decimal point; change it to "*.10".

"group by day" in Oracle doesn't appear to group by date

My query looks something like this:
select datesent, count(*) the_count from receivedmessaged where status=5000
and datesent>(to_date('20130101', 'YYYYMMDD')) group by datesent
What I'm looking for is a table that has the count of messages with a status of 5000 per day, newer than a certain date. What I'm getting is a table with the same dates over and over. What I think is happening is that there is a hidden time part in that datesent field, and its grouping the entries by the exact time they were sent, rather than just looking at the date. Can anyone confirm this and tell me how I can fix it? Thanks!
What I think is happening is that there is a hidden time part in that datesent field, and its grouping the entries by the exact time they were sent, rather than just looking at the date.
That's very probably what's happening. So try that:
select TRUNC(datesent), count(*) the_count from receivedmessaged where status=5000
and datesent>(to_date('20130101', 'YYYYMMDD')) group by TRUNC(datesent)
TRUNC will remove the "time part" and allow you to group by day.
Please note that the use of TRUNC wil invalidate your index. Take a look at your execution plan. And if needed, you should add a function-based index on TRUNC(datesend).
Of course, using TRUNC would solve your issue, and using a function-based index would make it efficient.
However, from 11g onwards, you could also use VIRTUAL colums. In your case, you can add a virtual column as new_date ALWAYS GENERATED AS (TRUNC(date_column)). You just need to use this virtual column in your query. For performance improvement, if required, you could create an index.
NOTE : Indexes defined against virtual columns are equivalent to function-based indexes.

Creating Easy Way to update variables in large code

I am using Oracle SQL Developer and have a rather large query built. The query is going to be run on a monthly or quarterly basis. I was wondering if there was a way that I can do a declare statment up top and then in the code just reference these variables created. That way when someone wants to run the query they can just change the dates at the top of the code rather then have to dig through all of it. I am kind of new to Oracle SQL Developer but I know in other sql codes I built I could simply declare the variable and then set it and then in the code call the variable name. Below is an example of what I know how to do but i am having trouble in Oracle SQL Developer.
Example: I have a data base that contains the columns Business, business type(small,medium,large) number of deposits, deposit amount and deposit date. I want to build a query that outputs a quarterly summary of the number of deposits and the deposit amount and be able to change the quarter and size of the business.
Example Code from my previous SQL expereince this is an example of what I am trying to do since i can not disclose my code with the table names etc in them.
Declare #busstype,#qbegindate,#qenddate
Set #busstype = 'small'
Set #qbegindate = '01-JAN-2013'
Set #qenddate = '01-MAR-2013'
Select business,numberofdeposits,depositamount
From business_transactions
Where ('#qbegindate'<=depositdate<='#qenddate'
And businesstype = '#busstype')
Group By Business
The results would list out the businesses name and then the total deposits and total amount.
I know this code is not right but its just an example of what I am looking to do in Oracle SQL Developer. The query I have built is working fine I just find it a pain to dig through the code to change dates and criteria and was wondering how I would do something like this since i have figured out that I am not able to do this in ORACLE Sql Developer.
Here is an example with predefined variable:
set feedback off
var abc varchar2
begin
:abc := 'abc';
end;
/
select :abc as a from dual;
Output:
A
--------------------------------
abc
Common table expressions allow variables to be defined at the top of the query. For performance and style reasons this is generally not a good way to
use common table expressions. The advantage is this query can be run in any IDE and it is completely self-contained.
--Variables - change these before running.
with busstype as (select 'small' value from dual),
qbegindate as (select date '2013-01-01' value from dual),
qenddate as (select date '2013-03-01' value from dual)
--Query - do not modify code below.
select business,numberofdeposits,depositamount
from business_transactions
where depostiddate between
(select value from qbegindate)
and
(select value from qenddate)
and businesstype = (select value from busstype)
group by business, numberofdeposits,depositamount;

How does Inline view differ from Inline table in oracle?

Could anyone tell the difference between Inline view and Inline table ?
Explanation with SQL code might be good to understand the concept easily.
"this question has been asked to me in an interview."
We hear this a lot. The problem with these type of questions is you're asking the wrong people: you should have have the courage to say to your interviewer, "I'm sorry, I'm not familiar with the term 'inline table' could you please explain it?"
Instead you ask us, and the thing is, we don't know what the interview had in mind. I agree with Alex that the nearest thing to 'inline table' is the TABLE() function for querying nested table collections, but it's not a standard term.
I have been on the opposite side of the interviewing table many times. I always give credit to a candidate who asked me to clarify a question; I always mark down a candidate who blusters.
The world is struggling to optimize the database query, so am I. Well if I say I have something which can speed the application by factors to 80%, if used at right situations, what will you say...
Here I give you a problem, suppose you want to calculate the lowest and highest salary across the department with the name of employees with their respective manager.
One way to do it is to create a temp table which contain the aggregated salary for the employees.
create table tmp_emp_sal as select t.emp_id,max(t.sal) as maxsal,min(t.sal) as minsal,avg(t.sal) as avgsal from sal t group by t.emp_id
and then use it in query further.
select concat(e.last_nm, e.first_nm) as employee_name,concat(m.last_nm,m.first_nm) as manager_name,tt.maxsal,tt.minsal,tt.avgsal from emp e,emp m,dept d,tmp_test tt where e.dept_id = d.dept_id and s.emp_id = tt.emp_id and e.mgr_id = m.emp_id order by employee_name, manager_name
Now I will optimize the above code by merging the two DML and DDL operations in to a single DML query.
select concat(e.last_nm, e.first_nm) as employee_name,concat(m.last_nm, m.first_nm) as manager_name,tt.maxsal,tt.minsal,tt.avgsal from emp e,emp m, dept d,(select t.emp_id, max(t.sal) as maxsal, min(t.sal) as minsal, avg(t.sal) as avgsal from sal t group by emp_id) tt where e.dept_id = d.dept_id and s.emp_id = tt.emp_id and e.mgr_id = m.emp_id order by employee_name,manager_name
The above query saves user from the following shortcomings :-
Eliminates expensive DDL statements.
Eliminates a round trip to the database server.
Memory usage is much lighter because it only stores the final result rather than the intermediate steps as well.
So its preferable to use inline views in place of temp tables.

number format in oracle

Hai,
i have a problem with number format.i'm using oracle.
I have a number field in database.But when i retreive it i need to be seen as floating point number
For example:
while retreiveing,now i got the result as 200 DR (DR for Debit,it is given manually).
Now i need to get the result as 200.00 DR as the result.
How can i solve this?Can any one help me?
You can use the TO_CHAR function to explicitely format data:
SQL> SELECT to_char(200.00, 'fm999G999G990D00') FROM dual;
TO_CHAR(200.00,'FM999G999G990D
------------------------------
200.00
Use TO_CHAR like this:
to_char(number,'999999.99')
For example:
SQL> select to_char(1234.5678,'999999.99')||' DR' as display from dual;
DISPLAY
-------------
1234.57 DR
The presence of a debit implies the need for a credit. In Oracle SQL we can use the SIGN() function to tell whether a number is positive or negative...
SQL> select to_char(abs(amt), 'fm999g999d00')||' '
2 ||case when sign(amt) = -1 then 'DR' else 'CR' end as fmt_amt
3 from transactions
4 order by txn_ts, txn_id
5 /
FMT_AMT
--------------
200.00 CR
200.00 DR
788.67 CR
788.67 DR
SQL>
The answers here that suggested TO_CHAR are correct, but if you're calling this SQL from the application code:
Get the number without formatting it with the SQL and then use your programming language to format it. For example, in Java, use the DecimalFormat class. In other words, leave formatting for the application specific code, not for the SQL.
The additional characters can be specified as part of the conversion by enclosing them in double-quotes, which might make things a little more simple:
To_Char(amount,'fm999G999G990D00" DR"')

Resources