table1 has columns A1,B1,C1 and table2 has columns A2,B2,C2 where A1 and A2 refer to the same thing, and B1 and B2 refer to the same thing.
How do I find rows in table1 such that the equivalent rows in table2 (A2=A1, B2=B1) are not in table1, and vice versa?
is it an EXCEPT?
You can use NOT EXISTS like
select A1, B1, C1
from table1
where not exists
(
select 1 from table2
where A2 = table1.A1
and B2 = table1.B1
)
Related
I have a SQLite database with this sample table:
CREATE TABLE PROVA
(
c1 TEXT NOT NULL UNIQUE,
c2 TEXT NOT NULL,
c3 TEXT NOT NULL
);
This table will be empty at certain times (e.g. during initialization) and I need a query which can return an output like this without having to specify the columns name (a dummy value for every column).
c1 c2 c3
-------- -------- --------
dummy dummy dummy
We could use a union trick here with the help of RANK:
WITH cte AS (
SELECT c1, c2, c3, 1 AS pos FROM PROVA
UNION ALL
SELECT 'dummy', 'dummy', 'dummy', 2
),
cte2 AS (
SELECT c1, c2, c3, RANK() OVER (ORDER BY pos) rn
FROM cte
)
SELECT c1, c2, c3
FROM cte2
WHERE rn = 1;
Need help. I am putting my requirement in simple steps here. I have a data like below.
with x as (
select 'a=x AND b=y AND c=z' C1, 'a' C2, '100' C3 from dual union all
select 'a=x AND b=y AND c=z' C1, 'b' C2, '200' C3 from dual union all
select 'a=x AND b=y AND c=z' C1, 'c' C2, '300' C3 from dual union all
select 'a=x AND d=y AND c=z' C1, 'd' C2, '400' C3 from dual union all
select 'a=x AND e=y AND c=z' C1, 'e' C2, '500' C3 from dual
)
select * from x;
My output looks like below:
C1 C2 C3
------------------------------
a=x AND b=y AND c=z a 100
a=x AND b=y AND c=z b 200
a=x AND b=y AND c=z c 300
a=x AND d=y AND c=z d 400
a=x AND e=y AND c=z e 500
I am looking for a query to get the output like below. I have a condition in one column (C1), Also I have look-up data in same table in different columns (C2 and C3). I want to replace the values in C1 if any of the string exists in column C2 with the value from column C3.
100=x AND 200=y AND 300=z a 100
100=x AND 200=y AND 300=z b 200
100=x AND 200=y AND 300=z c 300
100=x AND 400=y AND 300=z d 400
100=x AND 500=y AND 300=z e 500
My exact requirement is, I have a table with a column contains WHERE condition (C1) like above. They used business column names in condition and there is second column with business name (C2) and there is third column with actual physical column name in DB (C3) in the same table. I am looking for the query which can replace the business names in C1, by looking at column C2 with the corresponding value in column C3.
There are two tables(t1 and t2) in my database,both of them has three fields code ,date and price.t1 has 800 records ,t2 has 790 records , code fields are the same.
select distinct code from t1 = select distinct code from t2
i want to choose records from t1 and t2.
suppose that in t1
code date
x1 d1
x1 d2
x1 d3
in t2:
code date
x1 d4
x1 d2
x1 d5
I want to choose records in t1.
code date
x1 d1
x1 d3
I want to choose records in t2.
code date
x1 d4
x1 d5
how to write the sqlite command?
Think for CL,it works fine for me,
but it is difficult for me to understand the query.
1.what is the meaning of
SELECT 1
FROM t2
WHERE t2.code = t1.code
AND t2.date = t1.date ?
Why don't write it as
SELECT 1
FROM t1
WHERE t2.code = t1.code
AND t2.date = t1.date ?
2.why i can't write it as
SELECT *
FROM t1
WHERE NOT EXISTS (SELECT 1
FROM t2,t1
WHERE t2.code = t1.code
AND t2.date = t1.date)
the query SELECT 1 FROM t2 WHERE t2.code = t1.code AND t2.date = t1.date will get two 1 .and what is the meaning of two 1 exists?
To check whether a record exists, use EXISTS with a correlated subquery.
This selects all t1 rows whose combination of code/date values do not exist in t2:
SELECT *
FROM t1
WHERE NOT EXISTS (SELECT 1
FROM t2
WHERE t2.code = t1.code
AND t2.date = t1.date)
You can also try this:
SELECT *
FROM t1
WHERE (t1_code || t1_date) not in
(
select (t2_code || t2_date)
from t2 );
What I am doing is just concatenating code and date in t1 and concatenating code and date in t2 and getting the combinations that are there in t1 and not there in t2.
I have two tables
table1
c1t1 c2t1
1 saanu
3 abc
table2
c1t2 c2t2
2 val2
4 val4
I have to find out the values of c2t1 and c2t2 for the minimum and maximum value of c1t1 and c1t2 with one line command.
For the above example I have to find saanu and val4
I had a very similar problem and solved it with UNION ALL. The minimum of the aColumn column in tables aTable1, ... , aTableN can be computed as:
SELECT Min(aColumn)
FROM (
SELECT aColumn FROM aTable1 UNION ALL
SELECT aColumn FROM aTable2 UNION ALL
...
SELECT aColumn FROM aTableN) t;
You should be able to do Min in each of the inner selects, but I haven't found out how to do that!
One approach:
select max(case c1 when min1 then c2 end) c2_first,
max(case c1 when max1 then c2 end) c2_last
from (select c1t1 c1, c2t1 c2 from table1
union all
select c1t2 c1, c2t2 c2 from table2) u
cross join
(select min(min11, min12) min1, max(max11, max12) max1 from
(select min(c1t1) min11, max(c1t1) max11 from table1) t1
cross join
(select min(c1t2) min12, max(c1t2) max12 from table2) t2) m
SQLFiddle here.
1)
SELECT c2t1
FROM table1
ORDER BY c1t1 ASC LIMIT 1
2)
SELECT c2t2
FROM talbe2
ORDER BY c1t2 DESC LIMIT 1
I have table A with an Id column.
I have table B with an Id1 and Id2 column.
I want to return all rows in table b that neither Id1 or Id2 exist in table A. If either Id1 or Id2 in table b has a match in table A, I want to return that result.
Hence, if table A looked like the following:
Id
123
456
789
and table B looked like the following:
Id1 Id2
123 545
343 432
184 789
Row 1 and 3 would not be returned as they each have a match in table A. However, row 2 in table b matches on neither column, so it would be returned.
I've been racking my head and can't seem to figure out the query. Any help would be appreciated!
Assuming your Id columns are not null:
select * from tableB
where Id1 not in (select Id from tableA)
and Id2 not in (select Id from tableA)
or
select b.*
from tableB b
left join tableA a1 on b.id1=a1.id
left join tableA a2 on b.id2=a2.id
where a1.id is null and a2.id is null
When looking for records where some data does not exist in another table there is always the exists clause, hence the name ;-)
select * from tableB
where not exists
(
select *
from tableA
where id in (tableB.id1, tableB.id2)
);