i need to join tables to see which product is cheaper in terms of their continent. i have provided more detail below.
I have the following four tables:
country Product1 Product2 Product3
Austria 0.01 0.011 0.018
Cuba 0.15 0.015 0.012
Angola 0.17 0.017 0.019
Benin 0.84 0.015 0.025
Belize 0.24 0.019 0.018
where table name is PriceComparison
Code name continent_code
AD Andorra EU
AF Afghanistan AS
AI Anguilla NA
AL Albania EU
AO Angola AF
table name is countries
Id Name countrycode
1 Albania AL
2 Algeria DZ
3 Andorra AD
4 Angola AO
5 Austria AT
table name is CountryList
code name
AF Africa
AN Antarctica
AS Asia
EU Europe
NA North America
OC Ocenia
SA South America
table name is continents
Code name continent_code
AD Andorra EU
AF Afghanistan AS
AI Anguilla NA
AL Albania EU
AO Angola AF
table name is countries
Id Name countrycode
1 Albania AL
2 Algeria DZ
3 Andorra AD
4 Angola AO
5 Austria AT
table name is CountryList
code name
AF Africa
AN Antarctica
AS Asia
EU Europe
NA North America
OC Ocenia
SA South America
table name is continents
I need to update my PriceComparison with all the other tables i have. The objective here is to see which continent we are cheaper in terms of our products. Product1 is my product. Product1's price needs to be compared with product2's price and again product1's price needs to be compared to product3's price in terms of where my product is cheaper per continent.
Can someone assist with this query?
Tried so far:
select countries.continent_code,
continents.code,
CountryList.countrycode,
countries.code,
PriceComaprison.country,
CountryList.name
from CountryList as CL
inner join countries
on countries.continent_code = continents.code
inner join CL
on CL.countrycode = countries.code
inner join PriceComparison
on PriceComparison.country = CL.name
sample data:
countries: code (AS, AI,AO, BB,CA) name (the actual country name - eg.America Samao) continent_code (OC,NA,AN - the actual continent_code of the country)
CountryList table: id Name countrycode
continents: code name
Based on what you explained in the comments, this could be close to what you need:
select con.code continenct_code, con.name as continent, product1, Product2, product3
from PriceComparison p join countries c on p.country = c.name
join continents con on con.code = c.continent_code
where Product1 < Product2 and Product1 < Product3
However, the sample data you have provided is not correct. As it is already mentioned in the comments to your question, there are countries in the PriceComparison table that do not exist in the Countries table-if you run this with your real data I would assume this should work.
I did modify a little your data and you can check it here.
This query will show the average price of each product per continent, if you want the smallest price per continent use MIN() instead of AVG()
SELECT con.code,
AVG(p.product1),
AVG(p.product2),
AVG(p.product3)
FROM PriceComparison p
JOIN countries c ON p.country = c.name
JOIN continents con ON con.code = c.continent_code
GROUP BY con.code
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
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 have two tables on MySQL (using phpMyAdmin), looking like the following:
Table 1:
Country Total Minutes
USA 100
USA 90
Canada 60
Mexico 80
UK 90
France 70
France 10
Germany 10
In Table 2, what I need to do is the following:
Region Total Minutes
North America USA+USA+Canada+Mexico Mins
Europe UK+France+France+Germany Mins
Is there a way to have a row be the result of a query?
You either need a region column in table 1:
SELECT region, SUM(`Total Minutes`)
FROM timespent
GROUP BY region;
Or a separate region <-> country table:
SELECT region, SUM(`Total Minutes`)
FROM myregions r
INNER JOIN timespent t USING (country)
GROUP BY r.region;
The regions table would look like this:
region | country
--------------+--------
North America | USA
North America | Mexico
If you can't change anything in your database, look at Andomar's solution :)
You could translate the countries to regions in a subquery. The outer query can then group by on region:
select Region
, sum(TotalMinutes) as TotalMinutes
from (
select case country
when 'USA' then 'North America'
when 'France' then 'Europe'
end as Region
, TotalMinutes
from YourTable
) as SubQueryAlias
group by
Region
Sample data:
LOCATION NAME LABEL1 LABEL2 SERVICE TIME
NY Andrew A B HOUSE 2555
NY Andrew A B CAR 35
NJ Copley C A HOUSE 1025
NY Copley A B HOUSE 650
VA Dalton D C PET 25
What I want to do is add another column where in it shows sum(Time) of rows with same data except for the Service.Also, the services that I need are only the sum of car and house.Is this possible? If not can you help me with the right query
Sample output I need:
LOCATION NAME LABEL1 LABEL2 SERVICE TIME SUM
NY Andrew A B HOUSE 2555 **2590**
NY Andrew A B CAR 35
NJ Copley C A HOUSE 1025 1025
NY Copley A B HOUSE 650 650
SELECT `LOCATION`, `NAME`, `LABEL1`, `LABEL2`, SUM(`TIME`)
FROM `myTable`
WHERE `SERVICE` = "CAR" OR `SERVICE` = "HOUSE"
GROUP BY `LOCATION`, `NAME`, `LABEL1`, `LABEL2`
This does not add another column, but it does return the data you requested in a resultset when run as a query. I recommend taking this approach.
You should also ensure that your indexes are set up optimally for this sort of query.