mysql query hotel availability (Not calculate the total price) - mysql

Search the list of accommodation and the total price..
Here is my query and output: http://sqlfiddle.com/#!2/eaf58/43
query:
SELECT count(candidates.accommodation_id) as total, candidates.accommodation_id, omc_accommodation_season_period.season_id,
room_1, r1_price.price_normal AS room_1_price,
(SELECT SUM(room1_price.price_normal)
FROM omc_accommodation_room_pricelist room1_price
INNER JOIN omc_accommodation_room room1
ON room1_price.accommodation_id = room1.accommodation_id
INNER JOIN omc_accommodation_season_period so1
ON room1_price.accommodation_id = so1.accommodation_id
AND room1_price.season_id = so1.season_id
INNER JOIN omc_accommodation accommodation1
ON room1_price.accommodation_id = accommodation1.id
WHERE
so1.date BETWEEN '2013-08-13' AND '2013-08-15' AND
room1.quantity >= 3 AND room1.pax_max = 1 AND
accommodation1.id = candidates.accommodation_id AND
room1.id = room_1) AS room_1_sum,
room_2,r2_price.price_normal AS room_2_price,
omc_accommodation_season_period.date
FROM (SELECT r1.accommodation_id, r1.id AS room_1,
r2.id AS room_2, r1.quantity AS room_1_quantity,
r2.quantity AS room_2_quantity
FROM
omc_accommodation_room r1
INNER JOIN
omc_accommodation_room r2 ON r1.accommodation_id = r2.accommodation_id
WHERE
r1.quantity >= 3 AND r2.quantity >= 4 AND r1.pax_max = 1 AND r2.pax_max = 2) AS candidates
LEFT JOIN omc_accommodation_season_period
ON candidates.accommodation_id = omc_accommodation_season_period.accommodation_id
AND date BETWEEN '2013-08-13' AND '2013-08-15'
AND omc_accommodation_season_period.date <> 0
LEFT JOIN omc_accommodation_room_extra_quota eq1
ON (candidates.accommodation_id = eq1.accommodation_id
AND room_1 = eq1.room_type_id
AND eq1.date=omc_accommodation_season_period.date)
LEFT JOIN omc_accommodation_room_extra_quota eq2
ON (candidates.accommodation_id = eq2.accommodation_id
AND room_2 = eq2.room_type_id
AND eq2.date=omc_accommodation_season_period.date)
LEFT JOIN omc_accommodation
ON candidates.accommodation_id = omc_accommodation.id
LEFT JOIN omc_accommodation_room_pricelist r1_price
ON (candidates.accommodation_id = r1_price.accommodation_id
AND room_1= r1_price.room_type_id)
LEFT JOIN omc_accommodation_room_pricelist r2_price
ON (candidates.accommodation_id = r2_price.accommodation_id
AND room_1 = r2_price.room_type_id)
WHERE
r1_price.season_id = omc_accommodation_season_period.season_id
AND
r2_price.season_id = omc_accommodation_season_period.season_id
GROUP BY candidates.accommodation_id, omc_accommodation_season_period.date
Problem is just to calculate the total price of each room..
Help me fix it up..
Many thanks in advance

SELECT rsp1.date, rsp1.season_id, r1.hotel_id, r1.id AS room_1, rp1.price_normal AS room_1_price, SUM(rp1.price_normal) AS room_1_total, r2.id AS room_2, rp2.price_normal AS room_2_price
FROM
omc_hotel_room r1
LEFT JOIN omc_hotel_room r2 ON r1.hotel_id = r2.hotel_id
INNER JOIN omc_hotel_room_pricelist rp1 ON r1.hotel_id = rp1.hotel_id AND r1.id = rp1.room_type_id
INNER JOIN omc_hotel_room_pricelist rp2 ON r1.hotel_id = rp1.hotel_id AND r2.id = rp2.room_type_id
LEFT JOIN omc_hotel_season_period rsp1 ON r1.hotel_id = rsp1.hotel_id
WHERE
r1.pax_max = 1 AND
rp1.price_normal <> 0 AND
r2.pax_max = 2 AND
rp2.price_normal <> 0 AND
rsp1.date BETWEEN '2013/08/13' AND '2013/08/15'
GROUP BY r1.hotel_id, rsp1.date

Related

MySQL query with multiple INNER JOIN returns duplicate entries in the result

I have the following data structure:
There are multiple tables:
I need the following return: sum of balance (sum(a.balance) as pnl) from tabposition.
However, this query returns duplicate results. Can anybody help me with this? Thanks
SET #dt_inicio := '2021-12-01';
SET #dt_fim := '2021-12-09';
select a.data, a.fund, 'CASH-USD' as ticker , 'CASH' as strategy, 'Dolar Cash' as company,
'Others' as type, 'Others' as sector,
sum(a.balance) as pnl from tabposition as a
inner join tablistaativos as c on a.ticker = c.ticker
inner join tabdiautil as d on a.data = d.data_util
inner join tabhistorynav as e on a.data = e.data
inner join tabhistorynav as f on d.data_util_ant = f.data
inner join tabprice as g on a.data = g.data
inner join tabprice as i on d.data_util_ant = i.data
inner join tabhistorynav as j on d.data_util_ant = j.data and a.fund = j.fund
inner join tabfatorx as l on a.data = l.data_pos
where a.data >= #dt_inicio and a.data <= #dt_fim and a.fund = 'TORK FUND' and a.qnt <> 0
and (c.class = 'ACCO' or c.class = 'ACCOUNTING' or c.class = 'SPAC' or c.class = 'ACOES' or
c.class = 'OPCOES' or c.class = 'FUNDO')
and e.official = 'Yes' and e.fund = 'TORK FIA' and f.official = 'Yes' and f.fund = 'TORK FIA'
and g.ticker = 'usdbrl' and i.ticker = 'usdbrl'
and l.cotista = 'TORK FIA'
group by a.data
order by a.data

How to optimize join query with sub query

How can I optimize this join query? how can I get the latest start_dt in this query? Is any way can make it faster to load and the start_dt is the latest one? If I delete the subquery for this case, I will get the record which the start_dt is not the latest one. Please Help!!
SELECT
matrix.matrix_uuid,
matrix.name AS matrix_name,
courses.courses_uuid,
courses.name AS courses_name,
employees.employees_uuid,
CONCAT(employees.first_name,
' ',
employees.last_name) AS Name,
courses.validity,
courses.duration,
(SELECT
MAX(start_dt)
FROM
courses_schedule C
WHERE
C.courses_schedule_uuid = CS.courses_schedule_uuid) AS start_dt,
COUNT(courses.courses_uuid) AS refresher,
courses.courses_refresher_uuid,
courses_taken.overall_status,
courses_taken.status
FROM
employees
INNER JOIN
courses_taken ON courses_taken.employees_uuid = employees.employees_uuid
LEFT JOIN
courses_schedule CS ON CS.courses_schedule_uuid = courses_taken.courses_schedule_uuid
LEFT JOIN
matrix_courses matrix_courses ON matrix_courses.courses_uuid = CS.courses_uuid
LEFT JOIN
matrix ON matrix.matrix_uuid = matrix_courses.matrix_uuid
LEFT JOIN
courses ON courses.courses_uuid = matrix_courses.courses_uuid
WHERE
CS.start_dt = (SELECT
MAX(start_dt)
FROM
courses_taken CT
LEFT JOIN
courses_schedule ON courses_schedule.courses_schedule_uuid = CT.courses_schedule_uuid
LEFT JOIN
matrix_courses matrix_courses ON matrix_courses.courses_uuid = courses_schedule.courses_uuid
LEFT JOIN
roles_courses ON roles_courses.courses_uuid = matrix_courses.courses_uuid
LEFT JOIN
matrix M ON M.matrix_uuid = matrix_courses.matrix_uuid
LEFT JOIN
courses C ON C.courses_uuid = matrix_courses.courses_uuid
WHERE
C.courses_uuid = courses.courses_uuid
AND M.matrix_uuid = matrix.matrix_uuid
GROUP BY matrix.matrix_uuid , courses.courses_uuid)
AND courses_taken.overall_status = 1
AND employees.status = 2
AND IFNULL(employees.STATUS, 1) != 0
GROUP BY matrix.matrix_uuid , courses.courses_uuid , employees.employees_uuid

mysql joins to return perfect result

I have bought two courses VLSI electronics and VLSI extc which were
bought together so it has same order id
The query returns result when we searched for vlsi or electronics but
doesn't result anything when search in the filterextc.
SELECT `i.*,r.emailid,r.first_name,u.university_name,ct.country_name,st.state_name, cy.city_name,r.address,r.mobile,COUNT( k.order_id ) as order_cnt, k.product_type AS product_type,k.product_id AS product_id,tx.tax_amount,str.stream_name,fie.field_name,sems.semester_number,colg.college_name,`
IF( product_type = "course", (SELECT course_title FROM tbl_courses WHERE course_id = product_id AND course_title Like "%vlsi%"), 0 ) AS coursebought,
IF(product_type = "module",(SELECT module_title FROM tbl_modules WHERE module_id = product_id ), 0 ) AS modulebought
FROM (`tbl_orders` AS i)
LEFT JOIN tbl_users AS r ON i.user_id = r.user_id
LEFT JOIN tbl_discount_history AS hd ON i.order_id = hd.order_id
AND i.discount_id = hd.discount_id
AND hd.status = "Active"
LEFT JOIN tbl_shopping_cart AS k ON i.order_id = k.order_id
LEFT JOIN tbl_university AS u ON u.university_id = r.university_id
LEFT JOIN tbl_country AS ct ON u.country_id = ct.country_id
LEFT JOIN tbl_states AS st ON u.state_id = st.state_id
LEFT JOIN tbl_city AS cy ON u.city_id = cy.city_id
LEFT JOIN tbl_finalorder_tax_details AS tx ON i.order_id = tx.order_id
LEFT JOIN tbl_stream AS str ON r.stream_id = str.stream_id
LEFT JOIN tbl_fields AS fie ON r.field_id = fie.field_id
LEFT JOIN tbl_semester_type AS sems ON r.semester_id = sems.semester_id
LEFT JOIN tbl_colleges AS colg ON r.college_id = colg.college_id
and ct.country_id = cy.city_country_id
and st.state_id = cy.city_state_id
WHERE tx.tax_id = 1 and r.status = "Active" And 1=1 AND product_id IS NOT NULL
AND product_type = "course"
GROUP BY order_id
having coursebought IS NOT NULL and modulebought IS NOT NULL
ORDER BY order_id desc
limit 0,10

How to order by number of sold products during a date?

I need to order a result by number of sold products.
Here is an extract from my (complex) SQL:
SELECT ....
SUM(products.quantity) as numberSold,
....
ORDER by numberSold
How can I order by sold items ONLY during this year ?
I tried with
HAVING products.products.date>'01-01-2015', but it affcts the number of returned rows.
Here is the indigest SQL I did not write and that I have to change:
Select Distinct
boutique_produit.*,
boutique_produit_description.*,
boutique_produit_plus.*,
Sum(boutique_commande_detail.quantite) As numberSold
From
boutique_categorie Inner Join
boutique_categorie_produit On boutique_categorie_produit.boutique_categorie_id
= boutique_categorie.id_boutique_categorie Inner Join
boutique_produit On boutique_produit.id_boutique_produit =
boutique_categorie_produit.boutique_produit_id And
boutique_produit.zone_id = boutique_categorie_produit.zone_id Inner Join
boutique_produit_description
On boutique_produit_description.boutique_produit_id =
boutique_produit.id_boutique_produit And
boutique_produit_description.zone_id = boutique_categorie_produit.zone_id
And (boutique_produit_description.boutique_langue_disponible_code = 'FR')
Inner Join
boutique_produit_reference On boutique_produit_reference.boutique_produit_id =
boutique_produit.id_boutique_produit And boutique_produit_reference.zone_id
= boutique_categorie_produit.zone_id Inner Join
boutique_produit_reference_prix
On boutique_produit_reference_prix.boutique_produit_reference_id =
boutique_produit_reference.id_boutique_produit_reference And
boutique_produit_reference_prix.zone_id = boutique_categorie_produit.zone_id
Inner Join
boutique_taxe On boutique_taxe.id_boutique_taxe =
boutique_produit_reference.boutique_taxe_id Inner Join
boutique_produit_plus On boutique_produit_plus.boutique_produit_id =
boutique_produit.id_boutique_produit And boutique_produit_plus.zone_id =
boutique_categorie_produit.zone_id Inner Join
boutique_produit_plus_categories
On boutique_produit_plus_categories.boutique_produit_id =
boutique_produit_plus.boutique_produit_id And
boutique_produit_plus_categories.zone_id = boutique_produit_plus.zone_id And
(boutique_produit_plus_categories.categorie_id In (1750, 1227, 1880))
Inner Join
poi On boutique_produit_plus.poi_id = poi.ID_poi And poi.zone_id =
boutique_categorie_produit.zone_id And (((poi.payant = 1 And
('2015-12-10' >= poi.dateDebutValidite) And ('2015-12-10' <=
poi.dateFinValidite)) Or (poi.illimite = 1))) Inner Join
boutique_professionnel On poi.boutique_professionnel_id =
boutique_professionnel.id_boutique_professionnel And
boutique_professionnel.zone_id = poi.zone_id And
(boutique_professionnel.compte_actif = 1) Left Join
boutique_commande_detail
On boutique_commande_detail.boutique_produit_reference_id =
boutique_produit_reference.id_boutique_produit_reference And
boutique_commande_detail.zone_id = boutique_categorie_produit.zone_id
Where
boutique_categorie_produit.boutique_categorie_id = 382 And
(boutique_produit_plus.date_fin_valid = '' Or
boutique_produit_plus.date_fin_valid Is Null Or
boutique_produit_plus.date_fin_valid >= '2015-12-10 23:59:59') And
boutique_produit.produit_actif = 1 And
boutique_categorie_produit.zone_id = 4
Group By
boutique_produit.id_boutique_produit, boutique_produit.zone_id
Order By
numberSold Desc
Limit 0, 60
numberSold is the IMPORTANT field
use the WHERE clause on products.date
If I understand you correctly, you want to sum products.quantity only when products.date>'01-01-2015', and if the date is lower you want to exclude that quantity? Then try CASE.
SELECT ....
SUM(CASE WHEN products.date>'01-01-2015' THEN products.quantity
ELSE 0
END) as numberSold,
....
ORDER by numberSold

Only getting one result from sub select in inner join in mysql

Hi I have run into a dilemma, I am doing this query:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories, pr.promo_name, pr.add_value_text, c.contract_id, c.cmeal_plan, c.cmin_markup, c.civa, c.tax_include, c.hotel_id, hi.hname, hi.hstars, im.image_file, pl.plan_name, ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi ON hi.hotel_id = c.hotel_id AND hi.destination_id = '6460'
INNER JOIN images AS im ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl ON pl.plan_code = c.cmeal_plan AND pl.lang = '1'
INNER JOIN hotel_categories AS hc ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca ON ca.category_code = hc.category_code AND ca.lang = '1'
LEFT JOIN
(SELECT
r.hotel_id, AVG(r.double) AS price
FROM
rates AS r ) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr ON pr.hotel_id = hi.hotel_id AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0 AND pr.book_start <= '2012-11-01' AND pr.book_end >= '2012-11-02' AND travel_start <= '2012-11-23' AND travel_end >= '2012-11-30' AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01' AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id
I am getting all the desired results except for the sub select query.. each hotel has a price but it is only giving me back one result and the rest are all null. Is there an error in my query? If any additional information is needed please let me know and thank you in advance for any help!
You are missing the GROUP BY in your subquery, so MySQL will only return one-value. If you want all hotel_id's then you need to GROUP BY that field:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories,
pr.promo_name,
pr.add_value_text,
c.contract_id,
c.cmeal_plan,
c.cmin_markup,
c.civa,
c.tax_include,
c.hotel_id,
hi.hname,
hi.hstars,
im.image_file,
pl.plan_name,
ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi
ON hi.hotel_id = c.hotel_id
AND hi.destination_id = '6460'
INNER JOIN images AS im
ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl
ON pl.plan_code = c.cmeal_plan
AND pl.lang = '1'
INNER JOIN hotel_categories AS hc
ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca
ON ca.category_code = hc.category_code
AND ca.lang = '1'
LEFT JOIN
(
SELECT r.hotel_id, AVG(r.double) AS price
FROM rates AS r
GROUP BY r.hotel_id <-- add a GROUP BY hotel_id then you will get avg() for each hotel
) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr
ON pr.hotel_id = hi.hotel_id
AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0
AND pr.book_start <= '2012-11-01'
AND pr.book_end >= '2012-11-02'
AND travel_start <= '2012-11-23'
AND travel_end >= '2012-11-30'
AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01'
AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id