为什么select min() 没有返回最小值
SELECT * FROM wwwgx WHERE (CARDNO = 182956)
cardno balance vailddate
182956 369.42 20201231
182956 33.75 20201231
182956 164.07 20201231
182956 98.05 20201231
182956 78.95 20201231
SELECT CARDNO, MIN(BALANCE) FROM wwwgx WHERE (CARDNO = 182956) GROUP BY CARDNO
182956 164.07
为什么返回的不是33.75,而是164.07,这句sql用的不对吗?请高手指点。
------解决方案----------------------> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
GO
create table [TB]([cardno] int,[balance] varchar(7),[vailddate] datetime)
insert [TB]
select 182956,'369.42','20201231' union all
select 182956,'33.75','20201231' union all
select 182956,'164.07','20201231' union all
select 182956,'98.05','20201231' union all
select 182956,'78.95','20201231'
SELECT CARDNO, MIN(CAST(BALANCE AS DECIMAL(8,4))) FROM [TB] WHERE (CARDNO = 182956) GROUP BY CARDNO
/*
CARDNO
----------- ---------------------------------------
182956 33.7500
(1 行受影响)
*/
drop table [TB]
------解决方案--------------------SELECT CARDNO, MIN(cast(BALANCE as float)) FROM wwwgx WHERE (CARDNO = 1310500182956) GROUP BY CARDNO
因为 BALANCE 字段定义为 varchar 类型的 ,需要转换成数字
------解决方案--------------------balance字段类型是非数字类型(可能是字符型)的,所以1字开头的最小。