1

我试图在 case 语句中获取一条记录,我想将 seelect 语句放在哪里(1)

 CASE WHEN CAST(VehicleCC AS MONEY) BETWEEN -1 AND 50 THEN '33.94'
                          WHEN CAST(VehicleCC AS MONEY) BETWEEN 51 AND 125 THEN '39.65' 
                          WHEN CAST(VehicleCC AS  MONEY) BETWEEN 126 AND 500 THEN '55.90' 
                          WHEN CAST(VehicleCC AS  MONEY) BETWEEN 501 AND 650 THEN '67.31'
                          WHEN CAST(VehicleCC AS MONEY) BETWEEN 651 AND 1000 THEN '78.65' 
                          WHEN CAST(VehicleCC AS MONEY ) > 1001 THEN '79.85' 
                          else 
                           (SELECT TOP(1) cast (VehicleCC as varchar (50)) FROM  HIRE_INSTRUCTION HIRP 
                              INNER JOIN VEHICLE VEH ON HIRP.FKFleetVehicleID=VEH.vehicleid
                              WHERE HIRP.FKBLDRefID=cl.FKBLDRefID AND HIRP.Deleted=0 AND VEH.Deleted=0
                                ORDER BY veh.VehicleCC DESC) END AS [Hire Rate Charged Per Day],
4

2 回答 2

0

您可以使用 set rowcount 来限制查询返回的行数。

set rowcount 1 -- the query will return one record

select * from table 

set rowcount 0 --back to normal
于 2013-10-26T03:42:36.203 回答
0

如果 TOP(1) 不起作用,请尝试 RANK():

SELECT VehicleCC
FROM (
    SELECT CAST(VEH.VehicleCC as varchar (50)),
        RANK() OVER(ORDER BY CAST(VEH.VehicleCC as varchar (50))) AS CCRank
    FROM  HIRE_INSTRUCTION HIRP 
        INNER JOIN VEHICLE VEH 
            ON HIRP.FKFleetVehicleID=VEH.vehicleid
    WHERE HIRP.FKBLDRefID=cl.FKBLDRefID 
        AND HIRP.Deleted=0 
        AND VEH.Deleted=0
    ORDER BY veh.VehicleCC DESC
    ) CC
WHERE CCRank = 1
于 2013-10-25T23:37:43.530 回答