如何在SQL中仅显示每个字段组的最高结果

时间:2014-09-17 14:12:23

标签: sql sql-server

我一直在尝试编写一个存储过程,该存储过程将为登录用户的每个属性ID和合同号获取最高租户。

我已经能够让SQL能够完成其中的大部分工作而不是我只需要抓住每个组的顶部,我需要它。

这是我第一次不得不这样做,因此有点难以解决。也许如果你看到下面的例子,它将清楚最终结果应该是什么......

我使用的代码是:

SELECT              Id,
                    PropertyId,
                    Name + ', ' + Address1,
                    contractNumber

FROM                PropertyTenant 

WHERE               PropertyId IN (SELECT PropertyId FROM UserProperty WHERE UserId = '5E541037-747C-43B9-BB9F-97C8728083B7')
AND                 ContractNumber BETWEEN 1 AND 10;

我得到的结果

(租户名称和地址在实际情况下会有所不同,但这只是我使用的虚拟数据)

Id  PropertyId  Tenant Name and address             contractNumber
142 1501        chris devine, 12                    1
143 1501        steven mo, 12                       1
144 1502        chris devine, 12,whitebeam close    1
145 1502        steven, 12,whitebeam close          2
146 1503        sdf adsf, asda                      1
147 1504        dddd ddds, sdsdwsd                  1
160 1532        Christopher, 1                      1
161 1532        Christopher Devine, 6               1
162 1532        Christopher Devine, 11              2
163 1532        Christopher Devine, 16              2
164 1529        Christopher Devine, 12, windy       1
165 1529        Christopher Devine, 12 dog          1
166 1523        Christopher Devine, asdfasiuuh      1
167 1523        Christopher Devine, asdasd          1
182 1538        chris devine, 12 whitebam           1
183 1538        chris devine, 12, whitebeam close   2
184 1538        chris devine, 12asd                 1
185 1561        chris devine, 12 whitebeam          1

我想要和需要的结果应该是:

Id  PropertyId  Tenant Name and address             contractNumber
142 1501        chris devine, 12                    1
144 1502        chris devine, 12,whitebeam close    1
145 1502        steven, 12,whitebeam close          2
146 1503        sdf adsf, asda                      1
147 1504        dddd ddds, sdsdwsd                  1
160 1532        Christopher, 1                      1
162 1532        Christopher Devine, 11              2
164 1529        Christopher Devine, 12, windy       1
167 1523        Christopher Devine, asdasd          1
182 1538        chris devine, 12 whitebam           1
183 1538        chris devine, 12, whitebeam close   2
185 1561        chris devine, 12 whitebeam          1

任何帮助都会非常感谢,因为我认为我会以完全错误的方式解决这个问题:(

3 个答案:

答案 0 :(得分:2)

这是一种方法。

   with MyCTE as
(
    SELECT pt.Id
        , pt.PropertyId
        , pt.Name + ', ' + pt.Address1 as tenantinfo
        , pt.contractNumber
        , ROW_NUMBER() over (PARTITION BY PropertyID, contractnumber order by id asc) as RowNum
    FROM PropertyTenant pt
    WHERE pt.PropertyId IN 
    (
        SELECT PropertyId 
        FROM UserProperty 
        WHERE UserId = '5E541037-747C-43B9-BB9F-97C8728083B7'
    )
    AND ContractNumber BETWEEN 1 AND 10
)

select *
from MyCTE
where RowNum = 1
order by id asc

答案 1 :(得分:1)

试试这个: -

SELECT          Min(Id),
                PropertyId,
                Name + ', ' + Address1,
                contractNumber

FROM  PropertyTenant 

WHERE PropertyId IN (SELECT PropertyId 
                     FROM UserProperty 
                     WHERE UserId = '5E541037-747C-43B9-BB9F-97C8728083B7')
AND ContractNumber BETWEEN 1 AND 10
GROUP BY PropertyId, Name + ', ' + Address1, contractNumber

希望这会对你有所帮助。

答案 2 :(得分:0)

我认为您需要使用ROW_NUMBER()Common Table Expression函数:

;with ctePropertyTenant
as
(
    SELECT
        Id,
        PropertyId,
        Name + ', ' + Address1,
        contractNumber,
        ROW_NUMBER() OVER(PARTITION BY PropertyId, contractNumber ORDER BY Id) as PropertyRow
    FROM
        PropertyTenant 
    WHERE
        PropertyId IN (SELECT PropertyId FROM UserProperty WHERE UserId = '5E541037-747C-43B9-BB9F-97C8728083B7') AND
        ContractNumber BETWEEN 1 AND 10
)
Select
    *
from
    ctePropertyTenant
where
    PropertyRow = 1