у нас есть следующее требование
Мне нужно имя_пользователя, которое находится в Активном статусе и имя_атрибута = Город и значение атрибута в (Индор, Мумбаи), и результат должен возвращать число меньше, чем = 2
Это означает, что из результата для Индора я должен получить 2 результата из 3, а для Мумбаи я должен получить 1 из 1.
Я пробовал ниже 2 способа, но получил все строки для клиентов, которые находятся в город Индаур и Мумбаи
Таблица клиентов содержит ниже детали
customer_id customer_name customer_Status
------------------------------------------
1 ABC Active
2 XYZ Active
3 PQR NA
4 ABCD Active
4 ABCDE Active
customer_details В таблице ниже указаны детали
customer_id attribute_name attribute_value
------------------------------------------
1 City Indore
1 Phone Number 9100000000
1 Country India
2 City Mumbai
2 Phone Number 9100000001
2 Country India
3 City Delhi
3 Phone Number 9100000002
3 Country India
4 City Mumbai
4 Phone Number 9100000003
4 Country India
5 City Mumbai
5 Phone Number 9100000004
5 Country India
Код : -
select attribute_value, r.customer_name from customer_details res
join lateral (
select customer_name from Customer_table
where res.customer_id=customer_id
and customer_Status= 'Active'
limit 2
) r on true
where attribute_name= 'City' and attribute_value in ('Indore','Mumbai');
Код: -
SELECT s.customer_name,attribute_value
FROM (
SELECT *, row_number() OVER (PARTITION BY customer_id ) AS rn
FROM customer_details
WHERE attribute_name= 'City' and attribute_value in ('Indore','Mumbai')
) e
JOIN Customer_table s USING (customer_id)
WHERE rn <= 2
and and customer_Status= 'Active'
ORDER BY customer_id, e.rn;