I'm working in SQL and have the following table (named table1):
col1
col2
D
A
E
A
F
A
G
B
H
B
I
B
J
C
K
C
L
C
and I would like it to return:
col1
col2
D
A
G
B
J
C
I am looking for an SQL query that returns the first value in col1 for each distinct value in col2. Basically I am looking to only retrieve one value for each distinct value in col2 and remove any duplicates in col2. I am looking for a query that I would be able to apply to other tables where I might not know the positions of the distinct values. Thanks for in advance for any help!
Try the following:
SELECT MIN(col1) col1, col2 FROM table1
GROUP BY col2
Related
I'm trying to achieve what's in the example, the idea is to get data from table B if it exists and that with respec to the columns col1=col3 and col2=col4.
not sure wether this is possible or not using SQL
col1 col2
a b
c d
e f
Table B:
col3 col4
NULL x
y NULL
NULL NULL
Final_Table:
col1 col2
a x
y d
e f
You need to have a primary key to compare two or more tables. It is not possible if there is no primary key.
I have two tables A and B which look like this:
Table A
col1 col2 ID
--------------
a b 1
c d 2
Table B
col3 col4 ID
--------------
x t 1
y u 1
z o 2
m n 2
I want to create this new table:
Table C
col1 col2 col3 col4 ID
-------------------------
a b x t 1
a b y u 1
c d z o 2
c d m n 2
The ID's in A are not the unique ID's of those elements in that list. I just need to add a few more properties from their "parent" table(B) in this new table. I know there will be a lot of repetitions but I don't mind that for now.
I tried this following statement:
INSERT INTO C(SELECT A.*, B.col1, B.col2 from A LEFT OUTER JOIN B ON (B.ID = A.ID));
But it is resulting in something I did not expect. The row count of C must be the same as the row count of B. However when I used this SQL query, the resulting tables row count was even more than the total row count of A and B. I'm a beginner in using SQL I would appreciate any help.
A basic JOIN will do:
select
a.col1,
a.col2,
b.col3,
b.col4,
a.id
from tablea a
join tableb b on a.id = b.id
If I got you right your tables do not have any relation to each other.
So in this case you should be able to do something like this?
Select * from table1, table2
You can also combine this with an insert so you should get your new table
There are two tables named, for an example A and B.
A and B has a unique column named key_ref. and key_ref is the primary key of both tables
I need to find records in A but not in B. Therefore I wrote a query like,
SELECT a.*,b* from A a,B b WHERE key_ref NOT IN (SELECT key_ref from B)
The issue with this is, I do not get the empty columns of table B for the result. My result should include all the columns of A and B.
If I write my query like following my results get wrong. Is there any way , where I can achieve this even with a join condition.
SELECT a.* from A a WHERE key_ref NOT IN (SELECT key_ref from B)
Please refer following example.
Table A Table B
key ref col1 key ref col2
A aaa A aaa
B bbb B bbb
C ccc C ccc
D ddd
My answer should be,
key ref col1 col2
D ddd
If I understand your request:
You can use a LEFT OUTER JOIN operation so you'll get all rows in A not present in B (with condition in WHERE b.key_ref IS NULL)
Try this:
SELECT *
FROM a
LEFT OUTER JOIN b
ON a.key_ref = b.key_ref
WHERE b.key_ref IS NULL
Is this what you need:
select * from A
except
select * from B
?
If the second table tableB doesn't have a record associated with first table, then how would you display record from second table tableB which are not exists in the first table tableA.
So, the one way is to include NULL as columns ref. to tableB like that :
select a.*, null col2
from tableA a
where not exists (select 1 from tableB b where b.key_ref = a.key_ref);
While working on a oracle SQL , noticed that adding a column to the selected columns increases the total rows in the result. The query is using multiple subqueries declared using WITH. The join in the final query is a left join. Why is the row count getting impacted?
The only way I can think of to increase the number of result rows by adding a column to the SELECT clause is when using SELECT DISTINCT.
SELECT DISTINCT removes duplicates from the results, so
col1 col2
a b
a b
a c
a c
becomes
col1 col2
a b
a c
When adding a column
col1 col2 col3
a b d
a b e
a c f
a c f
becomes
col1 col2 col3
a b d
a b e
a c f
for instance, which is one row more than before.
I have Two column:
Col1 Col2
A D
B E
C F
I want in expression to join columns and get
Col
A
B
C
D
E
Any idea?
T1:
LOAD * INLINE [
Col1
A
B
C
];
T2:
LOAD * INLINE [
Col2
D
E
F
];
T:
Load
Col1 as Col
Resident T1;
Load
Col2 as Col
Resident T2;
drop tables T1,T2;
You don't explain that, but I'll assume that Col1 and Col2 are part of the same table, like this :
BASETABLE:
LOAD * INLINE [
Col1,Col2
A,D
B,E
C,F
];
You can then, using the fact that Qlikview assumes that the same column name is the same data to do this :
NEWTABLE:
LOAD Col1 as Col Resident BASETABLE;
LOAD Col2 as Col Resident BASETABLE;
The NEWTABLE will have a single column with data from Col1 and Col2.