SQL statement to list and country countries in SQL - sql

Let's say I have a some rows like this in a table:
SWEDEN
MEXICO
USA
SWEDEN
GERMANY
RUSSIA
MEXICO
SWEDEN
Now I need to create a script to count the countries and list them like this:
Country Amount of countries
SWEDEN 3
USA 1
MEXICO 2
RUSSIA 1
GERMANY 1
I'm stuck at:
SELECT Country
FROM dbo.Customers
How do I only show them once and create a row and count them?
Thanks a lot..

try
SELECT Country, count(*)
FROM dbo.Customers
group by Country

Related

How to sum values if I don't have column to group it by

I need to sum sales grouped by country, but I have to group them manually because I don't have any other way.
Unfortunately, I don't have the column 'continent', but there are not too many countries on the list so I can do it manually. I can't create any new columns in the table, so I need to do it in a query.
For example:
country | sum of sales
Germany 1000
Italy 500
Canada 700
UK 1300
USA 3000
I would like to see the total sales for Europe and North America
continent | sum of sales
Europe 2800
North America 3700
You should be able to combine case expression and in predicate, something along this lines:
SELECT CASE
WHEN country in ('Germany', 'UK') THEN 'Europe'
WHEN country in ('Canada', 'USA') THEN 'North America'
END as continent,
sum("sum of sales")
FROM table
GROUP BY 1

How group by count from multiple tables

I have 3 different tables, country, city, and customer. Tables are shown below:
country table:
id country_name
1 UK
2 US
3 Brazil
:
n Canada
city table
id city_name postal_code country_id
1 London 30090 1
2 Dallas 20909 2
3 Rio 29090 3
4 Atlanta 30318 2
:
n Vancouver 32230 n
customer table
id customer_name city_id
1 John 1
2 Pete 3
3 Dave 2
4 May 2
5 Chuck 4
6 Sam 3
7 Henry 3
***country.id is references city.country_id, and city.id is references customer.city_id
I want to write a query that can extract the country name, city name and the count of the customer of the associate city. But with one condition, the query will return all cities with more customers than the average number of customers of all cities
It will look something like below, this is the correct output
UK London 2
Brazil Rio 3
but I kept getting this output, which isn't correct
UK London 2
US Dallas 2
US Atlanta 1
Brazil Rio 3
I fixed my SQL query but it doesn't give me the result that I want
SELECT country.country_name, city.city_name, COUNT(customer.city_id) from country
JOIN city on country.id = city.country_id
JOIN customer on city.id = customer.city_id
Group by city_name,country.country_name;
I am wondering how can I do this and fix my code?
add country.country_name in group by
SELECT country.country_name, city.city_name, COUNT(customer.city_id) from country
JOIN city on country.id = city.country_id
JOIN customer on city.id = customer.city_id
Group by city_name,country.country_name
You are missing country.country_name in the query it will give you error. As a general rule all columns on which aggregate function is not applied should be part of group by clause.
So either you write your query without country_name in the select or add country_name in the group by clause.

How to show blank when value is repeated

SELECT * FROM Cities ORDER BY Country;
This is the result.
COUNTRY CITY PLACE
Italy Milan Zone_A
Italy Rome Zone_A
Italy Rome Zone_B
USA New York Zone_Q
USA Atlanta Zone_A
I would like to create a Stored Procedure that shows "blank" when the item is repeated. The final result should be the following. (Note that this rule is applied only in the first 2 columns, not in the third).
COUNTRY CITY PLACE
Italy Milan Zone_A
Rome Zone_A
Zone_B
USA New York Zone_Q
Atlanta Zone_A
If your version of maria DB supports window functions, you can use lag():
select
case when lag(country) over(order by country, city, place) = country
then null
else country
end country,
case when lag(city) over(order by country, city, place) = city
then null
else city
end city,
place
from cities
order by
country,
city,
place

I wonder how it works when multiple group by, like group by column_name(1), column_name(2), column_name(3)

When i checked it, it doesn't remove duplication of value. Why?
example) Group by a , Group by a,b,c
Is there a difference between Group by a, Group by a,b,c ?
I wrote SQL query like this ::
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
result ::
Table: Customers
COUNT(CustomerID) Country
---------------------------------
3 Argentina
2 Austria
2 Belgium
9 Brazil
3 Canada
2 Denmark
2 Finland
to
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country, CustomerID;
Table: Customers
COUNT(CustomerID) Country
---------------------------------
1 Germany
1 Mexico
1 Mexico
1 UK
1 Sweden
1 Germany
1 France
Why doesn't tie same value changed query from Column_name?
It display all value along column_name.
I wonder if it works. thank you.

Using Inner Join & Distinct in one query

I have two tables (shown below) called Merge_Codes & Factors. I am trying to write a query that takes the distinct countries from the factors table and joins this to the Merge_Codes table and returns the region.
Please see the results table at the bottom to see what I mean. I am trying to do this in one query but am getting no where near the solution, as you can see from the sql below.
SELECT * FROM Merge_Codes
INNER JOIN ON
(SELECT DISTINCT Country FROM Factors)
Merge_Codes Table
Region Country
EU Germany
EU France
EU Italy
Asia Japan
Asia Hong Kong
NA Canada
NA USA
SA Brazil
SA Peru
SA Chile
Factors Table
Factor Country
ABC Germany
ABC Germany
ABC Japan
ABC USA
ABC USA
ABC Hong Kong
Result
Country Region
Germany EU
Japan Asia
USA NA
Hong Kong Asia
select mc.*
from Merge_Codes mc
join (select distinct Country FROM Factors) f
on mc.Country = f.Country
select
MC.Country,
MC.Region
from
(SELECT DISTINCT Country FROM Factors) DC
INNER JOIN Merge_Codes MC ON MC.Country = DC.Country