1

我有以下输出以下图像的 mysql 查询:

select
  v.invoicenumber,
  v.invoicedate,
  v.haulier,
  v.transporttype,
  count(v.loadnumber) as totalloads,
  sum(v.cost) as totalcost, 
  concat(SUM(if(invoiceapproved = 'yes', 1, 0)),' / ',count(v.loadnumber)) AS count,  SUM(if(invoiceapproved = 'yes', 1, 0)) as approved
from v2loads v  
  where v.invoiced='yes'   
  group by invoicenumber

在此处输入图像描述 此查询执行 100%。

我想要做的是过滤掉计数为 100% 的任何行/数据。在示例输出中,我想过滤掉发票 16,因为它是 2/2 和 100%。所以在哪里

 count(v.loadnumber) <> SUM(if(invoiceapproved = 'yes', 1, 0))

如果我将此逻辑添加到 where 子句中,则会因错误无效使用组函数而失败。所以下面的代码不起作用:

select v.invoicenumber,
  v.invoicedate,
  v.haulier,
  v.transporttype,
  count(v.loadnumber) as totalloads,
  sum(v.cost) as totalcost, 
  concat(SUM(if(invoiceapproved = 'yes', 1, 0)),' / ',count(v.loadnumber)) AS count, 
  SUM(if(invoiceapproved = 'yes', 1, 0)) as approved 
from v2loads v  
  where v.invoiced='yes' and 
  (count(v.loadnumber))<>(SUM(if(invoiceapproved = 'yes', 1, 0)))  
  group by invoicenumber

我收到以下错误:

错误是#1111- 组功能的使用无效。

任何建议一如既往地受到赞赏。

4

2 回答 2

1

你可以这样做:

SELECT 
  *,
  CONCAT(approved, ' / ', totalloads) AS count, 
FROM
(
    SELECT
      v.invoicenumber,
      v.invoicedate,
      v.haulier,
      v.transporttype,
      COUNT(v.loadnumber)                    AS totalloads,
      SUM(v.cost)                            AS totalcost, 
      SUM(if(invoiceapproved = 'yes', 1, 0)) As approved 
    FROM v2loads v  
    WHERE v.invoiced='yes'
    GROUP BY invoicenumber
) t
WHERE totalloads <> approved;
于 2012-12-21T08:12:02.010 回答
0
select v.invoicenumber,
v.invoicedate,
v.haulier,
v.transporttype,
count(v.loadnumber) as totalloads,
sum(v.cost) as totalcost, 
concat(SUM(if(invoiceapproved = 'yes', 1, 0)),' / ',count(v.loadnumber)) AS count,
SUM(if(invoiceapproved = 'yes', 1, 0)) as approved  
from v2loads v 
where v.invoiced='yes'
group by invoicenumber 
having (count(v.loadnumber))<>(SUM(if(invoiceapproved = 'yes', 1, 0)))
于 2012-12-21T08:13:11.347 回答