a sql query from oracle - sql

I am using oracle 10g EE database.I have one table mytable and has two columns and data is as follows:
Note: I want to find out data based on same value in 2nd column only, it does not matter whether there exists same or different value in first column.
10 is repeated 3 times for A, B and C and these 3 are required output
similarly 20 is repeated 2 times for C and D and these are also required output
column1 column2
-------------- ---------------
A 10 //required
A 10 //required
B 10 //required
C 20//required
D 20//required
E 30--------not required as 30 is only here and not duplicated
F 40--------not required as 40 is only here and not duplicated
following output is required i.e. same value in 2nd column having same or different values in 1st column
column1 column2
-------------- ---------------
A 10
A 10
B 10
C 20
D 20

SELECT column1,
column2
FROM <table> t1
WHERE column2 IN (SELECT column2
FROM <table> t2
GROUP BY column2
HAVING count(*) > 1);

It sounds like you want
SELECT *
FROM table_name t1
WHERE column2 IN( SELECT column2
FROM table_name t2
GROUP BY column2
HAVING COUNT(*) > 1 )
This appears to work with your sample data
SQL> with table_name as (
2 select 'A' column1, 10 column2 from dual union all
3 select 'A', 10 from dual union all
4 select 'B', 10 from dual union all
5 select 'C', 20 from dual union all
6 select 'D', 30 from dual)
7 SELECT *
8 FROM table_name t1
9 WHERE column2 IN( SELECT column2
10 FROM table_name t2
11 GROUP BY column2
12 HAVING COUNT(*) > 1 );
C COLUMN2
- ----------
B 10
A 10
A 10

select * from table where column2 in ( select column2 from table group by coulmn2 having count(*)>1);
should work for you.
Thanks
Abhi

Related

How can I select a data from another column from rows that have been selected?

I tried my best to figure and google this out, but couldn't really find a solid answer to it.
The problem I'm facing is that
Table 1:
ID Value 1
1 a
2 b
3 c
Table 2:
ID Value 2
1 4a
3 5b
4 6c
and I'd basically have to select the value from Table 1 that doesn't exist on Table 2 (Thus, 'b')
I can select and identify the ID that I want by using minus function between the tables, but can't seem to figure out a way to call a query to instead call the data.
Use the MINUS as a subquery (i.e. an inline view) (lines #14 - 16):
Sample data:
SQL> with
2 table1(id, value1) as
3 (select 1, 'a' from dual union all
4 select 2, 'b' from dual union all
5 select 3, 'c' from dual
6 ),
7 table2 (id, value2) as
8 (select 1, '4a' from dual union all
9 select 3, '5b' from dual union all
10 select 4, '6c' from dual
11 )
Query begins here:
12 select a.*
13 from table1 a
14 where a.id in (select t1.id from table1 t1
15 minus
16 select t2.id from table2 t2
17 );
ID VALUE1
---------- ----------
2 b
SQL>
Alternatively, use not exists:
<snip>
12 select a.*
13 from table1 a
14 where not exists (select null
15 from table2 b
16 where b.id = a.id
17 );
ID VALUE1
---------- ----------
2 b
SQL>

How to cross join with out using a table?

Recently, I am trying to create a table. I have a column that contains 'a', 'b', 'c' and would like to cross join it with 1,2,3 in to the table below.
However, I don't have a table that contains values 1,2,3 and need to do it without creating a table.
Can I achieve this without creating any table? Thanks a lot!
Col1
a
b
c
Col1 Col2
a 1
b 1
c 1
a 2
b 2
c 2
a 3
b 3
c 3
Use a CTE instead:
SQL> with
2 a (col) as
3 (select 'a' from dual union all
4 select 'b' from dual union all
5 select 'c' from dual
6 ),
7 b (col) as
8 (select 1 from dual union all
9 select 2 from dual union all
10 select 3 from dual
11 )
12 select a.col, b.col
13 from a cross join b;
C COL
- ----------
a 1
a 2
a 3
b 1
b 2
b 3
c 1
c 2
c 3
9 rows selected.
SQL>
You can use:
SELECT *
FROM table1
CROSS JOIN (SELECT LEVEL AS col2 FROM DUAL CONNECT BY LEVEL <= 3);
or
WITH data (col1, col2) AS (
SELECT col1, 1 FROM table1
UNION ALL
SELECT col1, col2 + 1 FROM data WHERE col2 < 3
)
SELECT * FROM data;
Which, given your sample data:
CREATE TABLE table1 (col1) AS
SELECT 'a' FROM DUAL UNION ALL
SELECT 'b' FROM DUAL UNION ALL
SELECT 'c' FROM DUAL;
Both output:
COL1
COL2
a
1
b
1
c
1
a
2
b
2
c
2
a
3
b
3
c
3
db<>fiddle here
You can create "tables" within your query in many different ways, several of which have been illustrated in other answers already.
For your request I like an XML solution as shown below:
create table table1 (col1 varchar2(1));
insert into table1(col1) values('a');
insert into table1(col1) values('b');
insert into table1(col1) values('c');
commit;
select t1.col1, xmlcast(column_value as number) as col2
from table1 t1 cross join xmltable('1 to 3')
;
COL1 COL2
---- ----
a 1
a 2
a 3
b 1
b 2
b 3
c 1
c 2
c 3

How to use subquery to drop rows from Tab1 which are in Tab2 in Oracle SQL?

I have tables in Oracle SQL like below:
Tab1
ID
-----
1
2
3
Tab2
ID
-----
3
4
5
And I need to take values from Tab1 which are not in Tab2. I made query like below:
select ID
from Tab1
where ID not in (select ID from Tab2)
Above query does not work, how can I change it to achieve result as I need:
ID
---
1
2
I can add that I prefere to use subquery in this problem, how can I do that in Oracle SQL ?
With the MINUS set operator:
SQL> with
2 tab1 (id) as
3 (select 1 from dual union all
4 select 2 from dual union all
5 select 3 from dual
6 ),
7 tab2 (id) as
8 (select 3 from dual union all
9 select 4 from dual union all
10 select 5 from dual
11 )
12 select id from tab1
13 minus
14 select id from tab2;
ID
----------
1
2
SQL>
BTW, query you used (with a subquery) returns correct result; did you mean to say that you prefer NOT to use a subquery?
<snip>
12 select id from tab1
13 where id not in (select id from tab2);
ID
----------
1
2
I tried this code and it worked fine :
select ID
from Table1
where ID not in (select ID from Table2)
You cant DROP rows from a table, but you can DELETE them.
So correcting you title to
How to use subquery to DELETE rows from Tab1 which are in Tab2 in Oracle SQL?
do so:
delete from tab1
where id in (select id from tab2);
1 row deleted.
select * from tab1;
ID
----------
1
2
Do not forget to commit to make the change permanent.

Take values of different columns in the same 1 column table

I am working on Ms Sql Server 2008 R2.
now i want one column which has results of 3 different columns of same table..
Let me explain with Figure.
Table: mainTable
Id Column1 Column2 Column3
-------------------------------------
1 urla urlb urlc
2 urld urle urlf
3 urlg urlh urli
Now i want one column
Table Name: ResultTable
Id ColumnResult
-------------
1 urla
2 urlb
3 urlc
4 urld
5 urle
6 urlf
7 urlg
8 urlh
9 urli
Thanks in Advance.
Sahil Patel
You can use UNION ALL:
SELECT
Id = ROW_NUMBER() OVER(ORDER BY ColumnResult),
Column1
FROM (
SELECT Id, Column1 AS ColumnResult FROM mainTable UNION ALL
SELECT Id, Column2 FROM mainTable UNION ALL
SELECT Id, Column3 FROM mainTable
)t

How to get the records from multiple tables?

Hi I am new to the Database, and i am trying to get the records from the multiple tables, but depending upon there selection following is my tables
Table1
Column1 Column2
1 10
2 25
3 23
4 15
5 7
Table2
Column1 Column2
2 15
3 13
5 17
Table3
Column1 Column2
2 45
Resultant Table should have records like
Column1 Column2
1 10
2 45
3 13
4 15
5 17
i am trying but not got the output yet. Any help or the direction to work out this output will be great help.
UPDATE
What i want is get the all rows from table1 then if table2 contains the matching records then it will remove the matching records form the resultset and add the table2 matching records and then same is repeated by table3.
SELECT t1.column1, COALESCE(t3.column2,t2.column2,t1.column2)
FROM t1
LEFT JOIN t2 on t1.column1=t2.column1
LEFT JOIN t3 on t1.column1=t3.column1
Please use the Below Code and Try
select * from table1 where column1 not in ( select column1 from table2 union select column1 from table3)
union
select * from table2 where column1 not in (select column1 from table3)
union
select * from table3
select x.col1,max(x.col2) from (
select * from #t1
union
select * from #t2
union
select * from #t3
)x
group by x.col1
see it in action