Select all rows where two columns contain a combination of values - sql

Here's my schema:
CREATE TABLE T (A CHAR(1), B CHAR(1));
INSERT INTO T (A, B) VALUES('1', '1');
INSERT INTO T (A, B) VALUES('2', '2');
INSERT INTO T (A, B) VALUES('1', '2');
I wan't to select rows where columns A and B contain a combination of values. For example, lets say I want to find combinations A=1,B=1, and A=2,B=2, and nothing else. If it were a single value, I could use the IN statement, but when I try this:
SELECT * FROM T WHERE A IN ('1', '2') AND B IN ('1', '2')
I get back all three rows.
How can I match a combination of values?

You can check this query :
SELECT * FROM T WHERE (A,B) IN (('1', '1'),('2', '2'));
see given link below
Click here

Is this what you're looking for using OR with parentheses?
select *
from T
where ( A = '1' AND B = '1' ) OR ( A = '2' AND B = '2' )
SQL Fiddle Demo

you can use something like below sql
SELECT * FROM T WHERE (A == B) AND A IN ('1', '2');

Related

Update column based on IF Else Condition

I have two tables A and B
Table A
ID_number as PK
first_name,
L_Name
Table B
ID_number,
Email_id,
Flag
I have several people who have multiple email ID and are already flagged as X on table B.
Whereas i am trying to find list of people who have an email id or multiple email ID, but were never flagged.
e.g John clark might have 2 email in table B, but was never flagged.
Simply use not exists:
select a.*
from a
where not exists (select 1
from b
where b.id_number = a.id_number and b.flag = 'X'
);
You may want to perform an update, but your question seems to be only about selecting (probably to update based on select). It should be something like this:
SELECT A.L_Name
FROM A
WHERE NOT EXISTS (
SELECT 1
FROM B
WHERE B.ID_number = A.ID_number AND B.Flag = 'X'
)
OR the LEFT JOIN version
SELECT 1
FROM A
LEFT JOIN B ON B.ID_number = A.ID_number AND B.Flag = 'X'
WHER B.ID_number IS NULL
Usually, the first version is faster than the second one.
Forget Table A...
SELECT DISTINCT ID_number FROM table_b t1
WHERE NOT EXISTS(
SELECT NULL FROM table_b t2 WHERE t1.ID_number=t2.ID_number AND t2.flag='X'
)
Judging by your responses in the comments, I believe this is what you are looking for:
--drop table update_test;
create table update_test
(
id_num number,
email_id number,
flag varchar2(1) default null
);
insert into update_test values (1, 1, null);
insert into update_test values (1, 2, null);
insert into update_test values (2, 3, null);
insert into update_test values (2, 7, null);
insert into update_test values (3, 2, null);
insert into update_test values (3, 3, 'X');
insert into update_test values (3, 7, null);
select * from update_test;
select id_num, min(email_id)
from update_test
group by id_num;
update update_test ut1
set flag = case
when email_id = (
select min(email_id)
from update_test ut2
where ut2.id_num = ut1.id_num
) then 'X'
else null end
where id_num not in (
select id_num
from update_test
where Flag is not null);
The last update statement will update and set the Flag field on the record for each id_num group with the lowest email_id. If the id_num group already has the Flag field set for one it will ignore it.

SQL Querying on tuple values

I need to write a write a SQL query that selects values from a table based on several tuples of selection criteria. It could be done using a where clause like this :
where (a = 1 and b='a') or (a=5 and b='s')
Is the best way to select:
select a, pk from x where a in (1,5)
select b, pk from x where b in ('a','s')
and join the result of the two queries using the primary key?
do you mean something(a self join) like this:
select x.a, x.pk
from x
join x x2 on x.pk=x2.pk
where x.a in (1,5)
and x2.b in ('a','s')
?
You can use join on table expression from VALUES. You can add in VALUES as much rows as you want. It will work on MSSQL:
DECLARE #x TABLE ( a INT, b CHAR(1) )
INSERT INTO #x
VALUES ( 1, 'a' ),
( 1, 'b' ),
( 1, 'c' ),
( 2, 'd' ),
( 2, 'e' ),
( 5, 'f' ),
( 5, 's' )
SELECT x.*
FROM #x x
JOIN (
VALUES ( 1, 'a'),
( 5, 's')
) AS v( a, b ) ON x.a = v.a AND x.b = v.b
Output:
a b
1 a
5 s
Based on my understanding you want write a SQL that uses a combination of two filters. Here is a simple solution that will work in any database.
Create a new column say "COLUMN_NEW" in the same table or build a temp table or a view with a new column (plus existing columns from original table).
Insert concatenated values of column a and column b in "COLUMN_NEW". Based on the example mentioned by you values in "COLUMN_NEW" will be "1a" and "5s"
Now you may have a different syntax for concat in different databases. Example concat(a,b) in SQL server.
SQL to select records from the table will be select * from table where COLUMN_NEW in ("1a",5s");

Oracle SQL Insert whit Case Select

I'm new in this site and I have another question. In this case, it is from Oracle SQL, Insert operation with a CASE.
My SQL insert's code is:
INSERT WHEN (SELECT TB0083_DS_TIPODISPOSITIVO FROM TB0083_TIPODISPOSITIVO WHERE TB0083_ID_TIPODISPOSITIVO = (SELECT FOR_DISPOSITIVO FROM TFORPD01 WHERE FOR_CODIGO = &FORNECEDOR))='TIV' THEN
INTO TTRAPD01 (TRA_CODIGO,TRA_CODBARRA,TRA_CODLOC,TRA_CODCON,TRA_DATLOC,TRA_CODCAIXA,TRA_STATCOND,TRA_DT_CRIACAO,TRA_NM_USUARIOCRIACAO,TRA_DT_ALTERACAO,TRA_NM_USUARIOALTERACAO,TRA_CD_CONTA,TRA_CODCTR,TRA_TRANSACAO_ONLINE,TRA_TIV_HEXA,TRA_TIV_BINARIO,TRA_CD_DISPOSITIVO,TRA_DATSINC,TRA_ID_TPSEGREG,TRA_FORNECEDOR,TRA_STATUS)
VALUES (&NUMEROIDENTIFICADOR,&CODIGOBARRAIDENTIFICADOR,&CODIGOPONTOVENDALARM,259,SYSDATE,&CODIGOCAIXA,0,SYSDATE,&USUARIOLOGUEADO,SYSDATE,&USUARIOLOGUEADO,422,1,0,&TIVHEXA,&TIVBINARIO,423,SYSDATE,3,&FORNECEDOR,1)
ELSE
INTO TTRAPD01 (TRA_CODIGO,TRA_CODBARRA,TRA_CODLOC,TRA_CODCON,TRA_DATLOC,TRA_CODCAIXA,TRA_STATCOND,TRA_DT_CRIACAO,TRA_NM_USUARIOCRIACAO,TRA_DT_ALTERACAO,TRA_NM_USUARIOALTERACAO,TRA_CD_CONTA,TRA_CODCTR,TRA_TRANSACAO_ONLINE,TRA_TIV_HEXA,TRA_TIV_BINARIO,TRA_CD_DISPOSITIVO,TRA_DATSINC,TRA_ID_TPSEGREG,TRA_FORNECEDOR,TRA_STATUS)
VALUES (&NUMEROIDENTIFICADOR,&CODIGOBARRAIDENTIFICADOR,&CODIGOPONTOVENDALARM,259,SYSDATE,&CODIGOCAIXA,0,SYSDATE,&USUARIOLOGUEADO,SYSDATE,&USUARIOLOGUEADO,422,1,0,&TIVHEXA,&TIVBINARIO,423,SYSDATE,2,&FORNECEDOR,1);
This script doesn't found.
I need to fix it, because I need to change the value of field TRA_ID_TPSEGREG depends on the value of:
SELECT TB0083_DS_TIPODISPOSITIVO FROM TB0083_TIPODISPOSITIVO WHERE TB0083_ID_TIPODISPOSITIVO = (SELECT FOR_DISPOSITIVO FROM TFORPD01 WHERE FOR_CODIGO = &FORNECEDOR)
If value is 'TIV' then it inserts 3 in that position, else it inserts 2 in those field.
Thanks!!
There's PL/SQL INSERT WHEN command:
Try this:
INSERT FIRST
WHEN TB0083_DS_TIPODISPOSITIVO = 'TIV' THEN
INTO TTRAPD01
(
<COLUMN_NAME1>,<COLUMN_NAME2>......
)
VALUES
(
<COLUMN_VALUE1>,<COLUMN_VALUE2>.....
)
ELSE
INTO TTRAPD01
(
<COLUMN_NAME1>,<COLUMN_NAME2>......
)
VALUES
(
<COLUMN_VALUE1>,<COLUMN_VALUE2>.....
)
SELECT TB0083_DS_TIPODISPOSITIVO
FROM TB0083_TIPODISPOSITIVO
WHERE TB0083_ID_TIPODISPOSITIVO =
(SELECT FOR_DISPOSITIVO FROM TFORPD01 WHERE FOR_CODIGO = &FORNECEDOR
);
For eg:
CREATE TABLE TEST ( ID NUMBER);
INSERT FIRST
WHEN dummy = 'Y' THEN
INTO TEST
VALUES(222)
ELSE
INTO TEST
VALUES(555)
select dummy from dual;
The above query will insert 1 ROW on the very first condition satisfy. See more: >>here<<

PostgreSQL: How to search a list of strings as a table?

It's been a while since I've had to do any db work, so I'm not really sure how to ask this and I know I've done it in the past.
How do you create a temporary table out of a list of strings (not using CREATE TEMPORARY TABLE)? So, if you have something like :
'1', 'a', 'A'
'2', 'b', 'B'
'3', 'c', 'C'
SELECT field2
FROM { {'1','a','A'}, {'2','b','B'}, {'3','c','C'} }
AS fooarray(field1,field2,field3)
WHERE field1 = '2'
-- should return 'b'
Hint: It's similar to...
SELECT * FROM unnest(array[...]);
You don't need to mess around with arrays at all, you can build the table in-place using VALUES:
7.7. VALUES Lists
VALUES provides a way to generate a "constant table" that can be used in a query without having to actually create and populate a table on-disk.
See also VALUES.
So you can do things like this:
=> select *
from (
values ('1', 'a', 'A'),
('2', 'b', 'B'),
('3', 'c', 'C')
) as t(id, c1, c2)
where id = '2';
id | c1 | c2
----+----+----
2 | b | B
(1 row)
Don't forget to give your VALUES an alias complete with column names (t(id, c1, c2)) so that everything has a name.

How to fix a query which mixes AND and OR conditions?

I need to query a table with a filtering condition on three columns:
select *
from table a
where a.order = car
and a.color !=red
or a.automatic !=auto
This just checks the first conditions and ignores the second one.
I just can't get my head around it :(
Its not clear from your question but all you probably need is to wrap parens around your OR condition
Given this following table and rows
CREATE TABLE table_a
(
id int primary key,
a_order varchar2(20),
color varchar2(40),
automatic varchar2(10)
)
/
INSERT INTO table_a VALUES (1,'car','red','auto') /
INSERT INTO table_a VALUES (2,'car','red','manual') /
INSERT INTO table_a VALUES (3,'car','blue','auto') /
INSERT INTO table_a VALUES (4,'car','blue','manual') /
INSERT INTO table_a VALUES (5,'truck','red','auto') /
INSERT INTO table_a VALUES (6,'truck','red','manual') /
INSERT INTO table_a VALUES (7,'truck','blue','auto') /
INSERT INTO table_a VALUES (8,'truck','blue','manual') /
This select statement will return records 2, 3 and 4 since they are all cars and are either blue or manual
SELECT *
FROM table_a a
WHERE a.a_order = 'car'
AND ( a.color != 'red'
OR a.automatic != 'auto' )
Demo
Another option is to do the following using De Morgan's law "The negation of a disjunction is the conjunction of the negations."
SELECT *
FROM table_a a
WHERE a.a_order = 'car'
AND NOT ( a.color = 'red'
AND a.automatic = 'auto' )
Demo
The priority of AND is higher than OR logical operator. You can use OR condition within parenthesis.
SELECT *
FROM table_a a
WHERE a.a_order = 'car'
AND NOT ( a.color = 'red'
AND a.automatic = 'auto' )