Values of Column col1 that have multiple values in col2 - sql

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.

Related

Select multiple columns but distinct only one in SQL?

Lets say I have a table called TABLE with the columns col1, col2, col3 and col4
I want to select col1, col2 and col3 but distinct col2 values from the others, but I can't do it.
I tried something like this:
SELECT DISTINCT "col1", "col2", "col3" FROM [Table] WHERE col1 = Values
But the output brings me more than one record of col 2 with the same value.
I know that is because the distinct filtered all the columns that i specified, but i don't know how to get all the columns and filter only the values of col2.
Is it possible to SELECT more than 1 column but filter only one of them with SELECT DISTINCT ?
As you said, distinct just limits the full set of columns to eliminate duplicates. Instead, I'd just use an aggregate function with a GROUP BY statement.
SELECT MAX(col1) AS col1, col2,
MAX(col3) AS col3
FROM tbl
GROUP BY col2
That will take the top value alphanumerically from the supplied columns. Or, to list all values separated by commas:
SELECT STRING_AGG(col1,',') AS col1, col2,
STRING_AGG(col3,',') AS col3
FROM tbl
GROUP BY col2

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.

SQL DISTINCT based on a single column, but keep all columns as output

--mytable
col1 col2 col3
1 A red
2 A green
3 B purple
4 C blue
Let's call the table above mytable. I want to select only distinct values from col2:
SELECT DISTINCT
col2
FROM
mytable
When I do this the output looks like this, which is expected:
col2
A
B
C
but how do I perform the same type of query, yet keep all columns? The output would look like below. In essence I'm going through mytable looking at col2, and when there's multiple occurrences of col2 I'm only keeping the first row.
col1 col2 col3
1 A red
3 B purple
4 C blue
Do SQL functions (eg DISTINCT) have arguments I could set? I could imagine it to be something like KeepAllColumns = TRUE for this DISTINCT function? Or do I need to perform JOINs to get what I want?
You can use window functions, particularly row_number():
select t.*
from (select t.*, row_number() over (partition by col2 order by col2) as seqnum
from mytable t
) t
where seqnum = 1;
row_number() enumerates the rows, starting with "1". You can control whether you get the oldest, earliest, biggest, smallest . . .
You can use the QUALIFY clause in Teradata:
SELECT col1, col2, col3
FROM mytable
QUALIFY ROW_NUMBER() OVER(PARTITION BY col2 ORDER BY col2) = 1 -- Get 1st row per group
If you want to change the ordering for how to determine which col2 row to get, just change the expression in the ORDER BY.
With NOT EXISTS:
select m.* from mytable m
where not exists (
select 1 from mytable
where col2 = m.col2 and col1 < m.col1
)
This code will return the rows for which there is not another row with the same col2 and a smaller value in col1.

remove duplicated set of columns in table

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)

sqlite: select all columns where one filed has max value over all columns

I have a table like this:
id int, col1 int, ...
Different rows can have col1 of same value.
Now I want to gather all rows where col1 has a the maximum value.
e.g. this table values
1 4
2 3
3 4
The query shall give my row 1 and 3
You can use subquery:
SELECT id, col1
FROM tab
WHERE col1 = (SELECT MAX(col1) FROM tab);
SqlFiddleDemo