which command should i use on SQL for having? - sql

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;

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

How do I keep only 1 result according the alphabetical order in a tie with SQL queries?

The question asked to take the city with the highest sum of goods that bought by customer for each country. Basically, there are cities that have the same number of goods, but we only keep the first one in alphabetical order. The result only contains country name, the city with highest number of goods and their goods in sum.
Table Schema:
Country table:
country_name
city_name
Goods table:
city_name
user_id
number_of_goods
My queries result:
France Paris 85
Germany Berlin 100
Germany Frankfurt 100
Germany Luxembourg 100
Netherlands Amsterdam 75
Spain Barcelona 93
The right result should be:
France Paris 85
Germany Berlin 100
Netherlands Amsterdam 75
Spain Barcelona 93
You can use row_number() :
select t.*
from (select t.*, row_number() over (partition by country order by city) as seq,
max(no_goods) over (partition by country) as max_good
from table t
) t
where seq = 1;
use aggregation functions min() for city and max() for no_of_goods.
select t1.country, t1.no_of_goods, min(t2.city) as city
from
(select country, max(no_of_goods) as no_of_goods from tableA
group by country) t1
left join tableA t2 on t2.no_of_goods = t1.no_of_goods and t1.country = t2.country
group by t1.country, t1.no_of_goods
see dbfiddle.
Basically, there are cities that have the same number of goods, but we only keep the first one in alphabetical order.
Based on your sample data, all cities in a country seem to have the same number_of_goods. If so, you can just use aggregation:
select c.country, min(c.city_name), max(number_of_goods)
from countries c join
goods g
on c.city_name = g.city_name
group by c.country;

Query to output customers and employees and their cities where each city must contain at least one customer and one employee

I must take sellers and customers and output them in one column, showing their cities and types.
My problem is: I don't need to output customers whose cities aren't in sellers' table and vice versa.
SELECT
ContactName, City, Type
FROM
(SELECT
'Seller' AS Type,
ContactName, City
FROM
[dbo].[Suppliers] t
GROUP BY
City, ContactName
UNION
SELECT
'Customer',
ContactName, City
FROM
[dbo].[Customers] t
GROUP BY
City, ContactName) t
GROUP BY
ContactName, City, Type
Result :
| Ivan Ivanov | Seller | Moscow |
| Piotr Petrov | Seller | Moscow |
| Ivan Romanov | Customer | Moscow |
| Johnny Bravo | Customer | London |
(let's assume there are no sellers in London therefore this column shouldn't exist)
Expected result: only columns with information where a city has at least one seller and one customer grouped by contact name and city
This seems like union all and exists:
SELECT DISTINCT c.ContactName, 'Customer' as type, c.City
FROM Customers c
WHERE EXISTS (SELECT 1 FROM Sellers s WHERE s.city = c.city)
UNION ALL
SELECT DISTINCT s.ContactName, 'Seller' as type, s.City
FROM Sellers s
WHERE EXISTS (SELECT 1 FROM Customers c WHERE c.city = s.city);
I'm not sure the SELECT DISTINCT is really needed -- I don't see why the underlying tables would have duplicates (although ContactName is not really a good column for unique identification). However, your original query has GROUP BY, suggesting that you want to eliminate duplicates.

Challenge with Not Equal to operator

I have a problem to solve. I would like to get the countries where the gender not equal to Female from the following table using only the where clause. I don't want to use the sub query like: select country from table where country not in (select country from table where gender='Female')
Any ideas ?
ID Name Gender Country
1 Jhon Male USA
2 Katie Female USA
3 Steave Male UK
4 Gerry Female UK
5 Brad Male AUS
Regards,
Chandra.
Use not exists
select t.*
from table t
where not exists (select 1
from table
where Country = t.Country and
Gender = 'Female'
);
You can also use group by like that :
select Country
from table t
group by Country
having sum(case when Gender = 'Female' then 1 else 0 end) = 0;
You could avoid subquery and get full rows by using:
SELECT TOP 1 WITH TIES *
FROM tab
ORDER BY SUM(CASE WHEN Gender='Female' THEN 1 ELSE 0 END)
OVER(PARTITION BY Country);
DBFiddle Demo - SQL Server
You can do:
select country
from t
except
select country
from t
where gender = 'Female';
As a set operator, except removes duplicates.
Maybe I got your question wrong but why you don't use:
SELECT country FROM table WHERE gender NOT IN('Female')
Or is it a sub query?

SELECT clause with same data using DISTINCT

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