例如,我这样做
select * from tblClientInfo
with some condition
有2条记录:
ClientID=001, Salary=500.
ClientID=001, Salary=550.
如何仅选择薪水最高的记录?仅供参考,可能还有2条以上的记录。
我知道如何进行升序排序,但是我不知道如何选择其中之一。
这是我的代码
Select A.AgencyID,19,291,
BETAC.FirstName,BETAC.LastName,BETAC.Phone,BETAC.EMailAddress,BETAC.AnnualIncome,BETAC.Occupants,BETAC.Children2,
BETAC.Children5,ISNULL(BETAC.Children17, 0) ,BETAC.Elderlys,BETAC.Disableds,
0,0,TKFT.FuelTypeID,TKV1.LookupValueID,
BETAC.LandlordName,BETAC.LandlordAddress,BETAC.LandlordCity,BETAC.LandlordState,BETAC.LandlordZip,
BETAC.LandlordPhone,BETAC.HeatInRent,BETAC.DHWInRent,'2018/01/01',BETAC.FirstPrintedDate,
BETAC.StatusDate,NULL,NULL,ISNULL(BETAC.PovertyLevel,0),
BETAC.HighUse,BETAC.HighBurden,ISNULL(BETAC.Section8Housing,0),NULL,NULL,0,BETAC.IsRequestWNZ,
BETAC.SignatureConfirmation,BETAC.MoveInOut,CO.CountyID,
0,0,0,1,BETAC.BenefitAmount,0,1,U.UserID,BETAC.ClientNumber
from LABeta_FA.dbo.tblClientInfo as BETAC
join LABeta_FA.dbo.tgAgency as BETAA on BETAA.AgencyID=BETAC.AgencyID
join tgAgency as A on BETAA.Phone COLLATE DATABASE_DEFAULT=A.Phone COLLATE DATABASE_DEFAULT
join LABeta_FA.dbo.tgClientStatus as LACS on LACS.ClientStatusID=BETAC.ClientStatusID
--join tlkpValues as TKV on TKV.DisplayName COLLATE DATABASE_DEFAULT=LACS.ClientStatus COLLATE DATABASE_DEFAULT
join LABeta_FA.dbo.tlkpHeatingSource as LATKHS on LATKHS.HeatingID=BETAC.FuelTypeID
join tlkpFuelType as TKFT on TKFT.FuelType COLLATE DATABASE_DEFAULT=LATKHS.HeatingSource COLLATE DATABASE_DEFAULT
join LABeta_FA.dbo.tlkpBuildingType as TKBT on TKBT.BuildingTypeID=BETAC.BuildingTypeID
join tlkpValues as TKV1 on TKV1.DisplayName COLLATE DATABASE_DEFAULT=TKBT.BuildingType COLLATE DATABASE_DEFAULT
join LABeta_FA.dbo.tgUser as LAU on LAU.UserID=BETAC.UserID
join tgUser as U on U.UserName COLLATE DATABASE_DEFAULT=LAU.UserName COLLATE DATABASE_DEFAULT
join LABeta_FA.dbo.tgCounty as LACO on LACO.CountyID=BETAC.CountyID
join tgCounty as CO on CO.County COLLATE DATABASE_DEFAULT=LACO.County COLLATE DATABASE_DEFAULT
where BETAC.LastDate>'01/01/2018' and
BETAC.FirstName='Brandon'
然后结果就像
brandon benefitamount=400 date=2018/01/01
brandon benefitamount=450 date=2019/01/01
我希望结果是最高的收益金额。
答案 0 :(得分:1)
使用max()
select max(Salary) from tblClientInfo where ClientID=001
如果要选择表的所有列,则可以使用子查询
select * from tblClientInfo
where Salary=(select max(Salary) from tblClientInfo where ClientID=001
) and ClientID=001
答案 1 :(得分:1)
ROW_Number()
像波纹管一样
;With cte as (
select *, ROW_NUMBER() Over(partition by ClientID order by Salary DESC) as RN from tblClientInfo
)
Select * from cte where RN=1
答案 2 :(得分:0)
您可以使用这个:
Select * from tblClientInfo where ClientID=001 order by Salary desc limit 1
答案 3 :(得分:0)
您将使用order by
和仅提取一行的某种方法。在标准SQL中,这将是:
select t.*
from t
where clientID = '001'
order by t.salary dec
fetch first 1 row only;
并非所有数据库都支持fetch first
。在某些情况下,您使用top (1)
。在其他情况下,limit
甚至更深奥的构造。
答案 4 :(得分:0)
使用行号作为子查询:
row_number是提供结果并对其进行排序的功能。有序列表根据分区(clientID)重新开始,并根据顺序提供顺序(第一个Benefitamount desc和第二个dte desc)。
select *
from(
select [all of your columns and they must have alias]
, rn = Row_NUMBER() over (partition by ClientID Order by benefitamount desc, dte desc)
from [your tables and joins]
) a
where a.rn=1 -- returns the ordered result from above
答案 5 :(得分:0)
您只需要使用max(Salary)和一个子查询进行分组,这将帮助您为每个分组提供完整记录
Select * from table where
clientId,Salary
IN ( Select clientId, max(Salary) from
table
group by clientId)