I need to add the userid at the right for the btitle. But I only get USER??
this is the query I used.
BTITLE LEFT report_date CENTER 'Page: ' FORMAT 999 SQL.PNO-
RIGHT USER
/
It is SQL.USER you should use:
SQL> BTITLE LEFT report_date CENTER 'Page: ' FORMAT 999 SQL.PNO- RIGHT SQL.USER /
SQL> select * From dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
1 a b
report_date Page: 1- SCOTT/
SQL> show user
USER is "SCOTT"
SQL>
Related
I need to make a job application report, I need to produce a table where the duplicated job_id and job_name will be removed from the table
And I actually want to compute total applicants who apply for the job and then I need to get remaining job vacancies (no_of_vacancies - count()) and get the percentage by (count()/no_of_vacancies*100)
Applicant ID
Job ID
Job Name
No of Vacancies
Remaining Job Vacancies
Percentage(%)
A0001
Clinical Specialist
J0001
30
24
20
A0072
A0076
but I could only get this
Applicant ID
Job ID
Job Name
No of Vacancies
Remaining Job Vacancies
Percentage(%)
A0001
Clinical Specialist
J0001
30
29
3
A0072
Clinical Specialist
J0001
30
29
3
A0076
Clinical Specialist
J0001
30
29
3
The duplicated job id and name, no of vacancies and remaining vacancies is still there, and the percentage they shown is incorrect too
Here's the code:
SET pagesize 30
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
ACCEPT v_jobID CHAR FORMAT 'A5' PROMPT ' Enter job id: '
COLUMN job_id FORMAT A12 HEADING "Job ID";
COLUMN job_name FORMAT A20 HEADING "Job Name";
COLUMN applicant_id FORMAT A12 HEADING "Applicant ID";
COLUMN no_of_vacancies FORMAT 999 HEADING "No of Vacancies";
COLUMN Remaining_Job_Vacancies FORMAT 999 HEADING "Remaining Job Vacancies";
COLUMN Percentage FORMAT 999 HEADING "Percentage(%)";
BREAK on J.job_id on job_name skip2 on no_of_vacancies, Remaining_Job_Vacancies, Percentage;
COMPUTE number LABEL 'Total Applicants' ON A.applicant_id;
TTITLE CENTER 'Job Application Report for ' _DATE -
RIGHT 'Page No: ' FORMAT 999 SQL.PNO SKIP 2
SELECT A.applicant_id, J.job_id, job_name, no_of_vacancies, (no_of_vacancies - count(*)) AS Remaining_Job_Vacancies, (count(*)/no_of_vacancies*100) AS Percentage
FROM applicant A, job J, application AP
WHERE A.applicant_id = AP.applicant_id
AND AP.job_id = J.job_id
AND J.job_id LIKE '&v_jobID'
GROUP BY A.applicant_id , J.job_id, job_name, no_of_vacancies
ORDER BY J.job_id;
--CLEAR COLUMNS
--TTITLE OFF
That's because you don't break on table_alias.column_name
BREAK on J.job_id
--
^
|
this
but only on column name (or its alias):
BREAK on job_id
I don't have your tables nor data, so I'll use Scott's sample schema to illustrate it.
This is what you did:
SQL> break on e.deptno
SQL>
SQL> select e.deptno, e.ename from emp e order by e.deptno;
DEPTNO ENAME
---------- ----------
10 CLARK
10 KING
10 MILLER
20 JONES
20 FORD
20 ADAMS
20 SMITH
20 SCOTT
30 WARD
30 TURNER
30 ALLEN
30 JAMES
30 BLAKE
30 MARTIN
14 rows selected.
This is what you should have done:
SQL> break on deptno
SQL>
SQL> select e.deptno, e.ename from emp e order by e.deptno;
DEPTNO ENAME
---------- ----------
10 CLARK
KING
MILLER
20 JONES
FORD
ADAMS
SMITH
SCOTT
30 WARD
TURNER
ALLEN
30 JAMES
BLAKE
MARTIN
14 rows selected.
SQL>
I would like to compare two SQL statements which produce the same result. For example
SELECT
id, name,
(SELECT street || ' ' || town from addresses WHERE addresses.id=customers.addressid) AS address
FROM customers;
-- vs
SELECT
id, name,
street || ' ' || town AS address
FROM customers c LEFT JOIN addresses a ON c.id=a.customerid;
Here I want to compare the performance of the subquery compared to using a join. I know about the other advantages and disadvantages of subqueries and joins, but I’m more interested in comparing their performance.
How can I do a performance comparison? Is there some sort of timing test?
Is there some sort of timing test?
Yes, in SQL*Plus, do it as
SQL> set timing on
and then run queries, each of them several times (to avoid caching issues) and compare time needed for each query to complete. Note that on small data sets you won't notice any difference, so - whichever you use, it'll be OK.
SQL> select e.deptno, (select d.dname from dept d where d.deptno = e.deptno) dname, e.ename
2 from emp e
3 where e.deptno = 20;
DEPTNO DNAME ENAME
---------- -------------- ----------
20 RESEARCH SMITH
20 RESEARCH JONES
20 RESEARCH SCOTT
20 RESEARCH ADAMS
20 RESEARCH FORD
Elapsed: 00:00:00.04
SQL> select e.deptno, d.dname, e.ename
2 from emp e join dept d on e.deptno = d.deptno
3 where e.deptno = 20;
DEPTNO DNAME ENAME
---------- -------------- ----------
20 RESEARCH SMITH
20 RESEARCH JONES
20 RESEARCH SCOTT
20 RESEARCH ADAMS
20 RESEARCH FORD
Elapsed: 00:00:00.03
SQL>
Other than that, did you compare explain plans? Did you collect statistics on tables and indexes (and do it regularly)? Are there any indexes (should be on columns used in joins, most probably)?
I need to do a report with a procedure of every salesman in 6 fake companies if they sold more then the number the user entered. i'm in sql developper with a oracle Db.
I asked to ppl in my class how they did and the thing they did doesnt work for me, i always get error on the accept/prompt.
/
accept temp default '100';
/
when '&temp' < 90.00 then raise too_cold;
I also tried using it like that:
accept temp prompt 'Input degree (numerically in degrees F)?:';
I want to show a prompt with words so that the user knows what to enter and use the number i get.
Help yourself.
SQL> help accept
ACCEPT
------
Reads a line of input and stores it in a given substitution variable.
ACC[EPT] variable [NUM[BER] | CHAR | DATE | BINARY_FLOAT | BINARY_DOUBLE]
[FOR[MAT] format] [DEF[AULT] default] [PROMPT text | NOPR[OMPT]] [HIDE]
Here we go:
SQL> set ver off
SQL>
SQL> accept temp number default 20 prompt 'Enter department number: '
Enter department number: 10
SQL> select deptno, ename from emp where deptno = &temp;
DEPTNO ENAME
---------- ----------
10 CLARK
10 KING
10 MILLER
SQL>
If nothing's entered, the default value takes place:
SQL> accept temp number default 20 prompt 'Enter department number: '
Enter department number:
SQL> select deptno, ename from emp where deptno = &temp;
DEPTNO ENAME
---------- ----------
20 SMITH
20 JONES
20 SCOTT
20 ADAMS
20 FORD
SQL>
How to create a temporary table in oracle without knowing the number and name of columns.
For example:
Select columnA,columnB,* into temp_table from tableA.
Here,tableA maynot be a simple tablename but maybe derived from many queries.
How can this be achieved?Is there any alternative to this?
In Oracle, you have to first create a table, then insert into it. Or, create it directly (as in my example).
Note that I've created a "normal" table; if it were temporary, you could have chosen between a global or private (depending on database version you use).
For this discussion, I guess that it is just fine:
SQL> create table temp_table as
2 select a.*
3 from (select d.deptno, d.dname, e.ename --> this SELECT is your "tableA"
4 from emp e join dept d
5 on e.deptno = d.deptno
6 where job = 'CLERK'
7 ) a;
Table created.
SQL> select * from temp_table;
DEPTNO DNAME ENAME
---------- -------------------- ----------
10 ACCOUNTING MILLER
20 RESEARCH SMITH
20 RESEARCH ADAMS
30 SALES JAMES
SQL>
Alternatively, create a view and work with it:
SQL> create or replace view v_temp as
2 select d.deptno, d.dname, e.ename
3 from emp e join dept d
4 on e.deptno = d.deptno
5 where job = 'CLERK'
6 ;
View created.
SQL> select * from v_temp;
DEPTNO DNAME ENAME
---------- -------------------- ----------
10 ACCOUNTING MILLER
20 RESEARCH SMITH
20 RESEARCH ADAMS
30 SALES JAMES
SQL>
This statement creates temp_table which contains all columns and data from tableA and two other empty columns, varchar and numeric.
create table temp_table as
select cast (null as varchar2(10)) columnA,
cast (null as number(6)) columnB,
tableA.*
from tableA
If you need only structure, no data, then add:
where 1 = 0
I am not sure why you want to have a temp table as you do not know columns, but you can think of dynamic SQL to create table depending on columns required during your process and then drop it again. From my point of view I think it is not a good design.
I can suggest to think on using collection with 'x' number of columns with datatype as VARCHAR2. During transaction you can populate and process according to you need and it will also remain for that session.
Does anyone know why this select statement on this query runs just fine but when i add cte as in front of the same select statement it gives this error:
SQL Error: ORA-00957: duplicate column name)
CREATE TABLE t1 AS
SELECT *
FROM NS_F3
LEFT JOIN NS_FA2
ON NS_F3.PI_CANDIDATE_NUM = NS_FA2.PI_CANDIDATE_NUM
WHERE REGEXP_LIKE(NS_F3.TITLE, 'intern($|ship|[^a-z])', 'i');
It is because tables NS_F3 and NS_FA2 contain columns with the same name - in that case, you should use column alias to avoid duplicate column names.
Here's an example: I'm creating a simple table, as extract of several columns from Scott's EMP table:
SQL> create table t_first as select deptno, empno, ename from emp where rownum < 5;
Table created.
Join it to DEPT table:
SQL> select * from t_first e join dept d on e.deptno = d.deptno;
DEPTNO EMPNO ENAME DEPTNO DNAME LOC
---------- ---------- ---------- ---------- -------------- -------------
20 7369 SMITH 20 RESEARCH DALLAS
20 7566 JONES 20 RESEARCH DALLAS
30 7521 WARD 30 SALES CHICAGO
30 7499 ALLEN 30 SALES CHICAGO
^^^^^^^^^ ^^^^^^^^
this is DEPTNO column ... ... and here's another one
As long as it works in SELECT, it won't work in CREATE TABLE:
SQL> create table test as
2 select * from t_first e join dept d on e.deptno = d.deptno;
select * from t_first e join dept d on e.deptno = d.deptno
*
ERROR at line 2:
ORA-00957: duplicate column name
The solution is to use a column alias, such as:
SQL> create table test as
2 select e.deptno emp_deptno, --> first alias
3 e.empno,
4 e.ename,
5 d.deptno, dept_deptno --> second alias
6 d.dname,
7 d.loc
8 from t_first e join dept d on e.deptno = d.deptno;
Table created.
SQL> select * From test;
EMP_DEPTNO EMPNO ENAME DEPT_DEPTNO DNAME LOC
---------- ---------- ---------- ----------- -------------- -------------
20 7369 SMITH 20 RESEARCH DALLAS
20 7566 JONES 20 RESEARCH DALLAS
30 7521 WARD 30 SALES CHICAGO
30 7499 ALLEN 30 SALES CHICAGO
SQL>