![](/style/images/good.png)
![](/style/images/bad.png)
SQL Troubleshooting with Multiple Number in Query
source link: https://www.codesd.com/item/sql-troubleshooting-with-multiple-number-in-query.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SQL Troubleshooting with Multiple Number in Query
I already provide sql fiddle with schema and sample data.
http://sqlfiddle.com/#!2/e9d22/7/0
If I would like to know how many province and how many cities in Thailand.
Country Name | No. Provinces | No. Cities
Thailand | 77 | 1234
I guess that it need to use multiple COUNT(*) but I dont know how to use it.
Anybody know please suggest solution?
You need to use GROUP BY
and COUNT
:
SELECT c.name, count(distinct p.id) provincecoutn, count(distinct city.id) citycount
FROM country c
LEFT JOIN province p on c.id = p.country_id
LEFT JOIN City on p.id = city.province_id
GROUP BY c.name
Good luck.
Related Articles
UPDATE SQL database with multiple SET?
Clause 'In' in SQL server with multiple columns
How to select a single row from a sql table with multiple rows having the same value in a column
create a SQL table with multiple columns automatically
Can not read Excel file from SQL Server with multiple versions of SQL Server
SQL removal with multiple joins
C # ADO.NET Loading SQL Script with Multiple Declarations and Comments
SQL records with multiple values for a field
SQL SP with Multiple Values for an Input
Left SQL joins with multiple lines in a single line
SQL Pivot with multiple columns
Express + Node Route with multiple parameters in Query String
Select a single ID in the SQL statement with multiple criteria
Impala - Getting an error with Multiple number of distinct values
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK