Search for a city based off city id - sql

I'm trying to get cities which are equal to a particular city based off a user search I will be implementing .
I've got a sql query below which gives the exact output I want:
Select r.City, AVG(s.Longitude) AS Longitude, AVG(s.Latitude) AS Latitude
From CafeAddress r inner join Cafe s on s.CafeId = r.CafeId
Where City = 'Mumbai'
Group By City
Current output:
City Longitude Latitude
Mumbai -73.9904097 40.7036292
What I'm currently trying to add is a urlsafe "id" which is pretty much the city but with no white spaces, random chars just want it all lower case.
Like below:
id City Longitude Latitude
mumbai Mumbai -73.9904097 40.7036292
Is there a way to implement something like this?

Use LOWER to make it lowercase
Use TRIM to trim whitespace from beginning/end
Use REPLACE to replace interior spaces w/ underscore
Select REPLACE(TRIM(LOWER(r.City)),' ','_'),r.City, AVG(s.Longitude) AS Longitude, AVG(s.Latitude) AS Latitude
From CafeAddress r inner join Cafe s on s.CafeId = r.CafeId
Where City = 'Mumbai'
Group By City
Example, if r.City was ' SAN JOSE '
it would return: 'san_jose'
You can daisy chain REPLACE() to get rid of special characters or use TRANSLATE()

Related

Matching field + "Ctiy"

How would you solve this problem:
You have a table with country names and capitals like this:
tk, name, capital
How would you SELECT name where countries equals capital + "City". So you would get results like Mexico City and Panama City etc?
This question come from the following problem:
"The capital of Mexico is Mexico City. Show all the countries where the capital has the country together with the word "City".
Find the country where the capital is the country plus "City"."
You can concatenate the country's name to ' City' then compare it to its capital in the where clause:
select name
from countries
where capital = name || ' City';

When a statement contains an item in a list, show it in a new column

I would appreciate a little help on some script in sql. So I have a list like the one below and a database table -Table1 with statement as a colum name, and I will like to create a column called location, where the script can search in the statement column and once it finds any of the items in the list in any row it states that in the location column
(Tema, london, Sydney, Germany, China, Africa,)
Statement
-------------------
Going to london
Apples in Tema
Sydney is a city
China is a country
Africa is a continent
In the end I hope to see a table like this :
Statement
location
Going to london
London
Apples in Tema
Tema
Sydney is a city
Sydney
china is a country
China
Africa is a continent
Africa
By using this script,
SELECT Statement,
Case
WHEN Statement::text ~~* '%london%'::character varying::text
THEN 'london'::character varying
ELSE NULL::character varying
END AS location
FROM Table1
I think I would have to write a very tall script, but I was wondering if I could get help with something efficient and quite simple to achieve this
If you have a list of places, you can use that:
select t1.*, v.place
from table1 t1 cross join
(values ('tema'), ('london'), ('sydney'), ('germany'), ('china'), ('africa')
) v(place)
on Statement::text ilike '%' || v.place || '%';
Note: You might want to use regular expressions so you can include work boundaries but your example code doesn't do tis.

DB2: I want to see € or $ in the output of select

This simple query satisfy me
db2 => SELECT city,SUM(sales) as sum from offices group by city;
CITY SUM
---------------------------------------------------------------------------------------------------- ---------------------------------
Rome 14000,
Paris 19000,
But..how to add the $ or € symbol? To get an output like this?
CITY SUM
---------------------------------------------------------------------------------------------------- ---------------------------------
Rome 14000$
Paris 19000$
Normally, this kind of formatting is left to the client to display.
If you really need to have the DB do it, then VARCHAR_FORMAT is likely a better choice.
select city
, varchar_format(sum(sales),'$999G999G990D99' as sales
from offices group by city;
The G and D characters represent the grouping and decimal character for your locale
Solution found using the ||
select city,sum(sales) || '$' as sales from offices group by city;

SQL unknown columns?

Im new to SQL and am having difficulty with this query:
The average elevation of each Texas county which contains one or more zip code with a population over 20,000.
This is what I have so far:
SELECT county, zip_code, state, AVG(elevation)
FROM zip_codes
WHERE state=’TX’
GROUP BY county
HAVING population > 20000
ORDER BY county;
ERROR 1054 (42S22): Unknown column '’TX’' in 'where clause'
But when I select state from the zip_codes database it is there.
You're using wrong single quotes. Should be
WHERE state = 'tx'
instead of
WHERE state = ’tx’
Yours are ... where from? Some editor like MS Word or similar, which creates those "fancy" quotes. Well, don't use them.
Apart from that, columns that aren't aggregated should all be part of the GROUP BY clause:
group by county, zip_code, state
Fixing the quote issues is a must-have, as answered by Littlefoot.
However, your query is still invalid Oracle SQL, since:
the select clause has two non-aggregated columns that are not part of the group by clause (zip_code and state)
same goes for column population in the having clause, that is neither aggregated nor part of the group by clause
For this assignment:
The average elevation of each Texas county which contains one or more zip code with a population over 20,000.
Assuming that each record in the table corresponds to to a different zip_code (which seems relevant, given that the table itself is called zip_codes), you could phrase:
select county, state, avg(elevation) avg_elevation
from zip_codes
where state= 'TX'
group by county, state
having max(population) > 20000
order by county;
SELECT county, zip_code, state, AVG(elevation)
FROM zip_codes
WHERE state='TX'
GROUP BY county, zip_code, state
where population > 20000
ORDER BY county;
Without a data sample it's difficult to understand precisely what's going on, but I'll take a stab at it.
Your assignment says you are to compute "The average elevation of each Texas county which contains one or more zip code with a population over 20,000". Let's break down the elements of the assignment you need to find:
Average elevation
Of each county in Texas
Which has one or more zip code
With population > 20,000
And we've got a table of zip codes, with county, state, and elevation.
So, OK - first problem: find all the zip codes in Texas:
SELECT *
FROM ZIP_CODES
WHERE STATE = 'TX'
OK, easy so far. Now, limit it to zip codes with a population over 20,000:
SELECT *
FROM ZIP_CODES
WHERE STATE = 'TX' AND
POPULATION > 20000
Cool - but this gives us an entry for each zip code in the county with a population > 20000. All we care about are the individual counties which have AT LEAST one zip code with a population over 20K. So how about
SELECT DISTINCT COUNTY
FROM ZIP_CODES
WHERE STATE = 'TX' AND
POPULATION > 20000
Great. Now we need to compute the average elevation in each county returned by the above query:
SELECT COUNTY, AVG(ELEVATION)
FROM ZIP_CODES
WHERE STATE = 'TX' AND
COUNTY IN (SELECT DISTINCT COUNTY
FROM ZIP_CODES
WHERE STATE = 'TX' AND
POPULATION > 20000)
GROUP BY COUNTY
ORDER BY COUNTY
And there's your answer.

I want to trim the names in SQL. for eg. my data has one name as American Airlines and same as "American airlines"

I want to trim the names in SQL. for eg. my data has one name as American Airlines and same as "American airlines.
I am looking forward to write a sql query
select customer_name, requests.
from Table1.
where code = "blue"
--output:
American airlines 25
Alaska Airline 45
American airlines 6
looking for output
American airlines 31
Alaska Airline 45
Can someone please help.:)
Thank you
Use replace for replacing the " with null
REPLACE(string1, string_to_replace, replacement_string)
e.g.
select replace(customer_name,'"','') as cusname ,sum(requests)
.....
group by cusname
Have you tried group by with sum ??
select replace(customer_name,'"','') as customer_name, sum(requests)
from Table1.
where code = "blue"
group by replace(customer_name,'"','');