SQL How can I count the number of meetings for each client?

WiceJav

I have two tables Meeting and client. I have two foreign keys in the meeting table id_client1 and id_client2. How can I count the number of meetings that took place between clients?

I have such a query

SELECT client.id_client, count(meet.id_client1)count_meeting
FROM meet JOIN client ON meet.id_client1=client.id_client
GROUP BY  client.id_client;

But this query only count meeting client1. What can I do to count the meetings for both clients?

I would like such a result

id_client       count_meeting
  1                   3
  2                   1
  3                   2
Thorsten Kettner

You may want a union of the sets of clients:

SELECT id_client, COUNT(*)
FROM
(
  SELECT id_client1 AS id_client FROM meet
  UNION ALL
  SELECT id_client2 AS id_client FROM meet
) participants
GROUP BY id_client
ORDER BY id_client;

And if you want to include clients that didn't participate in any meeting:

SELECT c.id_client, COUNT(p.id_client)
FROM client c
LEFT JOIN
(
  SELECT id_client1 AS id_client FROM meet
  UNION ALL
  SELECT id_client2 AS id_client FROM meet
) p ON p.id_client = c.id_client
GROUP BY c.id_client
ORDER BY c.id_client;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i count the number of rows duplicated?

How can I do count on number of occurrences of each distinct value of a column in PostgreSQL?

How can I count the number of matches for a regex?

How can i count the number of errors on Form?

How can I count the number of relationships each node has in Cypher?

how can I count a number from a string

How can i get count of each month in current year SQL?

How can I produce a count on the number of times each word has occurred in the following

How can i count exams for each category

How can I count the number of rows within each group using SQL?

How can I get the count of consecutive positive number in each column in 2 dimensional df in python/ Padas

How can I adjust start and end time for reoccuring meetings in Outlook?

How to can I count the number of ones?

How can I count the number of CPU cores?

How can I count the result of a for each loop?

How can I count the number of

How can i count the number coming in excel

How can I count the number of elements in an array?

How can I count a number inside a foreach?

How can I count the number of messages in one channel of each user?

How can I count the number of elements in IEnumerator?

How can I count the number of numbers in a string

How Can I count the number of Capital Cities within each Continent?

How can I count each row with duplicate entries in SQL server?

How can I count the number of list items for each unordered list in jquery

How can i count number of records in last 30 days for each user per row in pyspark?

how I can count the number of positive value in each column?

SQL - How to count the number of wins for each player

How can I count the number of rectangular in column?