我从这个网站的帮助中得到了2个查询,它们是:
SELECT gr.g_name, (DATEDIFF(d, r.res_checkout_date, r.res_checkin_date) * pp.rate ) + ISNULL(i.inv_amount, 0)
FROM guest_reservation gr LEFT OUTER JOIN invoice i ON gr.confirm_no = i.confirm_no
JOIN reservation r ON gr.confirm_no = r.confirm_no
JOIN price_plan pp ON r.price_plan = pp.price_plan;
SELECT g.g_name, DATEDIFF(d, r.res_checkin_date, r.res_checkout_date)*p.rate+coalesce(i.inv_amount, 0) as Amount
FROM reservation as r INNER JOIN priceplan as p
ON r.price_plan = p.price_plan
INNER JOIN guest_reservation as g
ON r.confirm_no = g.confirm_no
LEFT OUTER JOIN invoice as i
ON r.confirm_no = i.confirm_no;
所有表格都包含以下数据:
guest reservation
表包含以下带有数据的列:
(confirm_no, agent_id, g_name, g_phone)
reservation
表包含以下带有数据的列:
(confirm_no, credit_card_no, res_checkin_date, res_checkout_date,
default_villa_type, price_plan)
我需要以某种方式添加来自dining_order
表的客人已订购的商品(与r_confirm_no
表中的dining_order
相关联等于来自confirm_no
的{{1}} reservation
表格),商品价格必须从dining_menu
表格中获取(dining_order.item
等于dining_menu.item
)并添加到上述查询中。
我需要添加的新信息的关联表是:
invoice
表包含以下带有数据的列:
(inv_no, inv_date, inv_amount, confirm_no).
price plan
表包含以下带有数据的列:
(price_plan, rate, default_villa_type, bed_type)
答案 0 :(得分:1)
这是未经测试的,但您可以使用子查询对每位客人的餐饮项进行求和,并将其添加到总数中。这也是一个外部连接,因为客人可能没有使用餐饮设施。
SELECT gr.g_name, (DATEDIFF(d, r.res_checkout_date, r.res_checkin_date) * pp.rate ) + ISNULL(i.inv_amount, 0) + ISNULL(d.total_dining, 0)
FROM guest_reservation gr
LEFT OUTER JOIN invoice i ON gr.confirm_no = i.confirm_no
JOIN reservation r ON gr.confirm_no = r.confirm_no
JOIN price_plan pp ON r.price_plan = pp.price_plan
LEFT OUTER JOIN (SELECT r_confirmation_no, SUM(price) AS total_dining
FROM dining_order do JOIN dining_menu dm ON do.item = dm.item
GROUP BY r_confirmation_no) AS d ON d.r_confirmation_no = r.confirm_no