user2514052
user2514052

Reputation: 21

Count specific occurence using SQL

I have a problem with SQL Select query. I need to count order's which belong to account which has one or more orders which cost equals 1.

Here is the structure:
enter image description here

Could anyone help with select query. The result should be 2. Many thanks for help.

Upvotes: 2

Views: 59

Answers (1)

Anders Abel
Anders Abel

Reputation: 69260

You have to make two nested queries against the table. An outer one that counts the number of orders for an account and an inner one that finds the accounts that have at least one order with cost equals 1.

SELECT Account_ID, COUNT(*)
FROM Orders
WHERE Account_ID IN (SELECT Account_ID FROM Orders WHERE PRODUCT_Cost = 1)
GROUP BY Account_ID

Upvotes: 2

Related Questions