Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
T SQL选择每个客户的上次销售_Sql_Sql Server_Tsql - Fatal编程技术网

T SQL选择每个客户的上次销售

T SQL选择每个客户的上次销售,sql,sql-server,tsql,Sql,Sql Server,Tsql,我试图检索每个客户的最后销售日期,如下所示,但它只是返回表中的最后一个条目: Select top 1 InvoiceDate ,Customer from salestable a order by InvoiceDate desc 非常感谢您的帮助。不要使用TOP 1-这就是为什么您只返回1个结果的原因 尝试使用MAX和GroupBy客户 SELECT Customer, MAX(InvoiceDate) FROM SalesTable GROUP By Customer ORDER By

我试图检索每个客户的最后销售日期,如下所示,但它只是返回表中的最后一个条目:

Select top 1 InvoiceDate
,Customer
from salestable a
order by InvoiceDate desc

非常感谢您的帮助。

不要使用
TOP 1
-这就是为什么您只返回1个结果的原因

尝试使用
MAX
GroupBy
客户

SELECT Customer, MAX(InvoiceDate)
FROM SalesTable
GROUP By Customer
ORDER By MAX(InvoiceDate) DESC

什么版本的SQL Server(我想可能是Sybase)?您好,谢谢,我第一次就这样使用它。首先,它给出了一个错误,InvoiceDate应该在Group by中,然后我将它添加到Group by中,并返回具有不同属性的相同客户InvoiceDates@wilest-订单中需要MAX(发票日期),这就是原因。我已经更新了我的答案。