SELECT clause with same data using DISTINCT - sql

Example in my employee table i having these data
//EMPLOYEE
E# NAME CITY
--------------------------
1 JOHN ENGLAND
2 SITI ENGLAND
3 JESS GERMANY
4 HOLY BOSTON
When i run this statement:
SELECT DISTINCT CITY, COUNT(E#) as "Total Employee" FROM EMPLOYEE Where CITY=;
what should i insert the value for
//CITY=?
in order to get the result like this
CITY TOTAL EMPLOYEE
----------------------------
ENGLAND 2
GERMANY 1
BOSTON 1
isn't impossible to use group by or having clause?
ANSWER FIXED ! THANKS

Well, you're not excluding any cities, so you don't need a WHERE clause at all! But you DO need a GROUP BY to do a count. You can just do:
SELECT CITY, COUNT(E#) as "Total Employee"
FROM EMPLOYEE
GROUP BY CITY
However if you only want to include those three cities (even if more are added to the underlying data), you can do:
SELECT CITY, COUNT(E#) as "Total Employee"
FROM EMPLOYEE
WHERE CITY IN ('ENGLAND', 'GERMANY', 'BOSTON')
GROUP BY CITY

Related

Oracle Sql Developer (Select Count) Twoc olumns

I have to make a query that has the Total number of customers by country and city
country and city are columns that are inside the customer table
On my own I have managed to get the total number of customers per city like this:
SELECT city, COUNT (*)
FROM employees
GROUP BY city
ORDER BY city
But how do I get it together with the country?
looking for information I think it should be something like this and ordered from largest to smallest
Country
City
TOTAL_CUSTOMERS
USA
Kirkland
3
USA
London
2
UK
Redmond
2
UK
Seattle
1
UK
Tacoma
1
What we have been told is to say Total number of customers by country and city.
You simply add country to the column list and group by list:
SELECT country,city, COUNT(*)
FROM employees
GROUP BY country,city
ORDER BY COUNT(*) DESC

which command should i use on SQL for having?

I want your help.
I have 2 columns with data, customer and city.
I want to show customers a mix if this customer has more different cities, and I want to show the customer if it has only 1 city.
for example, I have this data:
customer city
ana London
Ella London
Sarah Paris
Haidi Greece
Chloe France
ana London
Ella france
I want to show it like this:
Ana London
Ella Mix
Sarah Paris
Haidi Greece
Chloe France
How could I do this? Which command should I use?
Here you go.
Select
Customer,
case when count(DISTINCT city) > 1 then 'MIX' Else max(City) End as City
from MyTable
Group by Customer
You could use a command like before:
Select
Customer,
case when count(city) > 1 then 'MIX' Else City End as City
from Mytable
Group by Customer, City
but Ana is present twice in the table, and you have to use the Max to solve
Select
Customer,
case when count(city) > 1 then 'MIX' Else MAX(City) End as City
from Mytable
Group by Customer
If you wanted NULL instead of 'Mix', you could use:
select customer, nullif(min(city), max(city)) as city
from t
group by customer;
You can actually extend this to get 'Mix':
select customer, coalesce(nullif(min(city), max(city)), 'Mix') as city
from t
group by customer;
But NULL makes sense to me.
Here's what I think you're looking for
select customer,
case when count(*)>1
then 'mix'
else max(city) end as city
from t
group by customer;

Sql join in two tables and return empty tab as Unavaliable

In a SQL join, table 1 contains person info with city and table 2 contains city matched to country such as:-
Table #1
ID Name City
-------------------------
1 Kishan Pokhara
2 Ram Delhi
3 Shyam Beijing
Table #2
City Country
----------------------
Pokhara Nepal
Delhi India
I want to get the person ID, Name, Country so while joining the tables I want these items and if there is no country available for a city, I want "Unavailable" written in the country columns. Thanks
Try the below using left join and use coalesce() function to replace null country as 'Unavailable'
select id, name, a.city,coalesce(country,'Unavailable') as country
from table1 a left join table2 b on a.city=b.city

SQL ordering cities ascending and persons descending

I have been stuck in complicated problem. I do not know the version of this SQL, it is school edition. But it is not relevant info now anyway.
I want order cities ascending and numbers descending. With descending numbers I mean when there is same city couple times it orders then biggest number first.
I also need row numbers, I have tried SELECT ROW_NUMBER() OVER(ORDER BY COUNT(FIRST_NAME)) row with no succes.
I have two tables called CUSTOMERS and EMPLOYEES. Both of them having FIRST_NAME, LAST_NAME, CITY.
Now I have this kind of code:
SELECT
CITY, COUNT(FIRST_NAME),
CASE WHEN COUNT(FIRST_NAME) >= 0 THEN 'CUSTOMERS'
END
FROM CUSTOMERS
GROUP BY CITY
UNION
SELECT
CITY, COUNT(FIRST_NAME),
CASE WHEN COUNT(FIRST_NAME) >= 0 THEN 'EMPLOYEES'
END
FROM EMPLOYEES
GROUP BY CITY
This SQL code gives me list like this:
CITY
NEW YORK 2 CUSTOMERS
MIAMI 1 CUSTOMERS
MIAMI 4 EMPLOYEES
LOS ANGELES 1 CUSTOMERS
CHIGACO 1 CUSTOMERS
HOUSTON 1 CUSTOMERS
DALLAS 2 CUSTOMERS
SAN JOSE 2 CUSTOMERS
SEATTLE 2 CUSTOMERS
SEATTLE 5 EMPLOYEES
BOSTON 1 CUSTOMERS
BOSTON 3 EMPLOYEES
I want it look like this:
ROW CITY
1 NEW YORK 2 CUSTOMERS
2 MIAMI 4 EMPLOYEES
3 MIAMI 1 CUSTOMERS
4 LOS ANGELES 1 CUSTOMERS
5 CHIGACO 1 CUSTOMERS
6 HOUSTON 1 CUSTOMERS
7 DALLAS 2 CUSTOMERS
8 SAN JOSE 2 CUSTOMERS
9 SEATTLE 5 EMPLOYEES
10 SEATTLE 2 CUSTOMERS
11 BOSTON 3 EMPLOYEES
12 BOSTON 1 CUSTOMERS
You can use window functions in the ORDER BY:
SELECT c.*
FROM ((SELECT CITY, COUNT(*) as cnt, 'CUSTOMERS' as WHICH
FROM CUSTOMERS
GROUP BY CITY
) UNION ALL
(SELECT CITY, COUNT(*), 'EMPLOYEES'
FROM EMPLOYEES
GROUP BY CITY
)
) c
ORDER BY MAX(cnt) OVER (PARTITION BY city) DESC,
city,
cnt DESC;

count multiple values from single column

I have a table with customer information like date of birth, address, contactinfo, etc.
I want to count the number of customers per city with a single query that outputs two values per record, cityname and amount of customers living there:
Alabama 285
Kentucky 167
New York 4
Rio de Janeiro 950
etc...
There are hundreds of cities in the table so I can't do a
SELECT count(CASE WHEN city = 'Alabama' THEN 1 END) AS Alabama
You can use GROUP BY clause to count the number of customers per city :
SELECT city
, COUNT(*)
FROM table
GROUP BY city