How to combine two tables with different number of columns SQL - sql

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

Related

Removing duplicates in one column

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

SQL query with both table columns-Oracle

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);

How can I write a query to match groups of records

This is a puzzling SQL task. I'd like to do it with a query instead of stepping through with a cursor and doing it the "hard way".
If I have two tables TableA and TableB each with a grouping column as below:
TableA TableB
------------- -------------
id group id group
------ ------ ------ ------
1 D 1 X
2 D 2 X
3 D 3 Y
4 D 4 Y
4 E 5 Y
5 E 5 Z
5 F 6 Z
Note the group names are not the same name.
I want to know if a given TableB group is comprised entirely of IDs which are also grouped together in any group in TableA. The TableA group can have more IDs than the TableB group, so long as it has all of the same IDs as the TableB group. IDs can be in more than one group in either table.
From the tables above, I should find out that group X from TableB matches a group in TableA, but groups Y and Z do not.
I've tried many different queries, subqueries, recursive CTEs. I've only ended up with wrong results and headaches. The real dataset is significantly larger, so performance should be considered a factor too. Unfortunately, that means the cross-join solution proposed in an answer below won't work.
Is this even possible with SQL?
The idea for this query is to consider every group to every other group. Then count the number of times that the ids match. This involves a cross join to generate all the group pairs, then some joins and aggregation:
select b.group, a.group
from (select group, count(*) as cnt b group by group) bg cross join
(select distinct group a) ag left join
b
on b.group = bg.group left join
a
on a.group = ag.group and a.id = b.id
group by bg.group, ag.group, bg.cnt
having bg.cnt = count(a.id)
This query could give the answer.
SELECT B.[group]
FROM
TableB B
FULL JOIN TableA A ON B.id = A.id
GROUP BY
B.[group]
HAVING
COUNT(DISTINCT A.[group]) = 1

oracle sql row count affected by select clause

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.

SQL: Sort a table by the first relation to a second table

I have a Many-to-Many relationship between two tables. I'd like to sort the first table by the first relationship with the second table and only return a single result from that table. This is on SQL Server. I'd like something like this:
SELECT a.retrieve_me
FROM table_A AS a
JOIN table_B AS b ON a.foo = b.foo
JOIN table_C AS c ON b.bar = c.bar
ORDER BY c.sort_me
Unfortunately it returns MN(k) results, where M is the count of "table_A" and N(k) is the number of relations of a single row k with "table_C." To have it return just the results I wanted without post filtering I tried using DISTINCT on the SELECT clause and using TOP(SELECT COUNT(*) FROM table_A) but neither are valid syntax.
Any ideas? Hoping I can make this as performant as possible.
EDIT:
For clarity
table A
------------
"joe" 1
"betty" 2
"george" 3
table B
------------
1 2
1 3
2 3
2 4
3 1
table C
------------
1 "ashton"
2 "harding"
3 "spring"
4 "merry lane"
I'd like the results returned in the order of "george", "joe", and "betty" which is in the order (george -> ashton, joe -> harding, betty -> merry lane.)
If I understood correctly what you need, cause I think is very hard to follow you .. this should do it:
SELECT a.nm
FROM tablea a
cross apply (select top 1 *
from tableb b
join tablec c on b.id2 = c.id
where a.id = b.id1
order by c.nm) bc
order by bc.nm
http://sqlfiddle.com/#!3/661c0/5/0