How to show blank when value is repeated - sql

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

Related

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;

subquery with NOT IN

I have a table of cities like:
state city
----- ----
texas houston
texas austin
texas dallas
texas san antonio
texas beaumont
texas brownsville
texas el paso
california anaheim
california san diego
california los angeles
california oakland
california simi valley
california san francisco
I need a query to find the states that don't have a city named 'houston' or 'dallas'. My first thought was this
select distinct state from cities where city not in ('houston', 'dallas');
but that won't work. I think I need a subquery and a NOT IN of some sort..
A way you can do this is with a NOT EXISTS clause:
Select Distinct State
From Cities C1
Where Not Exists
(
Select *
From Cities C2
Where C2.City In ('Houston', 'Dallas')
And C1.State = C2.State
)
select distinct state from cities where state not in (SELECT state FROM cities WHERE city in ('houston', 'dallas'));
Another method, may be slightly faster:
select distinct state from cities where state not in (select state from cities where city in ('houston', 'dallas'));
Select State
from Cities
group by State
having count(case when Cities in ('houston', 'dallas') then cities end) = 0
This will return all states where the number of cities associated with that state and matching your criteria is 0 (i.e. there are no such cities associated with the state).

Using HAVING with GROUPING SETS

My sql string as below
SELECT Country, City, COUNT(*) AS [Count]
FROM CountriesAndCities
GROUP BY GROUPING SETS ( ( Country, City ), ( Country) )
ORDER BY Country, City
I am getting results as below
Country City CountryCount
---------- ---------- ------------
France NULL 4
France Paris 4
Spain NULL 6
Spain Barcelona 3
Spain Madrid 3
If country has got only one city record, can I get results as below with using HAVING
Country City CountryCount
---------- ---------- ------------
France Paris 4
Spain NULL 6
Spain Barcelona 3
Spain Madrid 3
SELECT Country, City, COUNT(*) AS [Count]
FROM CountriesAndCities
GROUP BY GROUPING SETS ( ( Country, City ), ( Country) )
HAVING GROUPING(City) = 0
OR COUNT(DISTINCT City) > 1
ORDER BY Country, City
GROUPING(City) = 0 if City one of the fields currently being grouped by.
Conversely, when GROUPING(City) = 1 then City is reported as NULL.
This means that we always include the rows where the City is mentioned (not NULL).
For the other rows, where GROUPING(City) = 1 aka City IS NULL, then we only include the row if more than one City has been aggregated in to the result.

SQL COUNT only display results once

My simple query is as follows in SQL Server Management Studio 2012:-
SELECT Last_Name, City,
(SELECT COUNT (City) FROM Customers IX WHERE IX.City = EX.City) as counting
FROM Customers EX
GROUP BY City, Last_Name
ORDER BY City
Outputs this:-
Last_Name City Counting
Joe London 3
Smith London 3
Carter London 3
Stones New York 3
Jones New York 3
White New York 3
But I would like it to not repeat the counts for the same city and output like this:-
Last_Name City Counting
Joe London 3
Smith London
Carter London
Stones New York 3
Jones New York
White New York
How would I achieve this please?
Seems like a very odd request, and easily done on the application side. But it is not that hard in SQL:
SELECT Last_Name, City,
(case when row_number() over (partition by Last_Name, City order by (select NULL)) = 1
then (SELECT COUNT(City) FROM Customers IX WHERE IX.City = EX.City) as counting
end) as Counting
FROM Customers EX
GROUP BY City, Last_Name
ORDER BY City;
This will put the value on one row for each city. It is not determinate which one, but would probably be the first in practice. You can guarantee this by using a stable sort:
SELECT Last_Name, City,
(case when row_number() over (partition by Last_Name, City order by Last_Name) = 1
then (SELECT COUNT(City) FROM Customers IX WHERE IX.City = EX.City) as counting
end) as Counting
FROM Customers EX
GROUP BY City, Last_Name
ORDER BY City, Last_Name

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