0

我正在尝试在普及数据库上运行此查询

select cust_no,cust_name,sum(bvtotal) as Amount
from sales_history_header  
where cust_no is not null and number is not null and bvtotal > 1000 and in_date < 20140101
group by cust_no,cust_name
order by sum(bvtotal) desc;

如何排除那些在子结果中有 in_date > 20140101 的组结果?

我的这个查询也在获取那些 in_date > 20140101 的结果

难道我做错了什么 ?

我得到的示例输出是这种格式

cust_no     cust_name      amount
A             a1            500
B             b1            500
C             c1            1000

我想用 cust_no 'A' 排除这条记录,因为它在 20140202 中有与 in_date 的交易

考虑在我的原始数据中,我有类似的记录

cust_no     cust_name      amount    in_date
A             a1            100      20130203
A             a1            400      20130101
A             a1            1000     20140503
4

3 回答 3

1

您需要根据一组 id 排除所有记录。通常,您使用子查询执行此操作:

SELECT cust_no,
    cust_name,
    sum(bvtotal) AS Amount
FROM sales_history_header
WHERE cust_no IS NOT NULL
    AND number IS NOT NULL
    AND bvtotal > 1000
    AND cust_no NOT IN (
        SELECT cust_no 
        FROM sales_history_header 
        WHERE in_date >= 20140101 
            AND cust_no IS NOT NULL
    )
GROUP BY cust_no,
    cust_name
ORDER BY sum(bvtotal) DESC;

子查询的AND cust_no IS NOT NULL一部分是为了避免NOT INNULL值出现问题。如果将其重写为NOT EXISTS相关子查询,您可能会获得更好的性能,但根据我的经验,MySQL 在这些方面做得很差。

另一种选择是更明确的自反连接方法(左连接并在右表为空的情况下过滤),但这有点……粗略的感觉?……因为你似乎允许cust_no这样做NULL,因为它是一个查询聚合,所以感觉就像你不得不担心乘以行:

SELECT s1.cust_no,
    s1.cust_name,
    sum(s1.bvtotal) AS Amount
FROM sales_history_header s1
LEFT JOIN (
        SELECT cust_no
        FROM sales_history_header
        WHERE cust_no IS NOT NULL
            AND number IS NOT NULL
            AND bvtotal > 1000
            AND in_date >= 20140101) s2
    ON  s2.cust_no = s1.cust_no
WHERE s1.cust_no IS NOT NULL
    AND s1.number IS NOT NULL
    AND s1.bvtotal > 1000
    AND s2.cust_no IS NULL
GROUP BY cust_no,
    cust_name
ORDER BY sum(bvtotal) DESC;

LEFT JOIN结合WHERE [...] s2.cust_no IS NULL是消除您不想要的记录的部分。

于 2015-03-11T19:30:21.073 回答
0

我认为您需要将日期常量指定为日期,除非您真的想处理整数。

select cust_no,cust_name,sum(bvtotal) as Amount
from sales_history_header  
where cust_no is not null and number is not null and bvtotal > 1000 and in_date < '2014-01-01'
group by cust_no,cust_name
order by sum(bvtotal) desc;
于 2015-03-11T18:23:38.883 回答
0

我不明白你到底需要什么,

但您似乎混合了 INT 和 DATE 类型。

因此,如果您的in_date字段类型为DATE

select 
  cust_no,
  cust_name,
  sum(bvtotal) as Amount
from sales_history_header  
where cust_no is not null 
  and number is not null 
  and bvtotal > 1000 
  and in_date < DATE('2014-01-01')
group by cust_no,cust_name
order by sum(bvtotal) desc;

如果您的in_date字段类型为TIMESTAMP

select 
  cust_no,
  cust_name,
  sum(bvtotal) as Amount
from sales_history_header  
where cust_no is not null 
  and number is not null 
  and bvtotal > 1000 
  and in_date < TIMESTAMP('2014-01-01 00:00:00')
group by cust_no,cust_name
order by sum(bvtotal) desc;
于 2015-03-11T19:09:57.790 回答