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.
Related
I have a query that can return the intended value but only 1 row. I need to have at least 26 rows of the values due based on the having clause.
Town
floor_area_sqm
resale_value
toronto
30
4500
chicago
44
300
toronto
22
3000
sydney
54
3098
pittsburg
102
2000
sydney
101
2000
pittsburg
129
2000
SELECT town, floor_area_sqm, resale_price
FROM X.flat_prices as X
GROUP BY town
HAVING Min(floor_area_sqm) = (SELECT MIN(floor_area_sqm) FROM X.flat_prices HAVING MAX(resale_price));
By using the formula above I get this:
Town
floor_area_sqm
resale_value
chicago
44
300
So the answer should show something like the following:
Town
floor_area_sqm
resale_value
chicago
44
300
toronto
22
3000
sydney
54
3098
pittsburg
102
2000
It should pull the lowest sqm for the town with the highest resale value. I got 26 towns and a database of over 200k.
I would like to replicate with MAX sqm using the same formula. Is join the way/only way to do it?
Use a subquery to get the minimum sqm for each town. Join that with the table to get all the properties with that sqm. Then get the maximum resale value within each of these groups.
SELECT t1.town, t1.floor_area_sqm, MAX(t1.resale_value) AS resale_value
FROM flat_prices AS t1
JOIN (
SELECT town, MIN(floor_area_sqm) AS floor_area_sqm
FROM flat_prices
GROUP BY town
) AS t2 ON t1.town = t2.town AND t1.floor_area_sqm = t2.floor_area_sqm
GROUP BY t1.town, t1.floor_area_sqm
DEMO
In MySQL 8.0 you can do it in one query with window functions, but I still haven't learned to use them.
This one goes for the highest resale price , then choose the one with the lowest sqm if multiple choices exist.
select t1.town ,min(floor_area_sqm) mi_sqm,resale_value from flat_prices t1
join
(select town,max(resale_value) mx_value from flat_prices group by town) t2
on t1.town=t2.town and resale_value=mx_value
group by town
;
I am trying to learn SQL. I have three different tables. All the tables have one common column. I am showing a very small portion of sample data.
I am trying to understand how to read from a parent table after a child table has been updated.
Table_1 (booking platform info)
booking_date column2 column3 column4 cust_country_id hotel_id booking_value
22-mar-2016 .................. 1001 1 $150
01-apr-2016 .................. 1002 2 $500
09-apr-2016 .................. 1001 2 $222
17-apr-2016 .................. 1002 4 $75
19-apr-2016 .................. 1003 1 $690
03-May-2016 ................., 1001 3 $301
Table_2 (hotel information)
hotel_id hotel_name hotel_country
1 Marriott Germany
2 Novotel France
3 Oberoi India
4 Osaka Japan
Table_3 (customer information)
country_id country_name
1001 India
1002 France
1003 Japan
My question is if a new hotel Hyatt from France is added to Table_2 (hotel information), how can I find out the first date at which a French customer booked that hotel?
I am getting a bit confused on how to approach this; as it has to take values from all 3 tables.
I think in this case you should write mysql query to find the oldest booking_date for customer from France and hotel with name Hyatt
select booking_date from Table_1
join Table_2 on Table_2.hotel_id = Table_1.hotel_id
join Table_3 on Table_3.country_id = Table_1.cust_country_id
where Table_3.country_name = 'France' and Table_2.hotel_name = 'Hyatt'
order by Table_1.booking_date asc
limit 1
I am new to SQL and I'm having difficulties writing the following query.
Scenario
A user has two addresses, home address (App\User) and listing address (App\Listing). When a visitor searches for listings for a Suburb or postcode or state, if the user's listing address does not match - but if home address does match - they will be in the search result too.
For example: if a visitor searches for Melbourne, I want to include listings from Melbourne and also the listings for the users who have an address in Melbourne.
Expected output:
user_id first_name email suburb postcode state
1 Mathew mathew.afsd#gmail.com Melbourne 3000 VIC
2 Zammy Zamm#xyz.com Melbourne 3000 VIC
Tables
users:
id first_name email
1 Mathew mathew.afsd#gmail.com
2 Zammy Zamm#xyz.com
3 Tammy tammy#unknown.com
4 Foo foo#hotmail.com
5 Bar bar#jhondoe.com.au
listings:
id user_id hourly_rate description
1 1 30 ABC
2 2 40 CBD
3 3 50 XYZ
4 4 49 EFG
5 5 10 Efd
addresses:
id addressable_id addressable_type post_code suburb state latitude longitude
3584 1 App\\User 2155 Rouse Hill NSW -33.6918372 150.9007221
3585 2 App\\User 3000 Melbourne VIC -33.6918372 150.9007221
3586 3 App\\User 2000 Sydney NSW -33.883123 151.245969
3587 4 App\\User 2008 Chippendale NSW -33.8876392 151.2011224
3588 5 App\\User 2205 Wolli Creek NSW -33.935259 151.156301
3591 1 App\\Listing 3000 Melbourne VIC -37.773923 145.12385
3592 2 App\\Listing 2030 Vaucluse NSW -33.858935 151.2784079
3597 3 App\\Listing 4000 Brisbane QLD -27.4709331 153.0235024
3599 4 App\\Listing 2000 Sydney NSW -33.91741 151.231307
3608 5 App\\Listing 2155 Rouse Hill NSW -33.863464 151.271504
Try this. You can check it here.
SELECT l.*
FROM listings l
LEFT JOIN addresses a_l ON a_l.addressable_id = l.id
AND a_l.addressable_type = "App\\Listing"
AND a_l.suburb = "Melbourne"
LEFT JOIN addresses a_u ON a_u.addressable_id = l.user_id
AND a_u.addressable_type = "App\\User"
AND a_u.suburb = "Melbourne"
WHERE a_l.id IS NOT NULL OR a_u.id IS NOT NULL
As per my understanding of your question, for any suburb - supplied by a visitor, you want to include all the listings where either User's address is the same as the suburb supplied or the Listing's address is the same as the suburb supplied.
Assuming addressable_id column is related to Id of Users table and Listings table, based on value in addressable_type column, you can use the following query to join and get the desired result:
Select l.*
From Listings l
inner join Addresses a on ((a.addressable_id = l.user_Id and a.addressable_type = 'App\\User') or (a.addressable_id = l.Id and a.addressable_type = 'App\\Listings'))
inner join Addresses a1 On a1.addressable_id = a.addressable_id and a1.Suburb = 'Melbourne'
try this,
SELECT
a.addressable_id AS `userid`,
b.first_name AS `username`
FROM
addresses AS a JOIN users AS b ON a.addressable_id=b.id
WHERE
a.suburb = 'Melbourne';
if < addressable_id > has relation with < id > in listing table,
SELECT
a.addressable_id AS `userid`,
b.first_name AS `username`
FROM
addresses AS a JOIN users AS b ON a.addressable_id=b.id AND addressable_type='App\\User'
WHERE
a.suburb = 'Melbourne'
UNION
SELECT
b.user_id AS `userid`,
c.first_name AS `username`
FROM
addresses AS a JOIN listings AS b ON a.addressable_id=b.id AND addressable_type='App\\Listing'
JOIN users AS c ON b.user_id=c.id
WHERE
a.suburb = 'Melbourne';
i am new to this sql. i just want to ask doubt regarding sql.( am using sql server 2012)
two tables:
states:
select * from [dbo].[States]
StateId StateName
1 Odisha
2 West Bengal
3 Bihar
4 Jharkhand
district: select * from [dbo].[Districts]
DistrictId StateId DistrictName
1 1 Mayurbhanj
2 1 Keonjhar
3 1 Khorda
4 1 Balasore
5 2 Hoogly
6 2 Howrah
7 2 Jalpaiguri
8 3 Aurangabad
9 3 Patna
10 4 Bokaro
i have tried this join query
select s.StateName, d.DistrictName from states s join Districts d on s.stateid=d.stateid
and got output like this
stateName districtName
Odisha Mayurbhanj
Odisha Keonjhar
Odisha Khorda
Odisha Balasore
West Bengal Hoogly
West Bengal Howrah
West Bengal Jalpaiguri
Bihar Patna
Jharkhand Bokaro
Bihar Aurangabad
but i want output like this
Odisha //statename
Mayurbhanj
Keonjhar //district names
Khorda
Balasore
West Bengal //statename
Hoogly
Howrah
Jalpaiguri
Bihar //statename
Aurangabad
Patna
Jharkhand //statename
Bokaro
in a same column....
is it possible to do using query or store procedure?? please help me to sort it out
If you really want to do it in SQL, here is one way using UNION ALL:
SELECT Name
FROM (
SELECT 0 AS DistrictId, StateId, StateName AS Name FROM states
UNION ALL
SELECT * FROM district
)AS t
ORDER BY StateId, DistrictId
If you are using php, you can use main query to populate states with while loop. Then add another sub query to populate districts under each state, using another while loop inside the main loop. I hope this will help you to get it. Regards !
You can use GROUP_CONCAT if you want to get the result in a single Query
SELECT s.StateName
,GROUP_CONCAT(d.DistrictName)
FROM states s
INNER JOIN district d ON s.StateId = d.StateId
GROUP BY s.StateId
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