简体   繁体   English

MySQL聚合查询

[英]MySQL aggregation query

I am currently trying to aggregate the people who visited a sports facility and how much it cost them.我目前正在尝试汇总访问体育设施的人数以及他们的花费。 I have run the following code successfully but need to aggregate the people.我已成功运行以下代码,但需要汇总人员。 How can I do that.我怎样才能做到这一点。

The tables used for the code is here Tables用于代码的表格在这里表格

SELECT CONCAT( members.firstname, " ", members.surname ) AS fullname,
CASE WHEN slots >0
THEN (
bookings.slots * facilities.membercost + bookings.slots * facilities.guestcost
)
ELSE NULL
END AS "cost"
FROM `Bookings` AS bookings
LEFT JOIN `Members` AS members ON bookings.memid = members.memid
LEFT JOIN Facilities AS facilities ON bookings.facid = facilities.facid
WHERE bookings.starttime LIKE '2012-09-14%'
AND (
bookings.slots * facilities.membercost + bookings.slots * facilities.guestcost
) >30
ORDER BY 1 DESC 

OutputScreenshot输出截图

I got throught with this.... Here is my solution:我已经解决了这个......这是我的解决方案:

SELECT CONCAT (members.firstname," ",members.surname) AS fullname,
    SUM(bookings.slots*facilities.membercost + bookings.slots*facilities.guestcost) AS cost

    FROM `Bookings` AS bookings
            LEFT JOIN `Members` AS members 
            ON bookings.memid = members.memid
            LEFT JOIN Facilities as facilities
            ON bookings.facid =facilities.facid
    WHERE bookings.starttime LIKE '2012-09-14%' AND (bookings.slots*facilities.membercost + bookings.slots*facilities.guestcost) >30


GROUP BY 1
ORDER BY 2 DESC

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

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