SQL-如何查询大于使用第一天出现的值的值?

SQL-如何查询大于使用第一天出现的值的值?,sql,Sql,我有下表 "customers_dishes" id - customer id, STRING date - date of arriving at the restaurant, DATE bill - total bill, DOUBLE 我试图输出所有客户的账单高于他们第一次访问餐厅账单的事件 id, date, bill Alen, 2018-03-01, 50 Alen, 2018-03-02, 48 Alen, 2019-03-01, 60 Bob, 2018-03-04, 45

我有下表

"customers_dishes"
id - customer id, STRING
date - date of arriving at the restaurant, DATE
bill - total bill, DOUBLE
我试图输出所有客户的账单高于他们第一次访问餐厅账单的事件

id, date, bill
Alen, 2018-03-01, 50
Alen, 2018-03-02, 48
Alen, 2019-03-01, 60
Bob, 2018-03-04, 45
Bob, 2018-03-06, 55
Bob, 2019-03-01, 50
输出应为:

id, date, bill
Alen, 2019-03-01, 60
Bob, 2018-03-06, 55
Bob, 2019-03-01, 50
尝试过这样做:

SELECT (*) FROM customers_dishes
WHERE date NOT IN (
SELECT date FROM customers_dishes ORDER BY id, date ASC LIMIT 1)
AND id NOT IN(
SELECT id FROM customers_dishes ORDER BY id, date ASC LIMIT 1)
有什么建议吗?

您可以使用第一个值:

SELECT a.*
FROM customers_dishes a
CROSS APPLY (
              SELECT TOP 1 bill
              FROM customers_dishes t
              WHERE t.id = a.id
                  AND a.DATE <> t.DATE
              ORDER BY DATE ASC
) b
WHERE b.bill is not null and a.bill > b.bill
还可以使用相关子查询:

select cd.*
from customers_dishes cd
where cd.bill > (select cd2.bill
                 from customers_dishes cd2
                 where cd2.id = cd.id
                 order cd2.date
                 fetch first 1 row only
                );

用您正在使用的数据库标记您的问题。
select cd.*
from customers_dishes cd
where cd.bill > (select cd2.bill
                 from customers_dishes cd2
                 where cd2.id = cd.id
                 order cd2.date
                 fetch first 1 row only
                );