Connecting Tech Pros Worldwide Forums | Help | Site Map

is it possible?

Newbie
 
Join Date: Sep 2009
Posts: 2
#1: Sep 11 '09
Hi all,

I have the following question.

Is it possible to make a query such that the result will be displayed in the following format:
Expand|Select|Wrap|Line Numbers
  1. country       totalInstitutions       noInstitutionsMet
  2. Germany             15                            7
  3. U.S.                   10                            0
  4. France                 5                            4 
Basically to show the total no. of Institutions and the no of Institutions that met the customer X, grouped by country.

having the following tables:
institution (id, name, country, etc.)
customers (idCustomer, idInstitution, nameCustomer)
meetings (idmeeting, idCustomer, idInstitution)

Thank you!
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#2: Sep 11 '09

re: is it possible?


Hey.

Sure, that's possible.

You could use do a simple SELECT to get the Country and TotalInstitutions values, and add a sub-query that counts the Institutions based on whatever filter you need.

Like, for example, if you needed the total number of cars sold by each car dealership in a database, as well as the total number of cars in a specific color:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     `Dealership`,
  3.     COUNT(`Cars`) AS 'Total Cars',
  4.     (    SELECT COUNT(*)
  5.         FROM `CarTable` AS `inner`
  6.         WHERE 
  7.             `outer`.`Dealership` = `inner`.`Dealership`
  8.         AND `inner`.`Color` = 'Red'
  9.     ) AS 'Total Red Cars'
  10. FROM `CarTable` AS `outer`
  11. GROUP BY `Dealership`
You might get something like:
Expand|Select|Wrap|Line Numbers
  1. +------------+------------+----------------+
  2. | Dealership | Total Cars | Total Red Cars |
  3. +------------+------------+----------------+
  4. | First      |         10 |              3 |
  5. | Second     |         23 |             12 |
  6. | Second     |         16 |              7 |
  7. +------------+------------+----------------+
Newbie
 
Join Date: Sep 2009
Posts: 2
#3: Sep 14 '09

re: is it possible?


Thank you Atli!

I got some results but the row with the "Total Red Cars" gives the same numer, which is not correct. If I try to group by the sub-query, I will get an errow: "Subquery returns more than one row".

Here is the real query I am using. Thank you once more!

Expand|Select|Wrap|Line Numbers
  1.  SELECT Address.country, count( Address.country ) AS CompanyTotal, (
  2. SELECT COUNT( * )
  3.     FROM Company
  4.     INNER JOIN Position ON ( Company.orgId = Position.compId )
  5.     INNER JOIN Address ON ( Company.addressId = Address.addressId )
  6.     INNER JOIN Meeting ON ( Company.orgId = Meeting.company )
  7.     INNER JOIN EventDefinition ON ( Meeting.event = EventDefinition.idEventDef )
  8.     WHERE Position.customer = 'XXX'
  9.     AND Meeting.customer = 'XXX'
  10.     AND Position.nbrOfShare !=0
  11.     AND EventDefinition.date > '2009-01-01 00:00:00'
  12.     ) AS CompanyMet
  13. FROM Company
  14. INNER JOIN Position ON ( Company.orgId = Position.compId )
  15. INNER JOIN Address ON ( Company.addressId = Address.addressId )
  16. WHERE customer = 'XXX'
  17. AND Position.nbrOfShare !=0
  18. GROUP BY Address.country
  19. ORDER BY ct DESC
Reply