帮助进行 mysql 查询

发布于 2024-12-02 07:23:43 字数 627 浏览 5 评论 0原文

我有一个数据库,其中包含我的在线网上商店的所有交易,我正在尝试进行查询以打印一份简单的财务报表。

它将打印在如下表中:

<th>month</th>
<th>number of sales</th>
<th>money in</th>
<th>money out</th>
<th>result</th>

失败的查询: #1111 - 组函数的无效使用

SELECT 
month(transaction_date) as month,
count(incoming_amount > '0') as number_of_sales,
sum(incoming_amount / 1.25) as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
FROM myDB WHERE year(timestamp) = '2011' order by id desc");

有人能指出我正确的方向吗?

i have a DB with all transactions of my online webshop, and im trying to make a query to print out a simple financial statement.

it will be printed in a table like this:

<th>month</th>
<th>number of sales</th>
<th>money in</th>
<th>money out</th>
<th>result</th>

the query that fails with: #1111 - Invalid use of group function

SELECT 
month(transaction_date) as month,
count(incoming_amount > '0') as number_of_sales,
sum(incoming_amount / 1.25) as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
FROM myDB WHERE year(timestamp) = '2011' order by id desc");

Can anyone point me in the right direction?

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

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

发布评论

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

评论(3

纵性 2024-12-09 07:23:43
SELECT 
month(transaction_date) as month,
sum(if(incoming_amount>0,1,0)) as number_of_sales,
sum(incoming_amount)/1.25 as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount/1.25)-outgoing_amount) as result
FROM myDB 
WHERE timestamp>='2011-01-01 00:00:00' AND timestamp<='2011-12-11 23:59:59'
GROUP BY month;
  1. 使用聚合函数时需要指定列
  2. year(timestamp) 不使用 mysql 索引(如果您在时间戳上定义了索引)
  3. 聚合函数 count(incoming_amount > ' 0') 不正确
  4. sum 看起来也不正确
SELECT 
month(transaction_date) as month,
sum(if(incoming_amount>0,1,0)) as number_of_sales,
sum(incoming_amount)/1.25 as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount/1.25)-outgoing_amount) as result
FROM myDB 
WHERE timestamp>='2011-01-01 00:00:00' AND timestamp<='2011-12-11 23:59:59'
GROUP BY month;
  1. you need to specify a column when using aggregate function
  2. year(timestamp) does not make use on mysql index (if you have define an index on timestamp)
  3. aggregate function on count(incoming_amount > '0') is incorrect
  4. sum does not looks correct too
肤浅与狂妄 2024-12-09 07:23:43

添加group by语句:

SELECT 
month(transaction_date) as month,
count(incoming_amount > '0') as number_of_sales,
sum(incoming_amount / 1.25) as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
FROM myDB WHERE year(timestamp) = '2011' GROUP BY month order by id desc");

Add group by statement:

SELECT 
month(transaction_date) as month,
count(incoming_amount > '0') as number_of_sales,
sum(incoming_amount / 1.25) as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
FROM myDB WHERE year(timestamp) = '2011' GROUP BY month order by id desc");
娇柔作态 2024-12-09 07:23:43

基于 @ajreal 的答案,您可以通过重用以前计算的值来加速此查询,如下所示:

SELECT s.*,
       (s.money_in - s.money_out) as result 
FROM
  (
  SELECT 
    month(transaction_date) as month,
    /*  year(transaction_date) as year   */  
    sum(incoming_amount>0) as number_of_sales, -- true = 1, false = 0.
    sum(incoming_amount)/1.25 as money_in,
    sum(outgoing_amount) as money_out,
  FROM myDB 
  WHERE transaction_date BETWEEN '2011-01-01 00:00:00' AND '2011-12-31 23:59:59'
  GROUP BY /*year,*/ month DESC;
  ) AS s

如果您选择超出年份,请取消注释相关部分。
请注意,您可以将 DESC 修饰符添加到 group by 以首先获取最新结果。

Building on @ajreal's answer, you can speed this query up by reusing previously calculated values like so:

SELECT s.*,
       (s.money_in - s.money_out) as result 
FROM
  (
  SELECT 
    month(transaction_date) as month,
    /*  year(transaction_date) as year   */  
    sum(incoming_amount>0) as number_of_sales, -- true = 1, false = 0.
    sum(incoming_amount)/1.25 as money_in,
    sum(outgoing_amount) as money_out,
  FROM myDB 
  WHERE transaction_date BETWEEN '2011-01-01 00:00:00' AND '2011-12-31 23:59:59'
  GROUP BY /*year,*/ month DESC;
  ) AS s

If you select beyond the year, uncomment the relevant sections.
Note you can add a DESC modifier to group by to get the most recent results first.

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