Using Inner Join & Distinct in one query - sql

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

Related

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.

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.

How do i indent the output so that the countries are indented 2 spaces?

This is the query that returns to me the output of region id in the left column, region_name in the right column as well as the countries that fall under that region but i want to make the countries indented a couple of spaces...
SELECT region_id, region_name
FROM
(
SELECT r.region_id, r.region_name, 0 AS ent
FROM regions r
UNION ALL
SELECT r.region_id, c.country_name, 1 AS ent
FROM regions r
INNER JOIN countries c
ON r.region_id = c.region_id
)
ORDER BY region_id, ent, region_name;
1 Europe
1 Belgium
1 Denmark
1 France
1 Germany
1 Italy
1 Netherlands
1 Switzerland
1 United Kingdom
2 Americas
2 Argentina
2 Brazil
2 Canada
2 Mexico
2 United States of America
This should work:
SELECT r.region_id, CONCAT(' ', c.country_name), 1 AS ent
You might need to add an AS clause for the combined field to put it all together.

How to do Conditional Column Joins in SQL?

I have a tables below
Main Table: tblMain
Phase Country City
AAA India Bangalore
AAA USA Chicago
ZZZ USA
ZZZ UK
SubTable: tblSub
Phase Country City Value
AAA USA Chicago 3
AAA USA NY 6
AAA UK London 5
AAA India Bangalore 6
AAA India Delhi 9
ZZZ USA Chicago 7
ZZZ UK London 8
Expected Result
Phase Country City Value
AAA India Bangalore 6
AAA USA Chicago 3
ZZZ USA 7
ZZZ UK 8
I want to join this with my Main table which has Phase, Country and City, however the condition is
For Phase "ZZZ" i want to join only by Country where as for Phase "AAA" i want to join ty Country & City. Is is possible to do in SQL Query without a Stored procedure or temp tables
I am looking to achieve this in plain query. Thanks in advance!!!
SELECT *
FROM This_Table TT
LEFT JOIN Main_Table MT ON TT.Countrry = MT.Country
AND TT.Phase = 'ZZZ'
LEFT JOIN Main_Table MT2 ON TT.Countrry = MT2.Country
AND TT.City = MT2.City
AND TT.Phase = 'AAA'
This should do it:
WHERE
(a.phase = 'ZZZ' AND a.country = b.country)
OR
(a.phase = 'AAA' AND a.country = b.country AND a.city = b.city)

SQL statement to list and country countries in 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