我创建了以下查询以检索我的各种Client和Matter信息 律师事务所。 - 基本上我们需要看看谁在哪个问题上工作过 客户(s)在过去三年。
除了物质创建日期,我们现在被要求撤回 交易日期 - TT.TRAN_DATE - (收费时间),让我们看看谁工作了 在最近具有更高准确度的事物上。问题在那里 将是每个客户/事项/收费者的多个交易日期,我们只是 确实需要查看最近的交易日期。
不幸的是,我很挣扎,时间很短。有人能告诉我如何修改 上面的查询,以便只返回每个的最大交易日期 客户/事项/收费者?日期也应与格式相同 现有的“事项创建日期”字段。
提前致谢,加文
SELECT DISTINCT HC.CLIENT_NAME AS [Client Name],
HC.CLIENT_CODE AS [Client Code],
HM.MATTER_CODE AS [Matter Code],
HM.CLNT_MATT_CODE AS [Client Matter Code],
convert(varchar,HM.OPEN_DATE,103) AS [Matter Creation Date],
HP.EMPLOYEE_CODE AS [Fee Earner Code],
HP.EMPLOYEE_NAME AS [Fee Earner Name],
TT.TRAN_DATE AS [Transaction Date]
FROM HBM_MATTER HM
LEFT OUTER JOIN TAT_TIME TT
ON HM.MATTER_UNO=TT.MATTER_UNO
LEFT JOIN HBM_CLIENT HC
ON HM.CLIENT_UNO=HC.CLIENT_UNO
LEFT JOIN HBM_PERSNL HP
ON TT.TK_EMPL_UNO=HP.EMPL_UNO
WHERE HM.INACTIVE='n'
AND HM.STATUS_CODE <> 'CLOSE'
AND HP.INACTIVE = 'N'
AND TT.TRAN_DATE > dateadd(month,-36,getdate())
ORDER BY HC.CLIENT_CODE, HM.MATTER_CODE
答案 0 :(得分:2)
假设您没有指定SQL Server,这是我的解决方案。我无法检查它,但我认为应该没问题:
WITH cte AS (SELECT DISTINCT
HC.CLIENT_NAME AS [Client Name]
,HC.CLIENT_CODE AS [Client Code]
,HM.MATTER_CODE AS [Matter Code]
,HM.CLNT_MATT_CODE AS [Client Matter Code]
,CONVERT(VARCHAR,HM.OPEN_DATE,103) AS [Matter Creation Date]
,HP.EMPLOYEE_CODE AS [Fee Earner Code]
,HP.EMPLOYEE_NAME AS [Fee Earner Name]
,TT.TRAN_DATE AS [Transaction Date]
,ROW_NUMBER() OVER(PARTITION BY HC.CLIENT_CODE, HM.MATTER_CODE, HP.EMPLOYEE_CODE
ORDER BY TT.TRAN_DATE DESC) AS trans_order
FROM HBM_MATTER HM
LEFT OUTER JOIN TAT_TIME TT ON HM.MATTER_UNO=TT.MATTER_UNO
LEFT OUTER JOIN HBM_CLIENT HC ON HM.CLIENT_UNO=HC.CLIENT_UNO
LEFT OUTER JOIN HBM_PERSNL HP ON TT.TK_EMPL_UNO=HP.EMPL_UNO
WHERE HM.INACTIVE = 'n'
AND HM.STATUS_CODE <> 'CLOSE'
AND HP.INACTIVE = 'N'
AND TT.TRAN_DATE > DATEADD(MONTH, -36, GETDATE())
)
SELECT * FROM cte
WHERE trans_order = 1
ORDER BY
CLIENT_CODE
,MATTER_CODE