I have 2 tables, Products & Discounts, where I need to get count of countries where the qty value is less than equal to Size, here are the two tables.
CREATE TABLE Products (P_Name VARCHAR(50), Qty INT);
INSERT INTO Products (P_Name, Qty)
VALUES
('Product A', 9),
('Product B', 2),;
CREATE TABLE Discounts (C_Name VARCHAR(50),Size INT);
INSERT INTO Discounts (C_Name, Size) VALUES
('USA', 2 ),
('Germany', 4),
('Brazil', 5),
('Russia', 6);
Here is the expected output
P_Name Qty Count
Product A 9 4
Product B 2 1
Explanation: For Product A the Qty is 9 , so its eligible to get discounts in 4 countries, similarly for Product B the count should be 1.
Here is what I tried, but Im unable to get correct output
Select *,
COUNT(CASE WHEN qty <= (SELECT Size from Discounts) then 1 else 0 END) as count
FROM
Products