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>
Related
For Oracle SQL problem how I can add some dots after the name to make it reach 10 characters such as ( Sara......)
and add dollar simple $ for every 100 in salary, if the salary= 500 so we need to add 5 simple$
Looks like a RPAD function, e.g.
SQL> select
2 rpad(ename, 10, '.') name,
3 sal,
4 rpad('$', (sal/100), '$') money
5 from emp
6 where deptno = 30;
NAME SAL MONEY
---------- ---------- ------------------------------
ALLEN..... 1600 $$$$$$$$$$$$$$$$
WARD...... 1250 $$$$$$$$$$$$
MARTIN.... 1250 $$$$$$$$$$$$
BLAKE..... 2850 $$$$$$$$$$$$$$$$$$$$$$$$$$$$
TURNER.... 1500 $$$$$$$$$$$$$$$
JAMES..... 950 $$$$$$$$$
6 rows selected.
SQL>
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 am new to sql.I would like to ask if there is any way to format my output to look more complex and more like one table?
My script looks like this
spool "\\PathToPutOutputInTextFile"
SELECT a.ARCHIVEID, count(*) as "Number of Documents", ROUND(SUM(c.CLENGTH)/1024/1024,2) as "Documents Size in MB"
FROM ds_doc d
INNER JOIN ds_arch a ON d.ARCHIVENO = a.ARCHIVENO
INNER JOIN ds_comp c ON d.DOCIDNO = c.DOCIDNO
GROUP BY a.ARCHIVEID;
spool off;
And I was also able to automatize this with .bat file that looks like this
sqlplus usr/pass#nameofdb #D:\IXTENT\monitoring\ASCheck.sql -path "\\PathToPutOutputInTextFile\test.txt
I was somehow manage to make this somehow work but my output looks like sh..t :/
This is my output.
ARCHIVEID
Number of Documents
Documents Size in MB
test_rt 39
3.03
IL 36
104
TN 139823
20683.57
ARCHIVEID
Number of Documents
Documents Size in MB
T5 6931
331978.15
TA 4
.34
TT 23
3.09
Is there any way to make it complex and look more like one table?
Thanks a lot.
One option would be
set echo off verify off head off feed off term off lines 120 pages 0
col "Number of Documents" for 9999999999
col "Documents Size in MB" for 9999999999
col ARCHIVEID for a30
spool "\\PathToPutOutputInTextFile"
SELECT a.ARCHIVEID, count(*) as "Number of Documents", ROUND(SUM(c.CLENGTH)/1024/1024,2) as "Documents Size in MB"
FROM ds_doc d
INNER JOIN ds_arch a ON d.ARCHIVENO = a.ARCHIVENO
INNER JOIN ds_comp c ON d.DOCIDNO = c.DOCIDNO
GROUP BY a.ARCHIVEID;
spool off;
However, if you want to use this file to load data in another database, a good option is to use set markup csv , guessing you have Oracle 12 or higher.
set echo off verify off head off feed off term off lines 120 pages 0
set markup csv delimiter ";"
spool "\\PathToPutOutputInTextFile"
SELECT a.ARCHIVEID, count(*) as "Number of Documents", ROUND(SUM(c.CLENGTH)/1024/1024,2) as "Documents Size in MB"
FROM ds_doc d
INNER JOIN ds_arch a ON d.ARCHIVENO = a.ARCHIVENO
INNER JOIN ds_comp c ON d.DOCIDNO = c.DOCIDNO
GROUP BY a.ARCHIVEID;
spool off;
There is a way; format columns.
For example:
SQL> select * from emp where rownum < 3;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- -------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17.12.80 800
20
7499 ALLEN SALESMAN 7698 20.02.81 1600 300
30
By setting columns' format, you'd get
SQL> col empno format 99999
SQL> col ename format a8
SQL> col mgr format 9999
SQL> col sal format 9G990
SQL> col comm format 990
SQL>
SQL> select * from emp where rownum < 3;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
------ -------- --------- ----- -------- ------ ---- ----------
7369 SMITH CLERK 7902 17.12.80 800 20
7499 ALLEN SALESMAN 7698 20.02.81 1.600 300 30
SQL>
If line is too short, make it longer:
SQL> set linesize 120
Or, make the page larger:
SQL> set pagesize 1000
There are different options you can use; see SQL*Plus documentation.
If you want to format your query output, they you can use RPAD(field_name, number of spaces) ... for ex RPAD(Name, 50) ... it will fix the width of each field while printing and will look in sync.
I have an issue while opening the text file of database output. The null value columns are replaced with the columns which have data. So the data is not properly formatting in the text file. I generated the text file from oracle sql developer. And after opening the text file I can see the alignment of columns is not proper(misplacing of values between null columns and columns which have data ). Can anyone please help me to solve this issue
Just guessing here, but - what you said reminds me of this. Example is based on Scott's EMP table where some employees have NULL in the COMM column:
SQL> select ename, job, comm, deptno
2 from emp
3 where deptno = 30;
ENAME JOB COMM DEPTNO
---------- --------- ---------- ----------
ALLEN SALESMAN 300 30
WARD SALESMAN 500 30
MARTIN SALESMAN 1400 30
BLAKE MANAGER 30
TURNER SALESMAN 30
JAMES CLERK 30
6 rows selected.
If you're spooling such data by concatenating columns, you get mess which is difficult to read:
SQL> select ename||'-'||job||'-'||comm||'-'||deptno
2 from emp
3 where deptno = 30;
ENAME||'-'||JOB||'-'||COMM||'-'||DEPTNO
----------------------------------------------------------
ALLEN-SALESMAN-300-30
WARD-SALESMAN-500-30
MARTIN-SALESMAN-1400-30
BLAKE-MANAGER--30
TURNER-SALESMAN--30
JAMES-CLERK--30
6 rows selected.
But, if you right-pad strings to certain length (RPAD function) and use NVL for missing data, then the result is somewhat prettier:
SQL> select rpad(ename, 10, ' ') ||'-'||
2 rpad(job , 10, ' ') ||'-'||
3 nvl(to_char(comm, '9990D00'), ' ') ||'-'||
4 deptno as result
5 from emp
6 where deptno = 30;
RESULT
---------------------------------------------------------------------
ALLEN -SALESMAN - 300,00-30
WARD -SALESMAN - 500,00-30
MARTIN -SALESMAN - 1400,00-30
BLAKE -MANAGER - -30
TURNER -SALESMAN - -30
JAMES -CLERK - -30
6 rows selected.
SQL>
I ran the simple select query in the command prompt,but the output rows are not coming in a single line. See below:
SQL> set pagesize 2000
SQL> select * from xtern_empl_rpt ;
EMP LAST_NAME
--- --------------------------------------------------
FIRST_NAME SSN
-------------------------------------------------- ---------
EMAIL_ADDR
--------------------------------------------------------------------------------
YEARS_OF_SERVICE
----------------
001 Hutt
Jabba 896743856
jabba#thecompany.com
18
002 Simpson
Homer 382947382
homer#thecompany.com
20
003 Kent
Clark 082736194
superman#thecompany.com
5
004 Kid
Billy 928743627
billythkid#thecompany.com
9
005 Stranger
Perfect 389209831
nobody#thecompany.com
23
006 Zoidberg
Dr 094510283
crustacean#thecompany.com
1
6 rows selected.
SQL>
Could you please help me to make each rows in a single line?
Edit
I tried below,but still is not prettified.
SQL> SET LINESIZE 4000
SQL> select * from xtern_empl_rpt ;
EMP LAST_NAME FIRST_NAME
SSN EMAIL_ADDR
YEARS_OF_SERVICE
--- -------------------------------------------------- -------------------------
------------------------- --------- --------------------------------------------
-------------------------------------------------------- ----------------
001 Hutt Jabba
896743856 jabba#thecompany.com
18
002 Simpson Homer
382947382 homer#thecompany.com
20
003 Kent Clark
082736194 superman#thecompany.com
5
004 Kid Billy
928743627 billythkid#thecompany.com
9
005 Stranger Perfect
389209831 nobody#thecompany.com
23
006 Zoidberg Dr
094510283 crustacean#thecompany.com
1
6 rows selected.
SQL>
set your column widths to fit in the screen
eg:
column EMAIL_ADDR format a30
where a is hte column width. you can use WRA to wrap the column
eg
column EMAIL_ADDR format a30 WRA
or TRU to truncate, WOR to break on word boundaries
for example:
SQL> select * from emp;
ID FIRST_NAME
---------- ------------------------------
LAST_NAME
------------------------------
EMAIL_ADDR
--------------------------------------------------
1 Dazza
Smith
d_dazzal#dazzal.com
so the output is a bit tricky to read as email_addr was padded to 300 characters (as my table had it defined as varchar2(300) which sql*plus uses to format the output).
first set an appropriate linesize:
SQL> set linesize 100
now lets set the columns so they fit on one line (linesize should be greater than the total col widths):
SQL> column email_addr format a30
SQL> column last_name format a20
SQL> column first_name format a20
SQL> select * from emp;
ID FIRST_NAME LAST_NAME EMAIL_ADDR
---------- -------------------- -------------------- ------------------------------
1 Dazza Smith d_dazzal#dazzal.com
so now the columns fit easily onto a reasonably sized terminal.
in your case first_name and last_name are varchar2(50)'s yet the data in them is much smaller, so i'd start with column first_name format a15 (same for last_name). with email, your column is varchar2(100) yet the max sized output was 25 chars, so put column email format a25 for a starter.
if you did that, you should get output (if linesize is high enough) like:
SQL> select * from xtern_empl_rpt ;
EMP LAST_NAME FIRST_NAME SSN EMAIL_ADDR YEARS_OF_SERVICE
--- --------------- -------------- --------- ------------------------- ----------------
001 Hutt Jabba 896743856 jabba#thecompany.com 18
finally as requested. WRA TRU and WOR. WRA is default by the way, so you dont have to use it but lets say we had:
SQL> select * from test;
A
--------------------------------------
THIS IS A SIMPLE WRAPPING TEST
but i wanted to format this as 10 characters width:
S
QL> col a format a10 WRA
SQL> select * from test;
A
----------
THIS IS A
SIMPLE WRA
PPING TEST
the WRA means just chop the string at 10 chars, regardless of whether we are in the middle of a word or not. if we wanted to break ONLY on word endings (where possible as a word > 10 still needs to break):
SQL> col a format a10 WOR
SQL> select * from test;
A
----------
THIS IS A
SIMPLE
WRAPPING
TEST
now the output is broken at word boundaries and not necessarily at 10 chars.
if we only wanted the first 10 chars and no line wrapping, we could use TRU:
SQL> col a format a10 TRU
SQL> select * from test;
A
----------
THIS IS A
This should fix your issue:
set wrap off
Try something like:
SET LINESIZE 120
(Adjust 120 to required maximum width.)
Before execute the select query, execute the following query to get the select query's output in CSV format. Then the output will be shown in CSV format.
set markup csv on;