select query on two tables with no unique keys - sql

I have two tables.
Table1
col1 col2 col3
100 A 1000
100 A 1000
100 A 1002
100 B 1003
100 C 1004
Table2
col1 col2 colC
100 A 1X
100 A 2X
100 A 3X
100 B 4X
100 C 5X
In the above table colC value is unique.
i want my ouptput to be like this, where colC values to be unique here also.
col1 col2 col3 colC
100 A 1000 1X
100 A 1000 2X
100 A 1002 3X
100 B 1003 4X
100 C 1004 5X
I have to use col1 and col2 as the key for the join.
Is that possible to do that. i got duplicates comming in for the first two records, when i tried with inner and left outer joins. TIA

Something like this?
select
a.col1,
a.col2,
a.col3,
b.colC
from (
select
row_number() over (partition by col1, col2 order by 1) r,
col1,
col2
from
table1
) a, (
select
row_number() over (partition by col1, col2 order by 1) r,
col1,
col2
from
table2
) b
where a.r = b.r and
a.col1 = b.col1 and
a.col2 = b.col2;

SELECT t1.col1, t1.col2, t1.col3, t2.colC FROM Table1 t1
JOIN Table2 t2
ON t1.col1 = t2.col1
AND t1.col2 = t2.col2
Is this not right?
EDIT: You say you get duplicates, but this will happen as the 2 columns you specify as keys, are not actually keys. There are other rows with the same values. So if the combination is meant to be unique there is either a fault with the data or your requirements need to be looked into again.

Related

how to extract the rows where a group appears more than a certain number of times

I have the following table
col1 col2 col3 key
A B C 1
A B B 2
A B B 3
A B D 4
B D C 5
I would like to extract the rows where the group col1, col2, col3 appears more than once in the table.
A B B 2
A B B 3
So far, I have:
SELECT col1, col2, col3, count(*)
FROM db.table
GROUP BY col1, col2, col3
HAVING count(*) > 1
col1 col2 col3 count(*)
A B B 2
Is there a way to extract those rows with A B B without having to join the final table with the initial table?
You could use exists logic:
SELECT col1, col2, col3, "key"
FROM yourTable t1
WHERE EXISTS (SELECT 1 FROM yourTable t2
WHERE t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND
t2.col3 = t1.col3 AND
t2."key" <> t1."key");
Try below query with CTE
with MyCTE
as
(
select col1,col2,col3,Key,COUNT(*) over(PARTITION BY col1,col2,col3 order
by col1,col2,col3) as Duplicate from yourtable
)
select col1,col2,col3,key from MyCTE where Duplicate>1

Compare values in Different column and row

I have the following table:
ID COl1 COl2
1 13 15
2 13 16
3 13 17
4 17 13
What I need is to select all rows where Col1 value is available in Col2 and vice versa.
This case only ROW 4 or ROW 3 should be returned. They have same values (13 17).
Take it as col1 is Buyer and col2 is Seller
I want to know who are the users who bought / sell from EACH OTHER.
if user a bought from user b, user b should buy from user a in order to be returned.
SELECT
a.*
FROM
yourTable a
INNER JOIN
yourTable b
ON a.Col1 = b.Col2
AND a.Col2 = b.Col1
AND a.id != b.id
This can be done by using sub queries:
SELECT ID, COl1, COl2
FROM table1 WHERE COl1 IN (SELECT DISTINCT COl2 FROM table1)
UNION
SELECT ID, COl1, COl2
FROM table1 WHERE COl2 IN (SELECT DISTINCT COl1 FROM table1)
This sounds like exists:
select t.*
from t
where exists (select 1 from t t2 where t2.col1 = t.col2) and
exists (select 1 from t t2 where t2.col2 = t.col1) ;
If you want them in the same row, I would still use exists:
select t.*
from t
where exists (select 1 from t t2 where t2.col1 = t.col2 AND t2.col2 = t.col1) ;
I recommend this over a self-join because it will not generate multiple rows if there are multiple examples of the buyers and sellers on either side.
This also works
SELECT * FROM your_table WHERE
col1 IN (SELECT col2 FROM your_table)
AND
col2 IN (SELECT col1 FROM your_table);

Select distinct values one column into multiple columns

I have the following data: column 1 with many category and column 2 with values for each category. I need to convert or pivot this information to show each value for category group across multiple columns.
col1 col2
----------------
1 a
2 b
2 c
2 d
3 e
3 f
4 g
4 h
And need this result:
col1 col2 col3 col4 col5 col6
-----------------------------------------------
1 a
2 b c d
3 e f
4 g h
There are no more than seven values per tb1 count(column 2) group(column 1). All values from tb1 column 2 are different and about + 50 records.
You want to pivot your table, but your table doesn't currently contain the field that you want to pivot on ("col1", "col2", "col3", etc...). You need a row number, partitioned by col1. The Jet database does not provide a ROW_NUMBER function, so you have to fake it by joining the table to itself:
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
Now you can pivot on row_num:
transform Min(x.col2) select x.col1
from(
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
) x
group by x.col1
pivot x.row_num

Select complete matching between two table

I have two tables
t1
col1 col2
A 1
A 2
B 1
C 2
t2
col1
1
2
I want to retrieve the records in tab1 which match all the records in tab2,
For the given scenario, I want to output A only because it has both 1 and 2 in col2 where as B and C has only a 1 or a 2 (not both).
You can write the query this way:
select t1.col1
from t1 join
t2
on t1.col2 = t2.col1
group by t1.col1
having count(distinct t1.col2) = (select count(distinct t2.col1) from t2);
This counts the number of matching values in the first table and compares it to the total number of values in the table.

SQL query for counting rows with similar columns

Now i have a table with 4 columns as below
seq col1 col2 col3
1 1 2 3
1 1 2 4
2 1 2 3
3 1 2 4
result should be as below
number of seq1 s which have ( col1 col2 col3) same
for the above example the output is expected as
count(seq) col1 col2 col3
2 1 2 3
2 1 2 4
Trust, this is what you require..
Select Count(seq) as countseq, col1, col2, col3 from <Table>
group by col1, col2, col3
Or if you have the columns to compare in Table2 then
Select A.col1, A.col2, A.col3, count(B.Seq) from
<Table2> as A inner join <Table> as B
on A.Col1 = B.Col1 and A.Col2=B.Col2 and A.Col3 = B.Col3
group by A.Col1, A.Col2, A.col3
Generally when you want to compare similar rows, you need to either select those columns as a result and build a checksum against them or select those rows in some form (concatenated or hash) and join the temp table on itself and select the result set on the remaining rows.
Your question does not have enough information to provide a more complete answer.
I think you want to compare col1,col2 and col3 and pick out the count of seq. I concatenated the values of col1,col2 and col3. I cast these columns to varchar and then replaced nulls by '-'. I guess this should help.
Select seq,
ISNULL(Cast(col1 as Varchar(5)),'-')+ISNULL(Cast(col2 as Varchar(5)),'-')+ISNULL(Cast(col3 as Varchar(5)),'-') as tempcol
into #temp from Table
Select Count(seq) from #temp group by tempcol