I have a table with a structure similar to the one below:
|C1|C2|C3|
|K1|V1|??|
|K1|V2|??|
|K1|V3|??|
|K2|V2|??|
I need to write a query that checks if the key(lets say K1) maps to a specific value in any row (say V2). If it does the value in column C3 is taken as 1 otherwise its 0.
I'd appreciate any help.
The folloiwng query should give the results you want. The subquery identifies all C1
values which map to a certain value in C2
at least once. If so, then we render the C3
values as 1, otherwise we show 0.
SELECT
t1.C1,
t1.C2,
CASE WHEN t2.C1 IS NOT NULL THEN 1 ELSE 0 END AS C3
FROM yourTable t1
LEFT JOIN
(
SELECT C1
FROM yourTable
GROUP BY C1
HAVING SUM(CASE WHEN C2 = 'V1' THEN 1 ELSE 0 END) > 0
) t2
ON t1.C1 = t2.C1
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.