[英]SQL - JOIN newest 1 value in a one-to-many relationship
我有以下查詢,它顯示有關“廚師”的信息,以及他們為食品服務應用程序出售的食品。 它顯示每個賣家、一些個人信息以及他們在平台上銷售的食品。
這是一個 postgresql 數據庫。
當前查詢引用並匯總來自 7 個不同表的數據。 我在下面包含了每個表的圖表,以及它們之間的鏈接關系。 我同意,這個查詢應該分成單獨的更相關的報告,但這是客戶要求的。
這個查詢幾乎完成了,最后的要求是以某種方式包含一個列來顯示每個食品的最近訂購時間 (orders.time_placed)。
想到一個主要問題:
這可能嗎? 考慮到“訂單”表沒有包含所購買食品的列。 “訂單”表僅鏈接回“商店”表……這讓我相信我能做的最好的事情就是拉出每家商店的最后一次銷售時間(而不是商品級別)。
在四處搜索時,我發現了一些與我非常相似的情況——尤其是這個: 鏈接
盡管我沒有成功實施該解決方案。 我是不是太復雜了?
這是我當前的查詢:
select
account.id as "Account ID",
account.firstname as "Seller First Name",
account.lastname as "Seller Last Name",
account.email as "Seller Email",
account.phone as "Seller Phone",
address.address as "Seller Address (Street)",
address.address_2 as "Seller Address 2",
account.zip_code as "Seller Zip",
address.neighborhood as "Seller Neighborhood",
menu.name as "Name of active menu",
kitchen_item.name as "Dishes",
kitchen_item.price as "Price",
kitchen_item.daily_max_orders as "Quantity",
menu.pickup_start_time as "Start time",
menu.pickup_end_time as "End time",
menu.repeat_mon as "Monday",
menu.repeat_tues as "Tuesday",
menu.repeat_wed as "Wednesday",
menu.repeat_thurs as "Thursday",
menu.repeat_fri as "Friday",
menu.repeat_sat as "Saturday",
menu.repeat_sun as "Sunday"
from account
left join store on account.id = store.account_id
left join menu on store.id = menu.store_id
left join menu_item on menu.id = menu_item.menu_id
left join kitchen_item on (menu_item.kitchen_item_id = kitchen_item.id and store.id = kitchen_item.store_id)
join store_address on store.id = store_address.store_id
join address on store_address.address_id = address.id
group by account.id, account.firstname, account.lastname, account.email, account.phone, address.address, address.address_2, account.zip_code, address.neighborhood, menu.name, kitchen_item.name, kitchen_item.price, kitchen_item.daily_max_orders, menu.pickup_start_time, menu.pickup_end_time, menu.repeat_mon, menu.repeat_tues, menu.repeat_wed, menu.repeat_thurs, menu.repeat_fri, menu.repeat_sat, menu.repeat_sun
order by account.id asc;
這是我嘗試從上面的鏈接借用的解決方案...令人驚訝的是,它不起作用:)(插入到最后一個 JOIN 的正下方):
inner join (
SELECT orders.store_id,
MAX(orders.placed) maxdate
from orders
group by orders.store_id
) maxdates on account.id = maxdates.buyer_account_id INNER JOIN
orders o on maxdates.store_id = store.id
and maxdates.maxdate = o.placed
有沒有更簡單的方法來完成這個看似簡單的任務? 謝謝
編輯 - 我很抱歉,我意識到這對我來說有點太多了,無法提供示例數據和預期的 output 除了屏幕截圖。 鏈接問題中有一個簡化的示例,但我不知道如何在此處將邏輯應用於我的查詢。
正如您所懷疑的,您無法查詢您沒有的數據。 由於您的架構不包含有關某些商品的訂購時間的任何信息,因此您無法檢索該信息。
要獲得商店的最新銷售信息,您可以使用 postgresql 的 DISTINCT ON(請參閱文檔)。
select distinct on (account.id, kitchen_item.name)
account.id as "Account ID",
account.firstname as "Seller First Name",
account.lastname as "Seller Last Name",
account.email as "Seller Email",
account.phone as "Seller Phone",
address.address as "Seller Address (Street)",
address.address_2 as "Seller Address 2",
account.zip_code as "Seller Zip",
address.neighborhood as "Seller Neighborhood",
menu.name as "Name of active menu",
kitchen_item.name as "Dishes",
kitchen_item.price as "Price",
kitchen_item.daily_max_orders as "Quantity",
menu.pickup_start_time as "Start time",
menu.pickup_end_time as "End time",
menu.repeat_mon as "Monday",
menu.repeat_tues as "Tuesday",
menu.repeat_wed as "Wednesday",
menu.repeat_thurs as "Thursday",
menu.repeat_fri as "Friday",
menu.repeat_sat as "Saturday",
menu.repeat_sun as "Sunday",
orders.time_placed as "Last Store Sale"
from account
left join store on account.id = store.account_id
left join menu on store.id = menu.store_id
left join menu_item on menu.id = menu_item.menu_id
left join kitchen_item on (menu_item.kitchen_item_id = kitchen_item.id and store.id = kitchen_item.store_id)
left join orders on (orders.store_id = store.id)
join store_address on store.id = store_address.store_id
join address on store_address.address_id = address.id
order by account.id asc, kitchen_item.name asc, orders.time_placed desc;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.