【发布时间】:2016-09-07 16:15:45
【问题描述】:
CREATE PROCEDURE [dbo].[GetIdleCustomerlist](
@num_months Int
)
AS
BEGIN
SELECT DISTINCT
cust.cust_code as cust_code ,
cust.name as cust_name,
MAX (invoice.created_date) as Last_invoice_date
FROM [dbo].[customer] cust LEFT JOIN [dbo].[crm_invoice_header] invoice on cust.cust_code = invoice.cust_code
GROUP BY cust.cust_code ,cust.name,invoice.created_date
HAVING (( MAX (invoice.created_date)< DATEADD(MONTH, -@num_months ,GETDATE())) OR invoice.created_date IS NULL)
ORDER BY CAST (cust.cust_code AS int) ASC
END
我想删除重复的客户代码,但 DISTINCT 关键字给了我这个错误。
消息 145,级别 15,状态 1,过程 GetIdleCustomerlist,第 21 行 如果指定了 SELECT DISTINCT,则 ORDER BY 项必须出现在选择列表中。
【问题讨论】:
标签: sql sql-server stored-procedures