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

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.

Related

How to get minimum number of records for a condition in oracle

I want to get a minimum number of record based on my condition I give in where condition.
for ex: I have a table with two columns(Id, Value)
My table has data like following:
Id Value
1 001
2 001
3 001
4 002
5 002
6 003
7 004
8 004
9 004
10 004
From the above table Value '001' has 3 Ids(1,2,3) and Value '002' has 2 and so on.
Now I want to identify the Value which has minimum Ids(like in this example, it should be Value '003' with only one Id).
How to write a query for this in Oracle.?
Thanks in advance.
The query below will select the value (or values) with the lowest row count. In the case of ties, all the values with the same, smallest number of rows are selected. The row count is not shown, but it could be shown easily (add it to the outer select).
The real work is done in the aggregate subquery. In addition to the value and the count of rows, we also compute the analytic function min(count(*)) - over the entire result of the aggregation, so the analytic clause is in fact empty: over ().
select value
from (
select value, count(*) as cnt, min(count(*)) over () as min_cnt
from your_table
group by value
)
where cnt = min_cnt
You can use a GROUP BY, order by the count and finally select the first row
SELECT * FROM (
SELECT value, count(*) as cnt
FROM sometable
GROUP BY value
ORDER BY count(*)
) WHERE ROWNUM = 1
sqlfiddle: http://sqlfiddle.com/#!4/e5f075/1
If you are on Oracle 12, you can do:
select value, count(*)
from mytable
group by value
order by 2
fetch first 1 rows only
If you need to have all values that share the minimum count, then replace only with with ties.

How to write a query to select only groups where certain values are present in all rows?

I need to write a query for Oracle 11g to select only the groups which are highlighted in the image here. Specifically, there are 3 columns -- A, B, and C. Group 1 contains a null value in column C, group 2 contains several nulls in column C.Group 3 does not contain any null values in column C. Group 4 yes contains some null values in column C, and Group 5 does NOT contain any null values in column C.
I need to selects only the groups of rows that do NOT contain any null values in column C -- that would be group 3 rows and group 5 rows. I only need the group number, like return only a 3 and a 5 What SQL functions can I use to write this query? What does this query look like?
something like (pseudo code)
select colA, count(*) As cnt From tblX join to itself on something
Where ...something ...
Group By colA
(or we could skip the grouping and just select 3 and 5 or the three 3's and the three 5's)
The aggregate function COUNT(colname) would only count non-NULL values, so something like this might work:
select A from yourtable
group by A
having count(A) = count(C)
assuming A is never NULL.
You can use a not in clause and subquery
select colA, count(*) As cnt
From tblX
Where colA not in (select distinct colA from tblx where colC is null)
Group By colA

How to order by case insensitive ASC or DESC, with DISTINCT and UNION

How to order by case insensitive ASC or DESC for P/L sql 11g. this p/l sql basic question but i can't find good answer in Google please tell how to sort the select result case insensitive
this what i tried
SELECT DISTINCT
asssss,
saas_acc
FROM DUAL
UNION SELECT '--ALL--','ALL' FROM DUAL
ORDER BY upper(asssss) ASC ;
that gave to me ORA-01785: ORDER BY item must be the number of a SELECT-list expression
The simplest option would be to sort by the upper- (or lower-) case column data
ORDER BY UPPER( column_name )
DISTINCT actually filtered the UNIQUE content in the result set, with whatever expressions given in the SELECT clause.
We cannot order it using a Different expression or column name. Please see the example here.
SQL> l
1 SELECT DISTINCT (col1),(col2)
2 FROM
3 ( SELECT 'Hello' col1,'World' col2 FROM DUAL
4 UNION ALL
5 SELECT 'HELLO','WORLD' FROM DUAL
6* )
SQL> /
COL1 COL2
----- -----
HELLO WORLD
Hello World
You can see that DISTINCT is CASE SENSITIVE here.(2 rows displayed)
So, let me Do a UPPER() on both columns.
SQL> l
1 SELECT DISTINCT UPPER (col1),UPPER(col2)
2 FROM
3 ( SELECT 'Hello' col1,'World' col2 FROM DUAL
4 UNION ALL
5 SELECT 'HELLO','WORLD' FROM DUAL
6* )
SQL> /
UPPER UPPER
----- -----
HELLO WORLD
Just 1 row is Displayed, ignoring the case.
Coming back to the actual problem. To order something on a DISTINCT Resultset, it has to be a part of DISTINCT clause's expression/column.
So, When you issue DISTINCT COL1,COl2, the order by may be by COL1 or COL2/.. it cannot be COL3 or even UPPER(COL1) because UPPER() makes a different expression conflicting the expression over DISTINCT.
Finally, Answer for your Question would be
if you want your ORDER to be case-insensitive, DISTINCT also has to the same way! As given below
SELECT DISTINCT
UPPER(asssss),
saas_acc
FROM DUAL
ORDER BY upper(asssss) ASC ;
OR if UNION has to be used, better do this, or same as above one.
SELECT * FROM
(
SELECT DISTINCT asssss as asssss,
saas_acc
FROM DUAL
UNION
SELECT '--ALL--','ALL' FROM DUAL
)
ORDER BY upper(asssss) ASC ;
Out of my own Experience, I had always felt, what ever expression/column is specified in the ORDER BY, it is implicitly taken to final SELECT as well. Ordering is just based on the column number(position) in the result actually . In this situation, DISTINCT COL1,COl2 is already there. When you give ORDER BY UPPER(COL1), it will be tried to append into the SELECT expression, which is NOT possible at all. So, Semantic check itself, would disqualify this query with an Error!
To sort case insensitive you need to set the NLS_COMP to ANSI
NLS_COMP=ANSI
Details: http://www.orafaq.com/node/999
You can use upper or lower functions.
order by upper(columnName)
Update1
Try removing order-by clause from your query which will give you correct error, which is ORA-00904: "SAAS_ACC": invalid identifier. So you can search on google for this error or ask another question on SO.
Also have a look at how to use order by in union.

How to Group specific Colomn in Oracle 9i

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

How to use the union operator in oracle

I know that the union operator is used to (for example) return all rows from two tables after eliminating duplicates. Example:
SELECT a_id
FROM a
UNION
SELECT b_id
FROM b;
The result of listing of all elements in A and B eliminating duplicates is {1,2,3,4,5,6,7,8}.
If you joined A and B you would get only {4,5}. You would have to perform a full outer join to get the full list of 1-8. My question is if I wanted to use the union operator to display from a table called employees, the employee_id and job_id ( employee id being a number data type, and job_id being a VARCHAR2 data type) How would I go about doing this?
Would it be something like this: This does not run in oracle obviously,
SELECT employee_id
UNION
SELECT job_id
FROM employees;
If you really wanted to union together all the EMPLOYEE_IDs followed by all the JOB_IDs you'd use
SELECT TO_CHAR(EMPLOYEE_ID) FROM EMPLOYEES
UNION ALL
SELECT JOB_ID FROM EMPLOYEES
If you had rows with EMPLOYEE_IDs of 1, 2, and 3, and those same rows had JOB_IDs of 1, 11, and 111 you'd get a result set of six rows with a single column which would have values of
1
2
3
1
11
111
By using UNION ALL Oracle will allow the duplicates to pass through.
Share and enjoy.

Resources