Hugo Quintela
Hugo Quintela

Reputation: 11

Count occurrences of multiple columns as total

First post here... :)

What I have is this:

And what I need to show is:

The code I have is:

SELECT  Client, Model, COUNT(Model) Total
FROM    Table
GROUP BY Client, Model

but all this does is return every occurrence of both columns. Can anyone help, please?

Thank you in advance.

Upvotes: 1

Views: 759

Answers (3)

BellevueBob
BellevueBob

Reputation: 9618

It looks like you want the number of "clients" for each model; so try this:

select model
     , count(distinct client) as Total
from   table
group by model

Upvotes: 1

Andomar
Andomar

Reputation: 238076

To count the number of distinct clients per model:

SELECT  Model
,       COUNT(distinct Client) Total
FROM    Table
GROUP BY 
        Model

Live example at SQL Fiddle.

Upvotes: 5

Simon
Simon

Reputation: 1605

This is due by the Client in the group by. Exclude the Client from your query. Try this :

SELECT Model, COUNT(Model) Total
FROM Table
GROUP BY MODEL

Upvotes: 1

Related Questions