Count values in a query

  • Thread starter Kostas Arvanitidis
  • Start date
K

Kostas Arvanitidis

Hello,
I have a table:
Table1
[cityID] [clientID] [company] [percentage]
1 1 xxy 23
1 2 xxv 12
2 1 sss 11
2 3 aaa 3

I need to make a query that exports a result like this:
[clientId] [numberoftoppercent]
1 2
2 0
3 0

To explain, i want to count that client '1' topped in 2 cities, client '2'
in '0' etc.
Any help please?
 
K

Ken Sheridan

Try this:

SELECT clientID,
(SELECT COUNT(*)
FROM Table 1 AS T2
WHERE T2.clientID = T1.clientID
AND T2.percentage =
(SELECT MAX(percentage)
FROM Table1 AS T3
WHERE T3.cityID = T2.cityID))
AS numberoftoppercent
FROM Table1 AS T1;

Ken Sheridan
Stafford, England
 
S

Smartin

Ken - Very nice.

Corrected "Table 1" and added DISTINCT:

SELECT DISTINCT clientID,
(SELECT COUNT(*)
FROM Table1 AS T2
WHERE T2.clientID = T1.clientID
AND T2.percentage =
(SELECT MAX(percentage)
FROM Table1 AS T3
WHERE T3.cityID = T2.cityID))
AS numberoftoppercent
FROM Table1 AS T1;


Ken said:
Try this:

SELECT clientID,
(SELECT COUNT(*)
FROM Table 1 AS T2
WHERE T2.clientID = T1.clientID
AND T2.percentage =
(SELECT MAX(percentage)
FROM Table1 AS T3
WHERE T3.cityID = T2.cityID))
AS numberoftoppercent
FROM Table1 AS T1;

Ken Sheridan
Stafford, England

Kostas Arvanitidis said:
Hello,
I have a table:
Table1
[cityID] [clientID] [company] [percentage]
1 1 xxy 23
1 2 xxv 12
2 1 sss 11
2 3 aaa 3

I need to make a query that exports a result like this:
[clientId] [numberoftoppercent]
1 2
2 0
3 0

To explain, i want to count that client '1' topped in 2 cities, client '2'
in '0' etc.
Any help please?
 
K

Kostas Arvanitidis

Thank you both...
It worked like a charm....

Thanks again....

Smartin said:
Ken - Very nice.

Corrected "Table 1" and added DISTINCT:

SELECT DISTINCT clientID,
(SELECT COUNT(*)
FROM Table1 AS T2
WHERE T2.clientID = T1.clientID
AND T2.percentage =
(SELECT MAX(percentage)
FROM Table1 AS T3
WHERE T3.cityID = T2.cityID))
AS numberoftoppercent
FROM Table1 AS T1;


Ken said:
Try this:

SELECT clientID,
(SELECT COUNT(*)
FROM Table 1 AS T2
WHERE T2.clientID = T1.clientID
AND T2.percentage =
(SELECT MAX(percentage)
FROM Table1 AS T3
WHERE T3.cityID = T2.cityID))
AS numberoftoppercent
FROM Table1 AS T1;

Ken Sheridan
Stafford, England

Kostas Arvanitidis said:
Hello,
I have a table:
Table1
[cityID] [clientID] [company] [percentage]
1 1 xxy 23
1 2 xxv 12
2 1 sss 11
2 3 aaa 3

I need to make a query that exports a result like this:
[clientId] [numberoftoppercent]
1 2
2 0
3 0

To explain, i want to count that client '1' topped in 2 cities, client '2'
in '0' etc.
Any help please?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top