SQL "IN" statement for multiple columns - sql

I would like to filter Name,X combinations for which is never X=Y
Let's assume the following table:
*Name* *X* *Y*
A 2 1
A 2 2 <--- fulfills requirement for Name=A, X=2
A 10 1
A 10 2
B 3 1
B 3 3 <--- fulfills requirement for Name=B, X=3
B 1 1 <--- fulfills requirement for Name=B, X=1
B 1 3
So I would like to return the combination Name=A, X=10 for which X=Y is never true.
This was my approach (which is syntactically incorrect)
SELECT *
FROM TABLE
WHERE NAME
, X NOT IN (SELECT DISTINCT NAME
, X
FROM TABLE
WHERE X=Y)
My problem is the where statement which cannot handle multiple columns. Does anyone know how to do this?

Just put the columns into parentheses
SELECT *
FROM TABLE
WHERE (NAME, X) NOT IN (SELECT NAME, X
FROM TABLE WHERE X=Y);
The above is ANSI standard SQL but not all DBMS support this syntax though.
A distinct is not necessary for a sub-query for IN or NOT IN.
However NOT EXISTS with a co-related sub-query is very often faster that an NOT IN condition.

I use this on SQL Server
SELECT *
FROM TABLE
WHERE (SELECT NAME + ';' + X)
NOT IN (SELECT NAME + ';' + X
FROM TABLE WHERE X = Y);

I think you can use two condition to achieve this
SELECT *
FROM TABLE
WHERE NAME NOT IN(
SELECT a.NAME FROM TABLE a WHERE a.X=a.Y
) AND X NOT IN (
SELECT b.X FROM TABLE b WHERE b.X=b.Y
)

SELECT *
FROM TABLE T
WHERE NOT EXISTS (SELECT NAME
,X
FROM TABLE t2
WHERE t1.Name=t2.Name
AND t1.X=t2.Y)
This will check if there is such a record

Related

SQL: select rows from a certain table based on conditions in this and another table

I have two tables that share IDs on a postgresql .
I would like to select certain rows from table A, based on condition Y (in table A) AND based on Condition Z in a different table (B) ).
For example:
Table A Table B
ID | type ID | date
0 E 1 01.01.2022
1 F 2 01.01.2022
2 E 3 01.01.2010
3 F
IDs MUST by unique - the same ID can appear only once in each table, and if the same ID is in both tables it means that both are referring to the same object.
Using an SQL query, I would like to find all cases where:
1 - the same ID exists in both tables
2 - type is F
3 - date is after 31.12.2021
And again, only rows from table A will be returned.
So the only returned row should be:1 F
It is a bit hard t understand what problem you are actually facing, as this is very basic SQL.
Use EXISTS:
select *
from a
where type = 'F'
and exists (select null from b where b.id = a.id and dt >= date '2022-01-01');
Or IN:
select *
from a
where type = 'F'
and id in (select id from b where dt >= date '2022-01-01');
Or, as the IDs are unique in both tables, join:
select a.*
from a
join b on b.id = a.id
where a.type = 'F'
and b.dt >= date '2022-01-01';
My favorite here is the IN clause, because you want to select data from table A where conditions are met. So no join needed, just a where clause, and IN is easier to read than EXISTS.
SELECT *
FROM A
WHERE type='F'
AND id IN (
SELECT id
FROM B
WHERE DATE>='2022-01-01'; -- '2022' imo should be enough, need to check
);
I don't think joining is necessary.

Apply a Substr to a distinct result in oracle

In Oracle, I need to do the average of a column (timeInmillis) from a join query that shows "duplicated" values (relative to the timeInmillis column from table1). I need to mantain the values from the join, but get the right result for the average.
I'm trying to do something like this:
select AVG(SUBSTR(DISTINCT(concat(id1,timeInMillis)),LENGTH(id1)+1,LENGTH(CONCAT(id1,timeInMillis)))), someColumn, otherColumn
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id1 = t2.id1 group by somestuff,someotherStuff;
If I try to do this, I get:
ORA-00936: missing expression
This would be an example:
Table1:
id1 timeInMillis otherColumn
1 5 X
2 15 X
Table2:
id2 id1 otherColumn
--------------------
1 1 X
2 1 X
3 2 X
From my join I get a resultset like this:
id1 id2 timeInmillis moreColumns
--------------------------------
1 1 5 X
1 2 5 X
2 3 15 X
I need to get the average of 5 and 15 (with distinct id1), but I can't modify the where part of the sql (cause of the other values I'm getting)
My result should be:
AVG(TIMEINMILLIS) otherResults
----------------------------------
10 'whatever'
Thanks in advance.
1) Option
select SUBSTR(someColumn,n,m) from (
select DISTINCT someColumn from MYTABLE
);
2) Option
select DISTINCT SUBSTR(someColumn,n,m) from MYTABLE;
*) Queries can return different result.
Your last edit finally explains clearly what you want. You want one line only, showing the avarage of table1's values, but of course without the duplicates that you got because of joining with table 2.
One solution is to get to the final value in two steps:
select avg(distinct_time), sum(sub_sum)
from
(
select max(timeinmillis) as distinct_time, sum(some_other_colum) as sub_sum
from (query)
group by id1
);
The other solution would be to rewrite the query.
Your syntax is wrong. You can try somthing like this either:-
select avg(TimeInMillis), other_cols_as_well
from(SELECT TAB1.id1, TAB2.id2, avg(TimeInMillis) as TimeInMillis
FROM TAB1, TAB2
WHERE TAB1.id1 = TAB2.id1
group by TAB1.id1, TAB2.id2) temp
where temp.id1 <> temp.id2
group by other_cols_as_well
Here is the fiddle
http://sqlfiddle.com/#!4/1fc017/16

SQL - Query Basic between 2 tables

I would like to know in MYSQL (editor HeidiSQL)
I have 2 Tables (email & spam) 2 rows (ID, EMAIL) and i want to clean my database :
Tables Used in Set Operation Examples
EMAIL
x y
------------------
1 one#edu.com
2 two#edu.com
2 two#edu.com
3 three#edu.com
SPAM
x z
------------------
1 one#edu.com
2 two#edu.com
4 four#edu.com
USUALLY i use EXCEPT but it doesn't work on MYSQL.
proc sql;
title 'EMAIL EXCEPT SPAM'
select * from sql.EMAIL
except
select * from sql.SPAM;
Producing Rows That Are in Only the First Query Result (EXCEPT)
EMAIL EXCEPT SPAM
x y
------------------
3 three#edu.com
i try to use WHERE NOT EXISTS but i don't know.
Can you help me for making the query :
SELECT *
FROM EMAIL
WHERE ... ????
Thank's
SELECT * FROM EMAIL WHERE y NOT IN (SELECT z FROM SPAM)
Left Outer Join with Null in spam should work.
SELECT E.*
FROm EMAIL E
LEFT JOIN SPAM S
ON E.Email = S.Email
WHERE S.Id IS NULL
USUALLY i use EXCEPT but it doesn't work on MYSQL.
EXCEPT is not supported in mySQL. Note that in absence of an explicit ALL | DISTINCT keyword, EXCEPT defaults to EXCEPT DISTINCT. Therefore, in your workaround you should explicitly use SELECT DISTINCT (because SELECT defaults to SELECT ALL).
i try to use WHERE NOT EXISTS
SELECT DISTINCT y
FROM EMAIL
WHERE NOT EXISTS ( SELECT *
FROM SPAM
WHERE y = z );

Counting the rows of multiple distinct columns

I'm trying to count the number of rows that have distinct values in both of the columns "a" and "b" in my Sybase ISQL 9 database.
What I means is, the following dataset will produce the answer "4":
a b
1 9
2 9
3 8
3 7
2 9
3 7
Something like the following syntax would be nice:
SELECT COUNT(DISTINCT a, b) FROM MyTable
But this doesn't work.
I do have a solution:
SELECT COUNT(*) FROM
(SELECT a, b
FROM MyTable
WHERE c = 'foo'
GROUP BY a, b) SubTable
But I was wondering if there is a neater way of constructing this query?
How about:
SELECT COUNT(*)
FROM (SELECT DISTINCT a, b FROM MyTable)
For more information on why this can't be done in a simpler way (besides concatenating strings as noted in a different answer), you can refer to the this Google Answers post: Sql Distinct Count.
You could concatenate a and b together into 1 string like this (TSQL, hopefully something very similar in Sybase:
SELECT COUNT(DISTINCT(STR(a) + ',' + STR(b)))
FROM #YourTable

Select values in SQL that do not have other corresponding values except those that i search for

I have a table in my database:
Name | Element
1 2
1 3
4 2
4 3
4 5
I need to make a query that for a number of arguments will select the value of Name that has on the right side these and only these values.
E.g.:
arguments are 2 and 3, the query should return only 1 and not 4 (because 4 also has 5). For arguments 2,3,5 it should return 4.
My query looks like this:
SELECT name FROM aggregations WHERE (element=2 and name in (select name from aggregations where element=3))
What do i have to add to this query to make it not return 4?
A simple way to do it:
SELECT name
FROM aggregations
WHERE element IN (2,3)
GROUP BY name
HAVING COUNT(element) = 2
If you want to add more, you'll need to change both the IN (2,3) part and the HAVING part:
SELECT name
FROM aggregations
WHERE element IN (2,3,5)
GROUP BY name
HAVING COUNT(element) = 3
A more robust way would be to check for everything that isn't not in your set:
SELECT name
FROM aggregations
WHERE NOT EXISTS (
SELECT DISTINCT a.element
FROM aggregations a
WHERE a.element NOT IN (2,3,5)
AND a.name = aggregations.name
)
GROUP BY name
HAVING COUNT(element) = 3
It's not very efficient, though.
Create a temporary table, fill it with your values and query like this:
SELECT name
FROM (
SELECT DISTINCT name
FROM aggregations
) n
WHERE NOT EXISTS
(
SELECT 1
FROM (
SELECT element
FROM aggregations aii
WHERE aii.name = n.name
) ai
FULL OUTER JOIN
temptable tt
ON tt.element = ai.element
WHERE ai.element IS NULL OR tt.element IS NULL
)
This is more efficient than using COUNT(*), since it will stop checking a name as soon as it finds the first row that doesn't have a match (either in aggregations or in temptable)
This isn't tested, but usually I would do this with a query in my where clause for a small amount of data. Note that this is not efficient for large record counts.
SELECT ag1.Name FROM aggregations ag1
WHERE ag1.Element IN (2,3)
AND 0 = (select COUNT(ag2.Name)
FROM aggregatsions ag2
WHERE ag1.Name = ag2.Name
AND ag2.Element NOT IN (2,3)
)
GROUP BY ag1.name;
This says "Give me all of the names that have the elements I want, but have no records with elements I don't want"