-1

演示

SELECT ROW_NUMBER() OVER (ORDER BY Visit_Date DESC,P_Master.P_Name ASC) AS RowNumber, 
P_Master.P_Name,
(SELECT MAX(Visit_Date) FROM P_Visit v WHERE v.PID = p_Master.PID) as Visit_Date
FROM P_Master
LEFT JOIN P_VISIT
ON P_Master.PID=P_VISIT.PID
WHERE P_Master.P_Name LIKE 'j%'

如果我在“2013-10-29”上搜索,我必须显示 Visit_Date 列的最大日期,然后记录显示这些客户在列中有日期“2013-10-29”我必须显示客户的最大日期所有工作正常但如何按 PID 分组(我必须为给定的搜索只显示一条记录)

4

2 回答 2

1

请尝试以下查询,它也有助于分页

SELECT ROW_NUMBER() OVER (ORDER BY Visit_Date DESC,P_Name ASC) AS RowNumber, * 
FROM (SELECT DISTINCT P_Master.P_Name,
(SELECT MAX(Visit_Date) FROM P_Visit v WHERE v.PID = p_Master.PID) as Visit_Date
FROM P_Master
LEFT JOIN P_VISIT
ON P_Master.PID=P_VISIT.PID
WHERE P_Master.P_Name LIKE 'j%') tbl
于 2013-11-12T07:47:22.983 回答
0

这将起作用:

  SELECT DISTINCT
  P_Master.P_Name,
  (SELECT MAX(Visit_Date) FROM P_Visit v WHERE v.PID = p_Master.PID) as Visit_Date
  FROM P_Master
  LEFT JOIN P_VISIT
  ON P_Master.PID=P_VISIT.PID
  WHERE P_Master.P_Name LIKE 'j%'
于 2013-11-12T05:42:01.210 回答