It is possible insert dummy (dash) into first row data use select statement in oracle SQL? - oracle

table_A
col_color col_name col_qty
- - - <----- dummy dash
RED APPLE 2
YEL BANANA 1
GRN GREEN_APPLE 3
Hi, it is posible to insert first row of dummy dash for viewing not store into database
use oracle sql plus ?
Anyone help is much apprecited.

One option is to UNION two data sets; one contains dummy dashes, while another contains "real" data. Note that dashes are considered to be strings, which means that you'll have to cast other datatypes to character datatype (see to_char(deptno) in my example):
SQL> with temp as
2 (select 1 rn, '-' deptno , '-' dname, '-' loc from dual
3 union all
4 select 2 rn, to_char(deptno), dname , loc from dept
5 )
6 select deptno, dname, loc
7 from temp
8 order by rn, deptno;
DEPTNO DNAME LOC
---------- -------------- -------------
- - -
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
The rn column is used to correctly sort the output (dashes first, the rest of data next).

If you don't want to use 'with', then how about this?
(
SELECT '-' COL_COLOR
, '-' COL_NAME
, '-' COL_QTY
FROM DUAL
)
UNION ALL
(
SELECT *
FROM table_A
)
I think this way is the best way not using 'with'.

Related

Oracle Rest Data Service Order by desc nulls last not working

I am not able order by field_name DESC with null last option. Since the default behaviour on DESC is nulls first, I would like them to be pushed at last.
"$orderby": {"ENAME":"DESC NULLS LAST"}
Thanks in advance
How about such a trick? If ENAME is NULL, use CHR(ASCII(1E4)) - something that is, hopefully, far enough to avoid valid first characters in actual names in your database.
SQL> with test (id, ename) as
2 (select 1, 'Little' from dual union
3 select 2, null from dual union
4 select 3, 'Zoot' from dual
5 )
6 select *
7 from test
8 order by decode(ename, null, chr(ascii(1E4)), ename) desc;
ID ENAME
---------- ------
2
3 Zoot
1 Little
SQL>

Convert single row result into two column result. Unpivot error 'expression must have same datatype as corresponding expression'

Please read only EDIT section which is more relevant now.
I have table in oracle 12c database, say table_1.
I have to run a simple SQL which returns only 1 row and all columns from from table_1. Say like
select * from table_1 where col_1 = 65
But the result I want is like:
I did some search and found this link which is very similar to what i want.
But I was only able to get the first column in the expected result by folowing it. I am unable to get actual data in the second column of the expected result.
So far I was able to write only:
select v
from (
select 'col_1' as col_1, 'col_2' as col_2, 'col_3' as col_3, 'col_4' as col_4 from dual
) t
unpivot
(
v for val in (col_1,col_2,col_3,col_4)
) u;
How can I add the second column and the condition where col_1 = 65?
EDIT: (Above part is less relevant now)
I fount this link where there us unpivot example which I could use.
select * from olympic_medal_tables
Gives:
desc olympic_medal_tables
Name Null? Type
------------- ----- -----------
NOC VARCHAR2(3)
GOLD_MEDALS NUMBER
SILVER_MEDALS NUMBER
BRONZE_MEDALS NUMBER
Following SQL gives me what I could use, but I don't want the NOC column in the result:
select * from olympic_medal_tables
unpivot (medal_count for medal_colour in (
gold_medals as 'GOLD',
silver_medals as 'SILVER',
bronze_medals as 'BRONZE'
));
Result:
So when I add NOC as well (so it is not added as a column in the result) like:
select * from olympic_medal_tables
unpivot (medal_count for medal_colour in (
gold_medals as 'GOLD',
silver_medals as 'SILVER',
bronze_medals as 'BRONZE',
noc as 'NOC'
));
I get error: ORA-01790: expression must have same datatype as corresponding expression on line 6 Error at Line: 44 Column: 3
I have tried using TO_NUMBER and TO_CHAR as well but then I get various syntax errors.
Question: How can I get expected result with just 2 columns. Is it possible with this approach using unpivot?
Here's one option, which requires you to first select one row, and then apply UNPIVOT to it:
SQL> with test (col1, col2, col3, col4) as
2 (select '12', 'hfkds' , 'hk435k' , '32' from dual union
3 select '34', 'ldkfgj', 'fsjd4653', '324' from dual union
4 select '65', 'ifd' , 'dkfjs345', '23' from dual union
5 select '87', 'dg' , '345jh' , '65' from dual
6 ),
7 one_row as
8 (select * From test
9 where col1 = '65'
10 )
11 select *
12 from one_row
13 unpivot (col_value for col_name in
14 (col1 as 'col1', col2 as 'col2', col3 as 'col3', col4 as 'col4'));
COL_ COL_VALU
---- --------
col1 65
col2 ifd
col3 dkfjs345
col4 23
SQL>

not a single-group group function when using max

I am trying to get out the max avrg using query below but I am getting wrror saying
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action: Error at Line: 1 Column:
SELECT B.STUDENT_ID,
A.FRIST_NAME,
A.FATHER_NAME,
A.LAST_NAME,
SUM (B.GRADE) AS SUM_GRADE,
COUNT(B.COURSE_ID) AS COURSE_COUNT,
max(SUM(B.GRADE) / COUNT(B.COURSE_ID)) AS AVRG
FROM STUDENT A,
STUDENT_COURSE B
WHERE A.STUDENT_ID = B.STUDENT_ID
GROUP BY A.FRIST_NAME, A.FATHER_NAME, A.LAST_NAME, B.STUDENT_ID;
this error gone when I remove the max function any one can help me why ?
I tried to use having maxbut I am getting error that says invalid renational
any way to use having with this query ?
One option is to use your current query (without MAX) as an inline view, and apply the MAX function to "sum/count":
SELECT student_id,
first_name,
father_name,
last_name,
sum_grade,
course_count,
-- this:
MAX(avrg) max_avrt
FROM (-- your current query
SELECT b.student_id,
a.frist_name,
a.father_name,
a.last_name,
SUM(b.grade) AS sum_grade,
COUNT(b.course_id) AS course_count,
SUM(b.grade) / COUNT(b.course_id) AS avrg
FROM student a,
student_course b
WHERE a.student_id = b.student_id
GROUP BY a.frist_name,
a.father_name,
a.last_name,
b.student_id
)
GROUP BY student_id, first_name, father_name, last_name, sum_grade, course_count;
However, you won't achieve anything good, as you'd still get the same record set due to outer GROUP BY clause. Consider using SUM in its analytic form.
Here's a simple example which shows what I mean, based on Scott's schema.
This is what you have now:
SQL> select deptno, sum(sal) / count(*) ssc
2 from emp
3 group by deptno
4 order by deptno;
DEPTNO SSC
---------- ----------
10 2916,66667
20 2258,33333
30 1566,66667
Apparently, you'd like to select the first SSC value (2916). If you apply what I wrote earlier (i.e. use that query as an inline view), you'd get this:
SQL> select deptno, max(ssc) max_ssc
2 from (select deptno, sum(sal) / count(*) ssc
3 from emp
4 group by deptno
5 )
6 group by deptno
7 order by deptno;
DEPTNO MAX_SSC
---------- ----------
10 2916,66667
20 2258,33333
30 1566,66667
SQL>
No improvement, eh? So, analytical function might be what you need:
SQL> select deptno,
2 max(sum(sal) / count(*)) over (order by deptno) max_ssc
3 from emp
4 group by deptno
5 order by deptno;
DEPTNO MAX_SSC
---------- ----------
10 2916,66667
20 2916,66667
30 2916,66667
This does return desired MAX value (if that's what you're looking for. If not, explain what you'd want to get as a result).

Pl/Sql Approach to write complex procedure Best Practices

I am relatively new to Pl Sql and have to write a procedure which does following, It has got 6-7 different queries which are as follows :
Select manager, count(*) bank from abc ..........
select manager , count(*) retail from abc .......
Now each count is for a different department , while manager may be same . So problem is that I am looking for a best approach which helps me to store
the data in separate table which has following structure:
Manager : Count Bank : Count Retail : Count xyz ...........
How do I make sure using Pl sql that each manager has right count for all the columns(bank/retai/xyz) , the first thing is how to store multiple different query result and process it . I think we can use cursor but I need to research on it , also this procedure should be high performance .
Please suggest.
Thanks
XslGuy
This is easily achievable with conditional sums. Hopefully the below will give you enough of an idea of what you need to do:
with sample_data as (select 1 id, 1 dept, 10 val from dual union all
select 2 id, 1 dept, 20 val from dual union all
select 3 id, 2 dept, 30 val from dual union all
select 4 id, 3 dept, 40 val from dual union all
select 5 id, 1 dept, 50 val from dual union all
select 6 id, 3 dept, 60 val from dual union all
select 7 id, 2 dept, 70 val from dual union all
select 8 id, 4 dept, 80 val from dual)
-- end of creating a subquery that contains some sample data. See sql below:
select sum(case when dept = 1 then val end) dept_1_total,
sum(case when dept = 2 then val end) dept_2_total,
sum(case when dept = 3 then val end) dept_3_total
from sample_data
where dept in (1, 2, 3);
DEPT_1_TOTAL DEPT_2_TOTAL DEPT_3_TOTAL
------------ ------------ ------------
80 100 100

Is this a right query? If it is what does it mean

I was given a query to explain. Could someone please explain it to me:
select j.ip_num from
jobs j, address a
where j.jobtype='C' and
a.sel_code(+)='H' and
j.ip_num=a.ip_num and
a.ip_num is null order by a.ip_num
That query selects every JOB.IP_NUM which doesn't have a matching ADDRESS record or where the matching ADDRESS record has a SEL_CODE not equal to 'H'.
The (+) is Oracle's old outer join syntax. It is the only OUTER JOIN syntax supported in versions of Oracle before 9i.
In this query we get one row for every row in EMP which matches a department, plus a row for the DEPTNO=40, which has no employees:
SQL> select d.dname
2 , e.ename
3 from dept d
4 , emp e
5 where d.deptno = e.deptno(+)
6 /
DNAME ENAME
-------------- ----------
ACCOUNTING SCHNEIDER
ACCOUNTING BOEHMER
ACCOUNTING KISHORE
RESEARCH ROBERTSON
RESEARCH KULASH
RESEARCH GASPAROTTO
RESEARCH RIGBY
RESEARCH CLARKE
SALES HALL
SALES CAVE
SALES SPENCER
SALES BILLINGTON
SALES PADFIELD
SALES VAN WIJK
SALES KESTELYN
SALES LIRA
OPERATIONS PSMITH
HOUSEKEEPING VERREYNNE
HOUSEKEEPING FEUERSTEIN
HOUSEKEEPING PODER
HOUSEKEEPING TRICHLER
COMMUNICATIONS
22 rows selected.
SQL>
Now, if we put an additional filter on the EMP table like this, we simply get one record for each Department, because only one record in EMP now matches:
SQL> select d.dname
2 , e.ename
3 from dept d
4 , emp e
5 where d.deptno = e.deptno(+)
6 and e.ename(+) = 'CAVE'
7 /
DNAME ENAME
-------------- ----------
ACCOUNTING
RESEARCH
SALES CAVE
OPERATIONS
HOUSEKEEPING
COMMUNICATIONS
6 rows selected.
SQL>
/
To convert this query into the ANSI SQL syntax we have to do this:
SQL> select d.dname
2 , e.ename
3 from dept d
4 left outer join emp e
5 on ( d.deptno = e.deptno
6 and e.ename = 'CAVE' )
7 /
DNAME ENAME
-------------- ----------
ACCOUNTING
RESEARCH
SALES CAVE
OPERATIONS
HOUSEKEEPING
COMMUNICATIONS
6 rows selected.
SQL>
Note that if we don't include the additonal clause in the JOIN but leave it in the WHERE clause we get a different result:
SQL> select d.dname
2 , e.ename
3 from dept d
4 left outer join emp e
5 on ( d.deptno = e.deptno )
6 where e.ename = 'CAVE'
7 /
DNAME ENAME
-------------- ----------
SALES CAVE
SQL>
This is the equivalent of omitting the (+) in the second old skool query.
The query is joining the 2 tables jobs, and address. These tables are joining on the field ip_num but you are looking for the records that exist in the jobs table but do not exist in the address table.
This is a LEFT OUTER JOIN. This query could also be written
SELECT j.ip_num
FROM jobs j
LEFT OUTER JOIN address a
ON j.ip_num=a.ip_num
WHERE j.jobtype='C' AND
a.sel_code(+)='H' AND
a.ip_num is null
ORDER BY a.ip_num
It might be useful to see a visual picture joins http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Resources