简体   繁体   English

根据另一个表结果选择并分组

[英]select and group from based on another table result

I have two tables; 我有两个桌子。 hotelservices and guestorder 酒店服务和客人订单

hotelservices 酒店服务

s_id(pk)    serviceType    serviceName

guestOrder table: guestOrder表:

g_id(pk)    serviceType    totalAmount    balanceDue    PaidAmount    orderDate

I want to get the sum of totalAmount,balanceDue,PaidAmount based upon the serviceType from the hotelservice table. 我想从hotelservice表中获取基于serviceType的totalAmount,balanceDue,PaidAmount的总和。

That is, it will iterate and group the hotelservices.serviceType and get the associated data from the guestorder table based upon the serviceType column. 也就是说,它将基于serviceType列对hotelservices.serviceType进行迭代和分组,并从guestorder表中获取关联的数据。

My attempt so far: 到目前为止我的尝试:

SELECT
 g.OrderDate as saleDate,
 g.ServiceType,
 sum(g.TotalAmount) as TotalSales,
 sum(g.BalanceDue) as TotalBalanceDue,
 sum(g.PaidAmount) as TotalPaid,
 h.ServicesType as services
 FROM guestorder as g join hotelservices as h on g.ServiceType=h.ServicesType
 group by saleDate

but it is not what i want. 但这不是我想要的。 i need it to show others serviceType even if it has nothing in the guestorder.i don't know if i am making sense at all. 我需要它来显示其他serviceType,即使guestorder中没有任何内容。我也不知道我是否有意义。 thanks in advance 提前致谢

Maybe reverse your join, turn it into a LEFT OUTER JOIN and fix the problem with your GROUP BY, eg: 也许反转您的联接,将其变成左外联接,然后使用GROUP BY修复问题,例如:

SELECT
    g.OrderDate as saleDate,
    g.ServiceType,
    sum(g.TotalAmount) as TotalSales,
    sum(g.BalanceDue) as TotalBalanceDue,
    sum(g.PaidAmount) as TotalPaid,
    h.ServicesType as services
FROM 
    hotelservices as h 
    LEFT JOIN guestorder as g ON g.ServiceType=h.ServicesType
GROUP BY
    g.OrderDate,
    g.ServiceType,
    h.ServicesType;

i need it to show others serviceType even if it has nothing in the guestorder

from this im assuming you have data that doesn't match... when you use a JOIN or INNER JOIN (because thats what JOIN defaults to) you are filtering out anything that doesn't match. 从这个即时消息假设您有不匹配的数据...当您使用JOIN或INNER JOIN(因为那是JOIN默认值)时,您将过滤出所有不匹配的内容。 if you want to join without filtering then do a LEFT JOIN.. 如果您想加入而不过滤,请进行LEFT JOIN ..

you should add COALESCE() so that you don't have NULL values like so 您应该添加COALESCE()以使您没有像这样的NULL值

SELECT
    COALESCE(g.OrderDate, '') saleDate,
    COALESCE(g.ServiceType, '') guestServiceType,
    COALESCE(sum(g.TotalAmount), 0) TotalSales,
    COALESCE(sum(g.BalanceDue), 0) TotalBalanceDue,
    COALESCE(sum(g.PaidAmount), 0) TotalPaid,
    h.ServicesType as services
FROM hotelservices h 
LEFT JOIN guestorder g ON g.ServiceType = h.ServicesType
GROUP BY h.serviceType, saleDate

NOTE: 注意:

your select needs to be FROM the hotel services table so that way you have all of the service types.. then LEFT JOIN the guestorder table so that way there is no filtering. 您的选择需要来自“酒店服务”表,这样您才可以拥有所有服务类型。然后左键联接guestorder表,这样就不会进行过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM