I have a list of values and I am wondering if it's possible to query by said list, rather than having to do an "or" for each row of values or add them into a table and query the table (as I'm about to do)
Say, for example, my list is;
010, 46793, '329', '10'
011, 46798, '322', '12'
012, 33333, '111', '14'
I'd like to query like this;
SELECT VALUE1, VALUE2, VALUE3
FROM MYTABLE
WHERE (VALUEW VALUEX, VALUEY, VALUEZ) in(
(010, 46793, '329', '10'),
(011, 46798, '322', '12'),
(012, 33333, '111', '14'))
(This fails on syntax)
Rather than having to do;
SELECT VALUE1, VALUE2, VALUE3
FROM MYTABLE
WHERE (VALUEW VALUEX, VALUEY, VALUEZ) = (010, 46793, '329', '10')
OR (VALUEW VALUEX, VALUEY, VALUEZ) = (011, 46798, '322', '12')
OR (VALUEW VALUEX, VALUEY, VALUEZ) = (012, 33333, '111', '14')
Please note that something like;
SELECT VALUE1, VALUE2, VALUE3
FROM MYTABLE
WHERE VALUEW IN(010, 011, 012)
AND VALUEX IN(46793, 46798, 33333)
AND VALUEY IN('329', '322', '111')
AND VALUEZ IN('10', '12', '14')
Will not work in this scenario.
For example, if the below value existed in "MYTABLE";
011, 33333, '329', '10'
The aforementioned SQL would retrieve it, yet it's not on my list.
Try the syntax below (it is valid on Db2 LUW v11) not sure of Db2 for i:
SELECT VALUE1, VALUE2, VALUE3
FROM MYTABLE
where (VALUEW, VALUEX, VALUEY, VALUEZ) in ( values
(010, 46793, '329', '10'),
(011, 46798, '322', '12'),
(012, 33333, '111', '14')
);
Altenate solution for non-LUW DB2:
WITH mylist(w,x,y,z) AS (
SELECT 010, 46793, '329', '10' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 011, 46798, '322', '12' FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 012, 33333, '111', '14' FROM SYSIBM.SYSDUMMY1
)
SELECT VALUE1, VALUE2, VALUE3
FROM MYTABLE
INNER JOIN mylist
ON (VALUEW, VALUEX, VALUEY, VALUEZ)=(w,x,y,z)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.