My issue somewhat complex, so I will explain using simplified versions of my tables.
This is Table 1:
Value L AID OID
A 1 1
B 2 1
C 3 1
D 4 1
E 2 1
F 2 2
G 2 3
H 2 4
This is Table 2:
Value R AID OID VAR
Z 0 1 0
Y 1 1 1
X 2 1 1
W 4 1 1
V 0 2 0
U 1 2 1
T 3 2 1
I would like to join these tables such that any row in Table 1 that doesn’t have a corresponding row in Table 2 with both matching AID and OID is returned in a join to the row with a matching OID and an AID and VAR of 0. In this example it would look like this:
Value L Value R AID OID VAR
C Z 3 1 0
F V 2 2 0
H V 4 2 0
I am not certain how to tackle this. Specifically, not sure how to stop the rows that have a matching AID and OID from showing up in my join, and only having the rows that don't have that match. Any advice would be appreciated.
I think you want not exists:
select t1.*
from table1 t1
where not exists (select 1
from table2 t2
where t2.aid = t1.aid and t2.oid = t1.oid and t2.var = 0
);
Related
Please help me to create query. I have table with languages like
Id Code
---------
1 EN
2 DE
3 RU
and table with translations
Id Code LanguageId Value
------------------------------------------
1 1 1 EnglishTranslation
2 1 3 RussianTranslation
3 2 1 EnglishTranslation
4 2 2 DeutschTranslation
5 3 1 EnglishTranslation
I'm trying to get this result
Id Code LanguageId Value
------------------------------------------
1 1 1 EnglishTranslation
1 1 2 NULL
2 1 3 RussianTranslation
3 2 1 EnglishTranslation
4 2 2 DeutschTranslation
4 2 3 NULL
5 3 1 EnglishTranslation
5 3 2 NULL
5 3 3 NULL
Need to get translations for all languages by Code from Translations table So far I try
select
T.id, T.Code, L.Id, T.Value
from Languages L
left join Translations T on T.LanguageId = L.Id
but I got not expected result. Could you please suggest
http://sqlfiddle.com/#!6/e9bed/1
You can use CROSS JOIN operator to construct a cartesian product of (LanguageId, Code) pairs, and left-join translation table to it:
SELECT
t.Id, y.Code, x.LanguageId, t.Value
FROM
((SELECT Id AS LanguageId FROM Languages) AS x
CROSS JOIN
(SELECT DISTINCT(Code) AS Code FROM Translations) AS y)
LEFT OUTER JOIN Translations t ON y.Code=t.Code AND x.LanguageId=t.LanguageId
ORDER BY t.Code, t.LanguageId
Note that this wouldn't produce a valid translation Id for rows missing from Translations, i.e. the result would look like this:
Id Code LanguageId Value
---------------------------------------------
1 1 1 EnglishTranslation
NULL 1 2 NULL
2 1 3 RussianTranslation
3 2 1 EnglishTranslation
4 2 2 DeutschTranslation
NULL 2 3 NULL
5 3 1 EnglishTranslation
NULL 3 2 NULL
NULL 3 3 NULL
Demo.
I resolved the issue and got the result that you expected.
Run the below query:
SELECT
(CASE
WHEN T.Id is null and lc.Code = 1 THEN 1
WHEN T.Id is null and lc.Code = 2 THEN 4
WHEN T.Id is null and lc.Code = 3 THEN 5
ELSE T.Id
END) as Id,
lc.Code, lc.Id as LanguageId, T.Value from
(SELECT x.Id, y.Code from (SELECT Id FROM Languages) x cross join (SELECT DISTINCT(Code) as Code FROM Translations) y) as lc
left outer join Translations T ON lc.Id = T.LanguageId and lc.Code = T.Code
order by Id, Code, LanguageId
You can also see the solution in the below link:
http://sqlfiddle.com/#!6/e9bed/30
Hopefully it will work as you like.
I have a problem which I can't describe without explaining this on this example:
So there are 2 columns like:
X Y
A 2
A 1
A 3
B 3
C 2
A 1
D 2
B 1
B 3
C 1
A 1
D 3
D 1
and now I would like to select only that data from X, where one of the values from Y is 2.
So my output should look like:
X Y
A 2
A 1
A 3
C 2
A 1
D 2
C 1
A 1
D 3
D 1
because Y=2 for X=B doesn't exist in the main table.
My question is what is the query for this operation? I tried something with CASE WHEN but something didn't fix for me.
Try
SELECT X FROM Table WHERE X IN (SELECT X FROM Table WHERE Y=2)
OR Try
SELECT t1.X FROM Table t1
INNER JOIN Table t2 ON t1.X = t2.X
WHERE t2.Y = 2
Try a subquery:
SELECT X FROM table WHERE X IN (SELECT X FROM table WHERE Y = 2);
Have a table like that. Let's pretend it is full table and we dont have any other rows:
ID Place
1 A
1 B
2 C
3 D
How can I perform such thing:
ID Place YesNo
1 A 1
1 B 1
1 C 0
1 D 0
2 A 0
2 B 0
2 C 1
2 D 0
3 A 0
3 B 0
3 C 0
3 D 1
For yes/no I need CASE statement but how to do the rest- full join table on itlself or some other options?
Thanks!
You can achieve that with two subqueries which each list the possible values of one of the columns. The two results should then be crossed to get all combinations. Finally outer join this with the original table to see which combinations actually occur:
SELECT first.ID,
second.Place,
CASE WHEN mytable.ID IS NULL THEN 0 ELSE 1 END AS YesNo
FROM (
SELECT DISTINCT ID
FROM mytable
) first
CROSS JOIN (
SELECT DISTINCT Place
FROM mytable
) second
LEFT JOIN mytable
ON mytable.ID = first.ID
AND mytable.Place = second.Place
ORDER BY first.ID,
second.Place
Here is an SQL fiddle
If I have 2 tables, each table has one column only:
Table A:
col1
1
1
1
Table B:
col1
1
1
1
When I say:
select * from A left join B on a.col1 = b.col1
It has same output as :
select * from A,B (cartesian join).
Why is this?
If you add another column for clarity's sake, I think the answer becomes easier to visualise:
Table A:
ID col1
1 1
2 1
3 1
Table B:
ID col1
1 1
2 1
3 1
So your cartesian product is:
A.ID A.Col1 B.ID B.ID
1 1 1 1
1 1 2 1
1 1 3 1
2 1 1 1
2 1 2 1
2 1 3 1
3 1 1 1
3 1 2 1
3 1 3 1
Now add the predicate WHERE A.Col1 = B.Col1 and you can see that this is true for all the rows. So a left join will return the same results as a cross join.
I have 2 tables like below:
Table 1
---------
1
2
3
Table 2
--------
A
B
C
How do i join to get an output like below:
Output
---------
1 A
1 B
1 C
2 A
2 B
2 C
3 A
3 B
3 C
Use Cross join:
SELECT *
FROM Table1
CROSS JOIN Table2
You can change the order by replacing * with table fields.
Read more about Cross Join.
OR you could do this:
SELECT Table1.*,Table2.*
FROM Table2,Table1
Result:
ID NAME
1 A
1 B
1 C
2 A
2 B
2 C
3 A
3 B
3 C
You want to do a CROSS JOIN and that will give you the Cartesian product of all the rows.
See http://en.m.wikipedia.org/wiki/Join_(SQL)
select * from table 1,table 2 .
For o/p like A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
just