我试图根据其中一列的最大日期撤回一行详细信息。做一个简单的max(date.field)对我的表不起作用。我最初通过利用我使用此资源构建的查询来解决了这个问题See the entry by Tom H.:
SELECT
HBM_CLIENT.CLIENT_CODE,
HBM_NAME.NAME AS CLIENT_NAME,
PART_BILL1.PART_CAT_CODE AS BILLING_CODE,
PART_BILL1.EMPL_UNO AS BILLING_NAME_UNO,
HBM_PERSNL_BILL.EMPLOYEE_NAME AS BILLING_NAME,
PART_BILL1.PERCENTAGE AS BILLING_PERCENTAGE
FROM
HBM_CLIENT
INNER JOIN
HBM_NAME ON HBM_CLIENT.NAME_UNO = HBM_NAME.NAME_UNO
LEFT OUTER JOIN
TBM_CLMAT_PART AS PART_BILL1 ON PART_BILL1.CLIENT_UNO = HBM_CLIENT.CLIENT_UNO
AND PART_BILL1.PART_CAT_CODE = 'BILL'
LEFT OUTER JOIN
TBM_CLMAT_PART AS PART_BILL2 ON PART_BILL2.CLIENT_UNO = HBM_CLIENT.CLIENT_UNO
AND PART_BILL2.EFF_DATE > PART_BILL1.EFF_DATE
AND PART_BILL1.PART_CAT_CODE = 'BILL'
LEFT OUTER JOIN
HBM_PERSNL AS HBM_PERSNL_BILL ON PART_BILL1.EMPL_UNO = HBM_PERSNL_BILL.EMPL_UNO
GROUP BY
HBM_CLIENT.CLIENT_CODE, HBM_NAME.NAME,
PART_BILL1.PART_CAT_CODE, PART_BILL1.EMPL_UNO,
HBM_PERSNL_BILL.EMPLOYEE_NAME,
PART_BILL1.PERCENTAGE,
ORDER BY
HBM_CLIENT.CLIENT_CODE
此查询的问题在于,由于某种原因,它不会撤回所有结果,原因在于我使用的解决方案。
我想知道使用case语句是否更有意义,但我对case语句不是很熟悉。
以下是我正在处理的查询:
`SELECT
HBM_CLIENT.CLIENT_CODE,
HBM_NAME.NAME AS CLIENT_NAME,
PART_BILL1.PART_CAT_CODE AS BILLING_CODE,
PART_BILL1.EMPL_UNO AS BILLING_NAME_UNO,
HBM_PERSNL_BILL.EMPLOYEE_NAME AS BILLING_NAME,
PART_BILL1.PERCENTAGE AS BILLING_PERCENTAGE,
Part_BILL1.EFF_DATE,
CASE
WHEN
MAX(Part_BILL1.EFF_DATE) > Part_BILL1.EFF_DATE THEN max(Part_BILL1.EFF_DATE) ELSE Part_BILL1.EFF_DATE END
FROM HBM_CLIENT
INNER JOIN HBM_MATTER
ON HBM_CLIENT.CLIENT_UNO = HBM_MATTER.CLIENT_UNO
INNER JOIN HBM_NAME
ON HBM_CLIENT.NAME_UNO = HBM_NAME.NAME_UNO
LEFT OUTER JOIN TBM_CLMAT_PART AS PART_BILL1
ON PART_BILL1.CLIENT_UNO = HBM_CLIENT.CLIENT_UNO
AND PART_BILL1.PART_CAT_CODE = 'BILL'
LEFT OUTER JOIN HBM_PERSNL AS HBM_PERSNL_BILL
ON PART_BILL1.EMPL_UNO = HBM_PERSNL_BILL.EMPL_UNO
WHERE
(HBM_CLIENT.CLIENT_CODE = '065011')
GROUP BY
HBM_CLIENT.CLIENT_CODE,
HBM_NAME.NAME,
PART_BILL1.PART_CAT_CODE,
PART_BILL1.EMPL_UNO,
HBM_PERSNL_BILL.EMPLOYEE_NAME,
PART_BILL1.PERCENTAGE,
Part_BILL1.EFF_DATE
HAVING
(Part_BILL1.EFF_DATE = CASE
WHEN MAX(Part_BILL1.EFF_DATE) > Part_BILL1.EFF_DATE THEN max(Part_BILL1.EFF_DATE) ELSE Part_BILL1.EFF_DATE END)`
它会运行,但它会让我返回两行而不是一行,即它不会拉动最大值。我正在使用T-SQL并使用Visual Studio编写SSRS。
我很乐意回答任何问题。如果我的解释不是很好,我会提前道歉。
| CLIENT_CODE | CLIENT_NAME | BILLING_NAME | EFF_DATE | PERCENTAGE |
| ------------+--------------+--------------+----------+------------|
| 123456 | Entity, Inc. | Attorney A | 1/1/1990 |50% |
| 123456 | Entity, Inc. | Attorney B | 1/1/1990 |50% |
| 123456 | Entity, Inc. | Attorney B | 1/1/2017 |50% |
| 123456 | Entity, Inc. | Attorney C | 1/1/2017 |50% |
答案 0 :(得分:0)
使用带有SELECT TOP 1
子句的ORDER BY
查询是否可以简化这一点?
这样的事情:
SELECT TOP 1
HBM_CLIENT.CLIENT_CODE,
HBM_NAME.NAME AS CLIENT_NAME,
PART_BILL1.PART_CAT_CODE AS BILLING_CODE,
PART_BILL1.EMPL_UNO AS BILLING_NAME_UNO,
HBM_PERSNL_BILL.EMPLOYEE_NAME AS BILLING_NAME,
PART_BILL1.PERCENTAGE AS BILLING_PERCENTAGE,
Part_BILL1.EFF_DATE,
....
FROM HBM_CLIENT
INNER JOIN HBM_MATTER
ON HBM_CLIENT.CLIENT_UNO = HBM_MATTER.CLIENT_UNO
INNER JOIN HBM_NAME
ON HBM_CLIENT.NAME_UNO = HBM_NAME.NAME_UNO
LEFT OUTER JOIN TBM_CLMAT_PART AS PART_BILL1
ON PART_BILL1.CLIENT_UNO = HBM_CLIENT.CLIENT_UNO
AND PART_BILL1.PART_CAT_CODE = 'BILL'
LEFT OUTER JOIN HBM_PERSNL AS HBM_PERSNL_BILL
ON PART_BILL1.EMPL_UNO = HBM_PERSNL_BILL.EMPL_UNO
WHERE
(HBM_CLIENT.CLIENT_CODE = '065011')
ORDER BY Part_BILL1.EFF_DATE DESC
这应该只给你EFF_DATE的最新行,而不需要任何特殊的分组或案例逻辑。
编辑:在我看来,获得所需结果的最佳方法是利用
TOP
子句。通常,在编写查询时 像这样,你会写SELECT TOP 1
或SELECT TOP 1000
,但是TOP
子句也支持各种功能。您可以使用 子查询,计算,变量以及WITH TIES
获得你想要的功能。在您的情况下,您希望查询返回任意数量的行,这么长 因为它们都具有相同的
EFF_DATE
值,这也是。{ 表格中给定EFF_DATE
的最大CLIENT_CODE
值。因此,我建议只添加
WITH TIES
参数TOP
子句用于获取您正在寻找的功能:SELECT TOP 1 WITH TIES HBM_CLIENT.CLIENT_CODE, HBM_NAME.NAME AS CLIENT_NAME, PART_BILL1.PART_CAT_CODE AS BILLING_CODE, PART_BILL1.EMPL_UNO AS BILLING_NAME_UNO, HBM_PERSNL_BILL.EMPLOYEE_NAME AS BILLING_NAME, PART_BILL1.PERCENTAGE AS BILLING_PERCENTAGE, Part_BILL1.EFF_DATE, .... FROM HBM_CLIENT INNER JOIN HBM_MATTER ON HBM_CLIENT.CLIENT_UNO = HBM_MATTER.CLIENT_UNO INNER JOIN HBM_NAME ON HBM_CLIENT.NAME_UNO = HBM_NAME.NAME_UNO LEFT OUTER JOIN TBM_CLMAT_PART AS PART_BILL1 ON PART_BILL1.CLIENT_UNO = HBM_CLIENT.CLIENT_UNO AND PART_BILL1.PART_CAT_CODE = 'BILL' LEFT OUTER JOIN HBM_PERSNL AS HBM_PERSNL_BILL ON PART_BILL1.EMPL_UNO = HBM_PERSNL_BILL.EMPL_UNO WHERE (HBM_CLIENT.CLIENT_CODE = '065011') ORDER BY Part_BILL1.EFF_DATE DESC
TOP
子句的各种用法的完整解释可以是 找到here.简要解释一下,
WITH TIES
参数意味着你想要的 查询以基于值返回数据集的TOP X
行 在ORDER BY
子句中 - 在您的情况下,EFF_DATE
。所以,如果有的话 两行EFF_DATE
中具有相同值,或三行或五行 一百,他们都会被归还,因为你是有效的 搜索不是TOP 1 ROW
,而是搜索TOP 1 VALUE of EFF_DATE
希望有所帮助。