1

我创建了以下查询来检索我的律师事务所的各种客户和事项信息。- 本质上,我们需要查看过去三年谁为哪些客户处理了哪些问题。

除了事务创建日期之外,我们现在还被要求检索交易日期 - 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
4

1 回答 1

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 
于 2013-03-27T10:18:15.317 回答