How to Group specific Colomn in Oracle 9i - oracle

Please help me out to resolve my doubt,Am getting an output like this in oracle9i,
S.No Column1 Column2
---- ---------- -------
1 10/11/2011 Basic
2 10/11/2011 Basic
3 12/05/2012 Basic
4 12/05/2012 Basic
5 13/05/2012 Basic
But My real scenario is , i need to populate the output in below structure
S.No Column1 Column2
---- ---------- -------
1 10/11/2011 Basic
10/11/2011 Basic
2 12/05/2012 Basic
12/05/2012 Basic
3 13/05/2012 Basic
I dont know how to form the query , to retrieve the structure, please help me out, please anyone provide the solution for me..Thanks in advance

Here's one way of doing it.
SELECT CASE
WHEN r = 1
THEN w
ELSE NULL
END as s_no, column1, column2
FROM (SELECT column1, column2,
ROW_NUMBER () OVER (PARTITION BY column1 ORDER BY column2) AS r,
DENSE_RANK () OVER (ORDER BY column1) AS d
FROM SAMPLE);
Here ROW_NUMBER function returns unique number for each row within a group of column1.
DENSE_RANK function returns unique rank to to each group of column1.
Using this, you can chose to display the dense_rank column only for 1st row.
SQL Fiddle here

Related

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

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'.

Okay so I am trying to a rownum into a variable but I need it to give me only one value, so 2 if it's the second number in the row

select rownum into v_rownum
from waitlist
where p_callnum=callnum
order by sysdate;
tried doing this but gives too many values.
and if I do p_snum=snum, it will keep returning 1. I need it to return 2 if it's #2 on the waitlist.
select rn into v_rownum
from (select callnum,
row_number() over (order by sysdate) rn
from waitlist)
where p_snum=snum;
Almost got it to work. Running into issues in the first select. I believe I might have to use v_count instead. Also Ordering by Sysdate even if a second apart will order it correctly.
SNU CALLNUM TIME
--- ---------- ---------
101 10125 11-DEC-18
103 10125 11-DEC-18
BTW time is = date which I entered people into waitlist using sysdate. So I suppose ordering by time could work.
create table waitlist(
snum varchar2(3),
callnum number(8),
time date,
constraint fk_waitlist_snum foreign key(snum) references students(snum),
constraint fk_waitlist_callnum foreign key(callnum) references schclasses(callnum),
primary key(snum,callnum)
);
is the waitlist table.
I used Scott's DEPT table to create your WAITLIST; department numbers represent CALLNUM column:
SQL> select * From waitlist;
CALLNUM WAITER
---------- --------------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
How to fetch data you need?
using analytic function (ROW_NUMBER) which orders values by CALLNUMs, you'll know the order
that query will be used as an inline view for the main query that returns number in the waitlist for any CALLNUM
Here's how:
SQL> select rn
2 from (select callnum,
3 row_number() over (order by callnum) rn
4 from waitlist
5 )
6 where callnum = 30;
RN
----------
3
SQL>
rownum in oracle is a generated column, it does not refer to any specific row, it is just the nth row in a set.
With a select into it can only return one row (hence the two many rows error) so rownum will always be 1.
Without more details about your table structure, and how you are uniquely identifying records it is hard to give assist you further with a solution.

in oracle select query how to add a number column increment by 2 starting from 1

i need to write a oracle select query where i need a additional column which increment by 2 starting from 1.
Example:
column1 column2
amit 1
siva 3
pyll 5
here from oracle table i can get only column1. but in query i have to generate column2. So my issue is dynamically get a column like rownum() and increment it by 2. is there any way to get such result. in mysql we can use session variables inside a query. i expect a similar kind of solution in oracle. but i couldn't find a simple query to generate such numbers.
You know you have rownum available, but take a step back. You're starting with the contiguous sequence 1,2,3,4,5,6,... and you want to generate a sequence of odd numbers 1,3,5,7,9,11,.... So you need to figure out an algorithm that will convert one to the other.
If you say your starting number is n then you want to generate m where m=(2*n)-1.
You can use rownum (or row_number(), etc.) to generate your n values:
select column1, rownum as n
from your_table;
And you can then apply that algorithm:
select column1, (2*rownum)-1 as column2
from your_table;
COLUMN1 COLUMN2
------- ----------
amit 1
siva 3
pyll 5
jane 7
john 9
anna 11
...
With this simple approach the column2 values are not in the same order as the column1 values. You can either use row_number() or rank() instead, with a suitable order by clause; or use a subquery which does the ordering and apply rownum (and this algorithm) outside that:
select column1, (2*rownum)-1 as column2
from (
select column1
from your_name
order by column1
);
or some other variation, depending on the result you want to end up with.

Find completely non-distinct rows

I have an Oracle table I've compiled using an Informatica workflow. It's failing an integrity check because the following queries return a different number of rows:
SELECT DISTINCT * FROM table // 4,000 rows
SELECT * FROM table // 4,006 rows
The table consists of 17 fields, none of which are unique keys (obviously). How can I find the 6 duplicate rows?
For returning duplicate rows.
select * from
(SELECT cd.*,
ROW_NUMBER ()
OVER (PARTITION BY column1,column2...column2
ORDER BY column_names)
seq_no
FROM table cd)
where seq_no>1;
For example i have create one sample_table below for your better understanding.
create table sample_table
(
id1 number,
id2 number
)
i have inserted below data into table
ID1 ID2
1 2
1 2
1 2
2 3
2 3
2 3
In above data set we have 6 rows but only two rows are distinct.
By using below queries we can get distinct rows and non-distinct rows.
SELECT cd.*,
ROW_NUMBER ()
OVER (PARTITION BY id1
ORDER BY id1)
seq_no
FROM sample_table cd
after partition the table with the help of id1 we will get the below results
ID1 ID2 SEQ_NO
1 2 1
1 2 2
1 2 3
2 3 1
2 3 2
2 3 3
Then if you want to see the distinct rows use below query
select * from
(SELECT cd.*,
ROW_NUMBER ()
OVER (PARTITION BY id1
ORDER BY id1)
seq_no
FROM sample_table cd)
where seq_no=1;
if you want to see duplicate set use below query
select * from
(SELECT cd.*,
ROW_NUMBER ()
OVER (PARTITION BY id1
ORDER BY id1)
seq_no
FROM sample_table cd)
where seq_no>1;
A posibiliy is to use a analytical function to count the rows in the same group and I don't see how you can write the query without writing all the columns in some clause:
select *
from (
Select a.*, count(*) over (partition by column1, column2, ..., column17) as cnt
from your_table a
)
where cnt>1
This should get 12 rows, because 6 are duplicated.
A basic sql query would be:
select col1, col2, ..., col17
from table
group by col1, col2, ..., col17
having count(*) > 1;

I want to return data from duplicate rows SQL Query

I want to return data from duplicative rows
SELECT column1, column2 FROM table1
COLUMN1 COLUMN2
------- -------
CA 1
CB 2
CB 3
CC 4
CD 5
CE 6
CE 7
CE 8
CF 9
I want to return rows for 'CB' and CE. Here CB and CE has more than 1 row.
I'd code this as follows:
SELECT column1, column2 FROM table1 where column1 in ("CB", "CE")
Try this out - this query first finds out those items in column1 who appear multiple time and then extract their information.
select * from table1
where column1 in (
select column1
from table1
group by column1
having count(*) > 1
)
If you are only interested in knowing the values in column1, you could just run:
select column1
from table1
group by column1
having count(*) > 1
You Can try out this code. Basically the Query is in MySQL but you can use the same logic in Oracle Database. Here the inner subquery will find out the columns which is grouped by column1 and will return a column having a count greater than 1. The Outer query will display the rows of the column fetched by the inner query.Here I have created a table with the table name as name
SQL fiddle Added for your reference SQLCODE
select * from name where column1 in(select column1 from name
group by column1
having count(*)>1);

Resources