I'm new to SQL and my current sql query is:-
SELECT
CONCAT(LastName,', ',FirstName)AS 'Name',</li>
City,
Country,
ShipCity AS 'Shipped City'
From
itstudies.employees
INNER JOIN
Orders
ON Employees.EmployeeID = Orders.EmployeeID
WHERE City = ShipCity;
This query shows the output as:-
(Only part of the output is shown.)
Name City Country Shipped City
Smith, Jo York UK York
Avery, Paul Dallas USA Dallas
Avery, Paul Dallas USA Dallas
Kris, Jan York UK York
Kris, Jan York UK York
Hill, Ros Boston USA Boston
I need to take out the duplicates and change the query to show:-
Name City Country Shipped City
Smith, Jo York UK York
Avery, Paul Dallas USA Dallas
Kris, Jan York UK York
Hill, Ros Boston USA Boston
Thanks in advance.
You could just use
SELECT DISTINCT ...
but usually it is better to have a look at where the duplicates come from and work with a
GROUP BY
In this case you join with ORDERS and thus get duplicates for all orders of an employee. Is this join necessary? You could use
WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.EmployeeId=Employees.EmployeeId)
to get only employees who ordered.
Simple: Use the DISTINCT keyword.
You can refer to http://www.w3schools.com/sql/sql_distinct.asp for more details ;)
Related
The table name is c_list.
Country City Rating Date
------------------------------------
France Brest 95 24092016
France Brest 98 27092016
France Brest 95 03102016
France Lille 100 26092016
France Lille 92 28092016
Japan Tokyo 98 02102016
There are more than 50 different countries and each country have few cities. And each city may have more than one row of record or more. I want to select one City with highest average Rating (Compare to Cities in it's own country) and then compare with all other cities in different countries. So, the final query should display all Country and their ONE City with max(avg(Rating)) and in desc order. The sample output:
Country City max(avg(rating))
-------------------------------------
USA New York 97.25
UK Cardiff 96.70
Germany Greven 96.50
Turkey Afyon 94.88
France Guipavas 94.10
Canada Cartwright 91.35
I can only get the max(avg(rating)) for one country. Need help.
SELECT top 1 country, city, Avg(rating) AS Ratings
FROM c_list
where country = 'France'
GROUP BY city, country
order by Ratings desc
(Edited) The result that I want is similar like Miss world contest. Compete and win against local contestant in your country first. Next (my final result set) is to compete against the winners from other countries and rank them first to last using their avg(rating) that they got eatlier in their country.
If am not wrong you are looking for this
SELECT country,
city,
Avg(rating) AS Ratings
FROM c_list A
GROUP BY city,
country
HAVING Avg(rating) = (SELECT TOP 1 Avg(rating) AS Ratings
FROM c_list B
WHERE a.country = b.country
GROUP BY city
ORDER BY ratings DESC)
ORDER BY ratings DESC
Note : If you are using Mysql the replace TOP keyword with LIMIT
Base table:
select t.* from temp_lax t
COUNTRY CITY RATING
1 France Brest 95
2 France Brest 98
3 France Brest 95
4 France Lille 100
5 France Lille 92
6 Japan Tokyo 98
Query:
select t1.country, t1.city, Avg(t1.Rating) rating
from temp_lax t1
group by t1.country, t1.city
having avg(t1.rating) = (select max(avg(rating))
from temp_lax t2
WHERE t1.country = t2.country
GROUP BY t2.city)
order by rating desc
OUTPUT:
COUNTRY CITY RATING
1 Japan Tokyo 98
2 France Lille 96
3 France Brest 96
Please let me know if you are looking for different result set.
So basically there are two tables.
First is location table -
id location_id city_name country_name
1 400212 Paris Canada
2 400122 Paris France
2 400122 Dubai United Arab Emirates
Second is general_details table -
id hotel_id city_name country_name
1 2323 Dubai United Arab Emirates
2 1231 Dubai United Arab Emirates
3 2344 Paris Canada
4 1218 Paris France
So lets suppose I have two country_name and city_name pairs from table locations. I want to select number of rows for them each from table general details. Like I have Paris => Canada and Paris => France, so my result set should have 1, 1 i.e. their respective total records from table general_details. I am not sure how to do this. I can have multiple pairs from table 1 and I want all the counts from table 2.
Like I made an array of city and country -
array[0][city] = Paris, array[0][country] = France
array[1][city] = Paris, array[1][country] = Canada
Now I want output resultset after 2nd query to have count(*) as 1,1.
Kindly guide me here.
Try This:
SELECT city_name,country_name, COUNT(*)
FROM
general_details
WHERE
city_name IN('Paris') AND country_name IN ('Canada' , 'France')
GROUP BY city_name,country_name
SQL Query:
Aim:
Write a SQL query to retrieve the address of highest rated Phills
Coffee in United States addressing format.
id name house street city state zip country rating
1 Best Buy 34 Main St Carson CA 98064 USA 9
2 Phills Coffee 4568 Sepulveda Blvd Torrance CA 50833 USA 6
3 Starbucks 3 Ocean Blvd Long Beach WA 45093 USA 9
4 Phills Coffee 214 Carson St Huntington Beach PA 89435 USA 4
US Addressing Format (For people outside USA):
http://bitboost.com/ref/international-address-formats/united_states/
My attempt:
SELECT house, street, city,
state,country,zip
FROM table
WHERE name="Phills Coffee"
ORDER BY rating DESC LIMIT 1
Am I doing wrong? Or How can I improve this query?
Thanks,
US address format would be like:
4568 Sepulveda Blvd, Torrance, CA 50833 USA.
So your select would look like:
SELECT CONCAT(house, ' ', street, ', ', city, ', ', state, ' ', zip, ' ', country)
FROM table
WHERE name="Phills Coffee"
ORDER BY rating DESC LIMIT 1
You've been asked to retrieve the address, and in United States addressing format. So use that :
SELECT CONCAT(street,' ','country', ',') ...
Dont know what is the US addressing format, but use concat to get it done. Your WHERE condition and ORDER BY are OK
You will have to use Max(rating) As Highest Rating and Group By for this,
SELECT house, street, city,
state,country,zip,Max(rating)
FROM table
Group By house, street, city,
state,country,zip
Having name="Phills Coffee"
ORDER BY rating DESC LIMIT 1;
I hope it works out for you, I am sorry if I wasn't any help..
SELECT house, street, city, state, country, zip, rating
FROM table WHERE rating = (SELECT MAX(rating)
from table WHERE name = "Phills Coffee")
AND name= "Phills Coffee";
This should return:
4568 Sepulveda Blvd Torrance CA 50833 USA 6
Note that you can omit rating from the first line of this query and it will return the address only (no 6, but will still select the max rating's info)
I have this query result and I would like the second row for each id to be added as a
SELECT m.post_id,m.meta_value,p.post_title
from events_posts p
inner join events_postmeta m
on p.ID=m.post_id and p.post_type='event' and (p.post_status='publish' or p.post_status='recurring')
and(m.meta_key='address' or m.meta_key='st_date')
order by m.post_id
column.
The results right now :
post_id meta_value post_title
15 Carolina Beach Road, Wilmington, NC, United States An Art Exhibition
15 2014-02-03 An Art Exhibition
19 Dakota Street, Winnipeg, MB, Canada Weekly Karate Classes
19 2014-02-06 Weekly Karate Classes
23 Alaskan Way, Seattle, WA, United State Christmas Carnival
23 2014-02-03 Christmas Carnival
I would like it to be
post_id | meta_value| post_title
15 | Carolina Beach Road, Wilmington, NC, United States | An Art Exhibition | 2014-02-03
19 |Dakota Street, Winnipeg, MB, Canada |Weekly Karate Classes |2014-02-06
23 |Alaskan Way, Seattle, WA, United State |Christmas Carnival |2014-02-03
You are actually not trying to convert rows to columns. You are trying to merge two rows of your query into one. This behaviour should be possible through joining the events_postmeta twice.
SELECT p.ID, m1.meta_value as event_address, m2.meta_value as event_date, p.post_title
from events_posts p
inner join events_postmeta m1
on p.ID=m1.post_id and p.post_type='event' and (p.post_status='publish' or p.post_status='recurring')
and m1.meta_key='address'
inner join events_postmeta m2
on p.ID=m2.post_id and p.post_type='event' and (p.post_status='publish' or p.post_status='recurring')
and m2.meta_key='st_date'
order by p.ID
That way the m1-join adds the address as a column and the m2-join adds the date. I could not test this but I am pretty sure it works (modulo some typing syntax mistakes).
I hope you can help me with an SQL statment that I can't seem to figure out.
I have a table with a list of visits made, with the country visited and the dates such as;
United Kingdom, 1st Nov 2009
Germany, 8th June 2010
Frane, 10th September 2011
United Kingdom, 11th october 2011
etc.
I want to extract the data so that I get a table list such as follows
Times Visited - Country list
23 - United Kingdom
10 - France, Germany
4 - Czech Republic, USA, Canada
1 - Poland, Serbia, Argentina, New Zealand
So that the data shows that I have made 4 visist to the Czech Republic, USA & Canda
And the query
select * from
(
select count(*) as "trips", country from trip_view
group by country
order by 1 desc
) as a
returns the data
23 United Kingdom
10 France
10 Germany
4 Czech Republic
4 USA
4 Canada
etc.
So I need a kind of group on the outer SQL, but if I do the following
select * from
(
select count(*) as "trips", country from trip_view
group by country
order by 1 desc
) as a
group by trips
Then I only get 1 entry for each country. i.e.
23 United Kingdom
10 France
4 Czech Republic
etc.
So for each row I only have 1 country listed and not all of them. i.e. Row 2 should show France & Germany and row 3 should show Czech, USA & Canada
Any ideas how to do this in mysql?
Thanks
Using GROUP_CONCAT on the country column it should yield the result you want:
SELECT trips,
GROUP_CONCAT(country) AS country
FROM (
SELECT COUNT(*) AS "trips",
country
FROM trip_view
GROUP BY country
) a
GROUP BY trips
ORDER BY 1 DESC
Output:
TRIPS COUNTRY
23 United Kingdom
10 France,Germany
4 Czech Republic,Canada,USA
Live DEMO.