I have a table:
PRICE
ticker date close
AAPL 3/20/2019 $100
AAPL 3/21/2019 $101.5
AAPL 3/22/2019 $106.5
GOOG 3/20/2019 $100
GOOG 3/21/2019 $130
GOOG 3/22/2019 $110
MSFT 3/20/2019 $184.5
MSFT 3/21/2019 $188.5
MSFT 3/22/2019 $210
IBM 3/20/2019 $72
IBM 3/21/2019 $70
IBM 3/22/2019 $10
And I want to know the correct way to compare the count of rows under two different conditions and return the ticker if the counts are equal. (Ultimately want to return tickers only if close>100 for all of march). So:
SELECT ticker, COUNT(*) FROM PRICE WHERE close>100 group by ticker;
returns:
| ticker | COUNT(*) |
+--------+----------+
| AAPL | 2 |
| GOOG | 2 |
| MSFT | 3 |
And
SELECT ticker, COUNT(*) FROM PRICE group by ticker;
returns:
| ticker | COUNT(*) |
+--------+----------+
| AAPL | 3 |
| GOOG | 3 |
| MSFT | 3 |
So MSFT should be the only ticker that returns to the output.
New to SQL and any suggestions would be appreciated!
One simple approach aggregates by ticker symbol and asserts that all closing values are greater than 100:
SELECT ticker -- maybe include COUNT(*), if you want to see the counts
FROM price
GROUP BY ticker
HAVING COUNT(CASE WHEN close <= 100 THEN 1 END) = 0;