显示字段中值的总计

发布于 2024-10-20 17:42:12 字数 418 浏览 1 评论 0原文

下午好。我在想是否有可能显示表中选定字段的总和或总计以及与日期的关系:

例如我想知道我的酒店每月的啤酒总消耗量。

我这里有我的服务表:

[services_id[pk],
customer_id[fk],
date_in,date_out,room_type,room_number,
extra_ref,
extra_bed,extra_snack,
extra_beer,extra_softdrinks,
extra_pillows,extra_breakfast,
extra_snack_q,
extra_beer_q,
extra_softdrinks_q,
extra_pillows_q,
extra_breakfast_q]

您能给我一些关于如何获得它的建议吗? 提前致谢:

-renz

good pm. i was thinking is it possible to show the summation or grand total of a selected field in the table and with relation to date:

for example is i want to know the total beer consumption of my hotel every month.

i have here my table on services:

[services_id[pk],
customer_id[fk],
date_in,date_out,room_type,room_number,
extra_ref,
extra_bed,extra_snack,
extra_beer,extra_softdrinks,
extra_pillows,extra_breakfast,
extra_snack_q,
extra_beer_q,
extra_softdrinks_q,
extra_pillows_q,
extra_breakfast_q]

can you give some advice on how can i get it.
thanks in advance:

-renz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆夏时光 2024-10-27 17:42:12
SELECT SUM(beer_amount) as monthly_beer_amount
FROM [DATABASE].[TABLE] 
WHERE beer_sold_date BETWEEN '20110201' AND '20110228'

[额外信息]

另外,我认为组织此表的最佳方法是将其分成几个其他表。将客户信息存储在第一个表中,例如

[customer_id, customer_name, date_in, date_out, room_type, room_number]. 
1, Bob, 20110101, 20110110, big, 200
2, Joe, 20110101, 20110110, small, 202
....

,并有另一个名为 room_items 的表,其中包含以下内容,

[id, item_name]
1, BEER
2, BED
3, SNACK
...

然后另一个名为 room_purchases 的表,其中包含以下内容,

[customer_id, purchase_id, amount, date....]
1, 1, 10, 20110101
2, 3, 5, 20110101
3, 1, 9, 20110101
....

这将帮助您对所有三个表进行联接,它们将以这种方式更加规范化。

SELECT SUM(t2.amount) as beer_amount
FROM customers as t1 
LEFT JOIN room_purchases as t2 ON t1.customer_id = t2.customer_id
WHERE t2.purchase_id = 1 AND t2.date BETWEEN 20110101 AND 20110131
SELECT SUM(beer_amount) as monthly_beer_amount
FROM [DATABASE].[TABLE] 
WHERE beer_sold_date BETWEEN '20110201' AND '20110228'

[EXTRA INFO]

Also I believe that the best way to organize this table is to separate out this table into a few other tables. Store customer info in the first table such as

[customer_id, customer_name, date_in, date_out, room_type, room_number]. 
1, Bob, 20110101, 20110110, big, 200
2, Joe, 20110101, 20110110, small, 202
....

And have another table named something like room_items which would have the following,

[id, item_name]
1, BEER
2, BED
3, SNACK
...

And then another table named room_purchases which will have the following,

[customer_id, purchase_id, amount, date....]
1, 1, 10, 20110101
2, 3, 5, 20110101
3, 1, 9, 20110101
....

This would help you to do a join on all three tables and they would be more normalized in this way.

SELECT SUM(t2.amount) as beer_amount
FROM customers as t1 
LEFT JOIN room_purchases as t2 ON t1.customer_id = t2.customer_id
WHERE t2.purchase_id = 1 AND t2.date BETWEEN 20110101 AND 20110131
南街女流氓 2024-10-27 17:42:12
SELECT DATE_FORMAT(date_in,"%m-%Y") AS month,SUM(extra_beer) AS beers
FROM your_table
GROUP BY month

这将返回如下内容:

02-2011 | 43
03-2011 | 52

如果您想根据日期限制查询,只需在 GROUP BY 之前添加一个 WHERE 约束

SELECT DATE_FORMAT(date_in,"%m-%Y") AS month,SUM(extra_beer) AS beers
FROM your_table
GROUP BY month

this will return something like this:

02-2011 | 43
03-2011 | 52

if you want to limit the query depending on the date just add a WHERE constraint before the GROUP BY

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文