Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT Statement in SQL for filtering out multiple entries

I want to query how many different accounts are linked to one price.

name           | price | acc_no

Allnet Flat S  | 14.99 |   1
Allnet Flat XL | 24.99 |   2
Data Flat XL   | 14.99 |   2
Data Flat S    |  9.99 |   1
Family Flat S  | 29.99 |   3
Family Flat XXL| 59.99 |   4
SMS Flat S     |  9.99 |   1

My closest solution is a

SELECT price, COUNT(acc_no) FROM "table" GROUP BY price

The problem here is that it shows me two accounts linked to 9.99, although it is the same account number. How can I make it count the same account only once?

like image 885
neolith Avatar asked Feb 18 '26 17:02

neolith


1 Answers

Use DISTINCT in the COUNT function:

SELECT price, COUNT(DISTINCT account_no) FROM "table" GROUP BY price
--                  ^^^^^^^^
like image 88
Sergey Kalinichenko Avatar answered Feb 20 '26 06:02

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!