Oracle query by column1 where column2 is the same - oracle

I have a table like this in Oracle 9i DB:
+------+------+
| Col1 | Col2 |
+------+------+
| 1 | a |
| 2 | a |
| 3 | a |
| 4 | b |
| 5 | b |
+------+------+
Col1 is the primary key, Col2 is indexed.
I input col1 as condition for my query and I want to get col1 where col2 is the same as my input.
For example I query for 1 and the result should be 1,2,3.
I know I can use self join for this, I would like to know if there is a better way to do this.

I'd call this a semi-join: does it satisfy your 'no self joins' requirement?:
SELECT *
FROM YourTable
WHERE Col2 IN ( SELECT t2.Col2
FROM YourTable t2
WHERE t2.Col1 = 1 );
I'd be inclined to avoid the t2 range variable like this:
WITH YourTableSearched
AS ( SELECT Col2
FROM YourTable
WHERE Col1 = 1 )
SELECT *
FROM YourTable
WHERE Col2 IN ( SELECT Col2
FROM YourTableSearched );
but TNH I would probably do this:
WITH YourTableSearched
AS ( SELECT Col2
FROM YourTable
WHERE Col1 = 1 )
SELECT *
FROM YourTable
NATURAL JOIN YourTableSearched;

It's possible. Whether it's better (i.e. more performant) than using a self-join, particularly if there is an index on col1, col2, is anyone's guess.
Assuming col1 is unique, you could do:
SELECT col1
FROM (SELECT col1,
col2,
MAX(CASE WHEN col1 = :p_col1_value THEN col2 END) OVER () col2_comparison
FROM your_table)
WHERE col2 = col2_comparison;
And with :p_col1_value = 1:
COL1
----------
1
2
3
And with :p_col1_value = 5:
COL1
----------
4
5

Related

Reduce Rows to a map struct in hsql

New to hadoop/ hive and need to reduce a set of rows down into a map datatype as follows;
From
Col1
Col2
Jeff
Smith
Steve
Brown
To
Col1
Col2
1
{"Jeff":"Smith"}, { "Steve" : "Brown}
Is this work for you?
with myTable as (
select 'Jeff' as Col1, 'Smith' as Col2 union
select 'Steve' as Col1, 'Brown' as Col2
) -- test data
select str_to_map(concat_ws(",",(collect_list(concat_ws(":",Col1, Col2)))),",",":") as Col2
from myTable
;
+-----------------------------------+--+
| col2 |
+-----------------------------------+--+
| {"Jeff":"Smith","Steve":"Brown"} |
+-----------------------------------+--+

How to split two columns data in to two rows apart from using union? [duplicate]

I have 50 column in a table and it returns only one row and I want that one row with 50 column to be displayed in 50 rows and one column.
Can any one suggest me the Oracle query for it?
You can use UNPIVOT for one row like this to get only column with values
SELECT colvalue
FROM
(
SELECT *
FROM Table1
UNPIVOT INCLUDE NULLS
(
colvalue FOR cols IN (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, ... col50)
)
);
Sample output:
| COLVALUE |
------------
| 1 |
| 2 |
| (null) |
|..........|
If you need column with column names from your pivoted table just ditch the outer select
SELECT *
FROM Table1
UNPIVOT INCLUDE NULLS
(
colvalue FOR cols IN (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, ... col50)
);
Sample output:
| COLS | COLVALUE |
--------------------
| COL1 | 1 |
| COL2 | 2 |
| COL3 | (null) |
| ..... |......... |
Here is SQLFiddle demo
Be prepared for a lot of typing :) Oracle has UNPIVOT functionality but it wants at least two columns in the result, so it won't work for your situation.
First off, you'll need a counter from 1 to 50. You can query one like this:
SELECT LEVEL as Counter FROM DUAL CONNECT BY LEVEL <= 50
If you execute this query you'll get the numbers 1-50 as your result. With that as a basis, here's the full(ish) query:
WITH Cols AS (
SELECT LEVEL as Counter
FROM DUAL
CONNECT BY LEVEL <= 50
)
SELECT
CASE Cols.Counter
WHEN 1 THEN Col1
WHEN 2 THEN Col2
WHEN 3 THEN Col3
. . .
WHEN 50 THEN Col50
END AS myColumn
FROM myTable
CROSS JOIN Cols
ORDER BY Cols.Counter
Note that all of the columns must be the same data type, so if you have a mixture of character, number and date you'll need to convert them all to character.
Note that this query assumes one row in the table, as mentioned in the question. If there's more than one row you should end with ORDER BY a-column-that-identifies-the-row, Cols.Counter.

How can i make this INSERT in PL/SQL Procedure?

I am new in pl/sql programming and I need your help.
I would like to make a procedure.
To be more specific, I have tables like the folowing TABLE1
================================================
|COL1 | COL2 | COL3 | COL4 | COL5 | COL6 |COL7|
===============================================
|600 | 140 | 2 | 10 | 1300 | 500 | 1 |
|600 | 140 | 2 | 20 | 1400 | 340 | 4 |
|600 | 140 | 2 | 15 | 1400 | 230 | 3 |
|600 | 140 | 2 | 35 | 1700 | 120 | 2 |
|600 | 150 | 3 | 10 | 1300 | 166 | 6 |
|600 | 150 | 3 | 15 | 1400 | 435 | 5 |
----------------------------------------------
For the same COL1 and COL2/COL3 , check the select different values from COL4
For instance for COL1=600 , COL2=140/COL3=2 and COL2=150/COL3=3
Return 20 and 35
And insert in this table TABLE1 the rows
600 , 150 , 3, 20 , 1400 , 340, 7 (seq number)
600 , 150 , 3, 35 , 1700 , 120, 8 (seq number)
I make inserts in Table1 if P_FLG1 = 'Y'. IF P_FLG2 = 'Y' I make inserts also in TABLE3 etc
I am trying to make the procedure like below but I can't finish it
PROCEDURE COPY_COLUMNS ( P_COL1 IN A.COL1%TYPE,
P_FROM_COL2 IN B.COL2%TYPE,
P_FROM_COL3 IN B.COL3%TYPE,
P_TO_COL2 IN B.COL2%TYPE,
P_TO_COL3 IN B.COL3%TYPE,
P_FLG1 IN VARCHAR2,
P_FLG2 IN VARCHAR2,
P_FLG3 IN VARCHAR2
) IS
CURSOR CFL1 IS select COL4
FROM TABLE1
WHERE COL1 = P_COL1 AND COL2 = P_FROM_COL2 AND COL3 = P_FROM_COL3
MINUS
select COL4
FROM TABLE1
WHERE COL1 = P_COL1 AND COL2 = P_TO_COL2 AND COL3 = P_TO_COL3;
CURSOR CFL2 IS select COL4
FROM TABLE2
WHERE COL1 = P_COL1 AND COL2 = P_FROM_COL2 AND COL3 = P_FROM_COL3
MINUS
select COL4
FROM TABLE2
WHERE COL1 = P_COL1 AND COL2 = P_TO_COL2 AND COL3 = P_TO_COL3;
CURSOR CFL3 IS select COL4
FROM TABLE3
WHERE COL1 = P_COL1 AND COL2 = P_FROM_COL2 AND COL3 = P_FROM_COL3
MINUS
select COL4
FROM TABLE3
WHERE COL1 = P_COL1 AND COL2 = P_TO_COL2 AND COL3 = P_TO_COL3;
V_REC CFL1%ROWTYPE;
BEGIN
IF P_FLG1='N' OR P_FLG2='N' OR P_FLG3='N' OR P_FLG4 ='N' OR P_FLG5 = 'N' THEN
GOTO label; --do nothing
END IF;
IF P_FLG1 = 'Y' THEN
OPEN CFL1;
FETCH CFL1 INTO V_REC;
CLOSE C1;
SELECT COL5, COL6
FROM TABLE1
WHERE COL1 = P_COL1 AND COL2 = P_FROM_COL2 AND COL3 = P_FROM_COL3 AND COL4 = V_REC.COL4;
FOR REC IN CFL1 LOOP
INSERT INTO TABLE1
SELECT P_COL1, P_TO_COL2, P_TO_COL3, CFL1.COL4, -- COL5 , COL6 ?? -- , SEQ.NEXTVAL)
END LOOP;
END IF;
-- ..........
<<label>>
END;
I would appreciate it if you could help me.
Thanks a lot
CFL1.COL4 won't work, since you need to reference the resultset variable : REC.COL4.
And it doesn't hurt to specify the columns of the table you insert into.
for example :
INSERT INTO TABLE1 (col1, col2) values (rec.col1, rec.col2);
or
INSERT INTO TABLE1 (col1, col2) select rec.col1, rec.col2 from dual;
Also, you first fetch the CFL1, expecting only 1 record value.
But then use the same cursor in loop. Remove the fetch and get that col4 value from REC? A direct select in a procedure won't work anyway.

Select rows with same id without nulls and one row with Null if multiple nulls are present

I want to get only rows having a value and some other value than NULL for a particular username column.
If both rows have null for that particular username then it should show Null only once in output. If there are more than two rows for same username with null and some other value then display the value only not null.
Below is example sample and output. How it can be done using sql query?
Table:
Col1 | Col2
-------------------------
a | abc
a | bc
b | null
b | null
c | der
c | null
Output:
Col1 | Col2
-------------------------
a | abc
a | bc
b | null
c | der
Outlining the idea, there might be some syntax errors, don't have access to oracle.
SELECT * FROM
( SELECT DISTINCT USERNAME FROM <TABLE> ) USERS
LEFT OUTER JOIN
( SELECT USERNAME, COL2 FROM <TABLE> WHERE COL2 IS NOT NULL) USERS_COL2
ON
USRES.USERNAME = USERS_COL2.USERNAME
you use listagg () or stragg ()
drop table test;
create table test (
col1 varchar2(10),
col2 varchar2(10)
);
insert into test values ( 'a','abc');
insert into test values ( 'a','abc');
insert into test values ( 'b',null);
insert into test values ( 'b',null);
insert into test values ( 'c','der');
insert into test values ( 'c',null);
commit;
select col1,
listagg (col2,',') within group (order by col1) col2
from test
group by col1;
COL1 COL2
---------- -----------
a abc,abc
b
c der
select col1, stragg (col2)
from test
group by col1;
select col1, col2, count(*)
from omc.test
group by col1,col2;
you can remove count(*)

Oracle query to convert multiple column into one column

I have 50 column in a table and it returns only one row and I want that one row with 50 column to be displayed in 50 rows and one column.
Can any one suggest me the Oracle query for it?
You can use UNPIVOT for one row like this to get only column with values
SELECT colvalue
FROM
(
SELECT *
FROM Table1
UNPIVOT INCLUDE NULLS
(
colvalue FOR cols IN (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, ... col50)
)
);
Sample output:
| COLVALUE |
------------
| 1 |
| 2 |
| (null) |
|..........|
If you need column with column names from your pivoted table just ditch the outer select
SELECT *
FROM Table1
UNPIVOT INCLUDE NULLS
(
colvalue FOR cols IN (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, ... col50)
);
Sample output:
| COLS | COLVALUE |
--------------------
| COL1 | 1 |
| COL2 | 2 |
| COL3 | (null) |
| ..... |......... |
Here is SQLFiddle demo
Be prepared for a lot of typing :) Oracle has UNPIVOT functionality but it wants at least two columns in the result, so it won't work for your situation.
First off, you'll need a counter from 1 to 50. You can query one like this:
SELECT LEVEL as Counter FROM DUAL CONNECT BY LEVEL <= 50
If you execute this query you'll get the numbers 1-50 as your result. With that as a basis, here's the full(ish) query:
WITH Cols AS (
SELECT LEVEL as Counter
FROM DUAL
CONNECT BY LEVEL <= 50
)
SELECT
CASE Cols.Counter
WHEN 1 THEN Col1
WHEN 2 THEN Col2
WHEN 3 THEN Col3
. . .
WHEN 50 THEN Col50
END AS myColumn
FROM myTable
CROSS JOIN Cols
ORDER BY Cols.Counter
Note that all of the columns must be the same data type, so if you have a mixture of character, number and date you'll need to convert them all to character.
Note that this query assumes one row in the table, as mentioned in the question. If there's more than one row you should end with ORDER BY a-column-that-identifies-the-row, Cols.Counter.

Resources