Using this query will get the result as in shown in below image link
select c.property_title,b.property_title as building,d.property_title as floor,e.house_name as house,
f.room_no as room,g.bed_no as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'
I have written the query for fetching the count of parent and child .will get the building count as 7 floor count as 7 house count as 5 room count as 4, instead the count of building should be 2, floor = 2, house = 4, Rooms = 5 and Bed 1.
select c.property_title,count(b.property_title)as building,count(d.property_title)as floor,count(e.house_name)as house,
count(f.room_no)as room,count(g.bed_no) as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.house_id = g.id and g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'
Thank you in advance
Try this:
SQL SERVER
select count([building]) over (order by building) as [bulding count],
count([floor]) over (order by [floor],[building]) as [floor count],
count([house]) over (order by building,[floor],[house]) as [house count],
count([room]) over (order by building,[floor],[house],[room]) as [room count],
count([bed]) over (order by building,[floor],[house],[room],[bed]) as [room count]
Related
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
SELECT commerce_product_field_data_commerce_product__field_data_field_products.entity_id, field_products_commerce_product.nid FROM commerce_order o
join commerce_payment_transaction t on o.order_id = t.order_id
join commerce_line_item i on o.order_id = i.order_id
LEFT JOIN field_data_commerce_total s ON i.line_item_id = s.entity_id AND (s.entity_type = 'commerce_line_item' AND s.deleted = '0')
LEFT JOIN field_data_commerce_product field_data_commerce_product ON i.line_item_id = field_data_commerce_product.entity_id AND (field_data_commerce_product.entity_type = 'commerce_line_item' AND field_data_commerce_product.deleted = '0')
INNER JOIN commerce_product commerce_product_field_data_commerce_product ON field_data_commerce_product.commerce_product_product_id = commerce_product_field_data_commerce_product.product_id
LEFT JOIN
(select * from field_data_field_products)
commerce_product_field_data_commerce_product__field_data_field_products ON commerce_product_field_data_commerce_product.product_id = commerce_product_field_data_commerce_product__field_data_field_products.field_products_product_id
LEFT JOIN ( select nid as nid from node order by nid)
field_products_commerce_product
ON commerce_product_field_data_commerce_product__field_data_field_products.entity_id = field_products_commerce_product.nid LEFT JOIN (
select r.entity_id, r.field_ranges_value from field_data_field_ranges r
) r
on r.entity_id = field_products_commerce_product.nid
WHERE t.status = 'success' and i.type = 'product' and o.Uid <> 0
AND o.status IN ('completed') and o.created >= '1483228800' and o.created <= '1483315200' and r.field_ranges_value = 'Tasty Sticks'
Is my sql
It is giving me 5 results. I only need 4
One of the product id's belong to two Drupal nodes and I only want one of them
I tried changing LEFT JOIN ( select nid as nid from node order by nid) to
LEFT JOIN ( select nid as nid from node order by nid limit 1) but then I don't get any records at all. Any idea what needs changing please other than removing one of the duplicate nodes. Thanks
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
I have 1 table with string type. (photo_add, photo_comment etc.)
Then I have 4 other tables that are connected with these types.
I need to make a query which will choose the data from all these tables according to the type.
I created this but it does not work.
SELECT DISTINCT a.id, a.event, a.params, a.created_at, a.item_id, a.sender_id, a.receiver_id, u.name, u.id as userid
FROM `eva49_lovefactory_activity` a, eva49_users u
CASE a.event
WHEN "photo_add" THEN
INNER JOIN eva49_lovefactory_photos lp ON lp.id = a.item_id AND lp.fsk18 = -1
WHEN "group_create" THEN
INNER JOIN eva49_lovefactory_groups lg ON lg.id = a.item_id AND lg.fsk18 = -1
WHEN "photo_comment" THEN
INNER JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND lic.fsk18 = -1
WHEN "profile_comment" THEN
INNER JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND lic.fsk18 = -1
WHEN "profile_comment" THEN
INNER JOIN eva49_lovefactory_profiles lp ON lp.user_id = a.receiver_id AND lp.status_fsk18 = -1
ELSE 1=1
END
WHERE (u.id = a.sender_id OR u.id = a.receiver_id) and (u.id <> 379)
ORDER BY a.created_at DESC LIMIT 0, 10
Is there any way how to pair the tables? The database structure was given to me like this.
EDIT:
There is a table "eva49_lovefactory_activity" which contains information about recent activity. It has a column "event" that contains string with the event name (photo_add, photo_comment etc.)
There are other tables - eva49_lovefactory_photos, eva49_lovefactory_profiles, eva49_lovefactory_groups etc. In these tables I have to find whether the item was approved by admin. If it was I can show it. (fsk18 = -1)
Move CASE condition to JOIN condition and do LEFT JOIN..
SELECT DISTINCT a.id, a.event, a.params, a.created_at, a.item_id, a.sender_id, a.receiver_id, u.name, u.id as userid
FROM `eva49_lovefactory_activity` a, eva49_users u
LEFT JOIN eva49_lovefactory_photos lp ON lp.id = a.item_id AND lp.fsk18 = -1
AND a.event='photo_add'
LEFT JOIN eva49_lovefactory_groups lg ON lg.id = a.item_id AND lg.fsk18 = -1
AND a.event='group_create'
LEFT JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND
lic.fsk18 = -1 AND a.event='photo_comment'
LEFT JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND
lic.fsk18 = -1 AND a.event='profile_comment'
WHERE (u.id = a.sender_id OR u.id = a.receiver_id) and (u.id <> 379)
ORDER BY a.created_at DESC LIMIT 0, 10
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