为什么min()没有返回最小值
SELECT * FROM wwwgx WHERE (CARDNO = 1310500182956)
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 = 1310500182956) GROUP BY CARDNO
182956 164.07
为什么返回的不是33.75,而是164.07,这句sql用的不对吗?请高手指点。
------最佳解决方案--------------------CARDNO = 1310500182956
数据库里有这样的数吗?
------其他解决方案--------------------cardno的前几位打字时没有显示出来,看sql语句没有问题的
------其他解决方案--------------------SELECT CARDNO, MIN(cast(BALANCE as float)) FROM wwwgx WHERE (CARDNO = 1310500182956) GROUP BY CARDNO
------其他解决方案--------------------SELECT CARDNO, MIN(BALANCE) FROM wwwgx WHERE (CARDNO = 182956) GROUP BY CARDNO
不应该是这样的吗?
------其他解决方案--------------------SELECT CARDNO, MIN(BALANCE) FROM wwwgx WHERE (CARDNO = 1310500182956) GROUP BY CARDNO
是不是where条件有问题,你好好检查下
------其他解决方案--------------------因为 BALANCE 字段定义为 varchar 类型的 ,需要转换成数字
------其他解决方案--------------------你的那个字段不是浮点类型的,是文本类型的
--> 测试数据:[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]