remove duplicated set of columns in table - sql

I created co-occurrence table as follows.
col1 col2 count
a b 10
b a 10
c d 7
d c 7
I want to keep co-occurrence rows without duplication like this.
col1 col2 count
a b 10
c d 7
How can I do this?

One simple method is:
select col1, col2, count
from t
where col1 < col2;
If you actually want to change the table, you can do:
delete t from t
where col1 > col2;
This assumes that all pairs of columns are in the database.

When inserting or selecting, do something like this instead of col1, col2:
LEAST(col1, col2), GREATEST(col1, col2)

Related

SQL HAVING COUNT WITH TWO COLUMNS

I have the following tableA
COL1 COL2 COL3
A 1 10-1-2019
B 4 13-4-2019
A 1 13-4-2019
A 1 10-1-2019
A 1 10-1-2019
C 3 20-4-2020
A 1 13-4-2019
I this is the sql code i wish to write but the count do not accept two elements :
select COL1,COL2,COL3
from TableA
group by COL1,COL2,COL3,
HAVING COUNT(COL1,COL2) > 2
And only the result A 1 10-1-2019 should appear because it is the only one that have the pair COL1 COL3 with more than 2 results(in this case three times). The pair COL1 /COL3 is something like a composed primary key
How can i achieve this ?
My database is Sybase ASE
Thanks in advance.
I think this does what you want:
select COL1, COL2, COL3
from TableA
group by COL1, COL2, COL3,
HAVING COUNT(*) > 2;
Perhaps you have a bad example. But I don't see why you think you need to exclude col2.

Values of Column col1 that have multiple values in col2

I have a table like this:
col1 col2
a 10
b 20
a 30
c 40
a 05
I am trying to write a query to get all values in column col1 that have values 10,20,30 in column col2. I used AND statement but it returns nothing
You could use IN clause to give multiple inputs
SELECT col1 FROM yourtable WHERE COL2 IN (10,20,30)
It is equivalent of doing:
SELECT col1 FROM yourtable WHERE COL2 = 10 OR COL2 = 20 OR COL2 = 30
You can do this using group by and having:
select col1
from t
where col2 in (10, 20, 30)
group by col1
having count(distinct col2) = 3;
The use of AND for this type of query is a very, very common mistake. The problem is that no one row can have all three values -- and WHERE is only looking at one row at a time when it is filtering. Instead, you need to get all the rows with these values and then bring those rows together to see how many matches there are for each col1 value.

How to use and "in" clause in "having" in HIVE?

I have my data in sometable like this:
col1 col2 col3
A B 3
A B 1
A B 2
C B 1
And I want to get all of the unique groups of col1 and col2 that contain certain rows of col3. Like, all groups of col1 and col2 that contain a "2".
I wanted to do something like this:
select col1, col2 from sometable
group by col1, col2
having col3=1 and col3=2
But I want it to only return groups that have an instance of both 1 and 2 in col3. so, the result after the query should return this:
col1 col2
A B
How do I express this in HIVE? THANK YOU.
I don't know why others deleted answers that where correct and then almost correct but I will put their's back up.
SELECT col1, col2, COUNT(DISTINCT col3)
FROM
sometable
WHERE
col3 IN (1,2)
GROUP BY col1, col2
HAVING
COUNT(DISTINCT col3) > 1
If you actually want to return all of the records that meet your criteria you need to do a sub select and join back to the main table to get them.
SELECT s.*
FROM
sometable s
INNER JOIN (
SELECT col1, col2, COUNT(DISTINCT col3)
FROM
sometable
WHERE
col3 IN (1,2)
GROUP BY col1, col2
HAVING
COUNT(DISTINCT col3) > 1
) t
ON s.Col1 = t.Col1
AND s.Col2 = t.Col2
AND s.col3 IN (1,2)
The gist of this is narrow/filter your rowset to the rows that you want to test col3 IN (1,2) then count the DISTINCT values of col3 to make sure both 1 and 2 exist and not just 1 & 1 or 2 & 2.
I think below mentioned query will be useful for your question.
select col1,col2
from Abc
group by col1,col2
having count(col1) >1 AND COUNT(COL2)>2

Select column in query based on other table

I have a table called A where records contains some column name of table B.
table A
Id, columnName
1 col1
2 col2
3 col3
table B
ID, col1, col2, col3, col4, col5
I want to select columns of B based on the value of table A.
Example
Select col1, col2, col3
from B
If the record number 3 in table A were deleted the sql statement will be.
Select col1, col2
from B
You need a join. A basic SQL construct.
http://www.w3schools.com/sql/sql_join_inner.asp

select all columns with one column has different value

In my table,some records have all column values are the same, except one. I need write a query to get those records. what's the best way to do it? the table is like this:
colA colB colC
a b c
a b d
a b e
What's the best way to get all records with all the columns? Thanks for everyone's help.
Assuming you know that column3 will always be different, to get the rows that have more than one value:
SELECT Col1, Col2
FROM Table t
GROUP BY Col1, Col2
HAVING COUNT(distinct col3) > 1
If you need all the values in the three columns, then you can join this back to the original table:
SELECT t.*
FROM table t join
(SELECT Col1, Col2
FROM Table t
GROUP BY Col1, Col2
HAVING COUNT(distinct col3) > 1
) cols
on t.col1 = cols.col1 and t.col2 = cols.col2
Just select those rows that have the different values:
SELECT col1, col2
FROM myTable
WHERE colWanted != knownValue
If this is not what you are looking for, please post examples of the data in the table and the wanted output.
How about something like
SELECT Col1, Col2
FROM Table
GROUP BY Col1, Col2
HAVING COUNT(*) = 1
This will give you Col1, Col2 that have unique data.
Assuming col3 has the difs
SELECT Col1, Col2
FROM Table
GROUP BY Col1, Col2
HAVING COUNT(*) > 1
OR TO SHOW ALL 3 COLS
SELECT Col1, Col2, Col3
FROM Table1
GROUP BY Col1, Col2, Col3
HAVING COUNT(Col3) > 1