Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 need help to find out the lowest value

Author  Topic 

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-12-03 : 06:24:35
My table and data as following,

declare @t1 table
(idx int identity, whichMonth varchar(10),
acct1 decimal(10,2), acct2 decimal(10,2), acct3 decimal(10,2));

insert into @t1(whichMonth, acct1, acct2, acct3) values('jan',0.00,450.00,900);
insert into @t1(whichMonth, acct1, acct2, acct3) values('feb',0.00,450.00,100);
insert into @t1(whichMonth, acct1, acct2, acct3) values('mar',0.00,450.00,300);
insert into @t1(whichMonth, acct1, acct2, acct3) values('apr',400.00,450.00,900);
insert into @t1(whichMonth, acct1, acct2, acct3) values('may',0.00,450.00,1200);
insert into @t1(whichMonth, acct1, acct2, acct3) values('jun',230.00,450.00,900);
insert into @t1(whichMonth, acct1, acct2, acct3) values('jul',0.00,1450.00,2300);
insert into @t1(whichMonth, acct1, acct2, acct3) values('aug',0.00,136.00,1900);
insert into @t1(whichMonth, acct1, acct2, acct3) values('sep',0.00,0.00,2300);
insert into @t1(whichMonth, acct1, acct2, acct3) values('oct',0.00,2000.00,900);
insert into @t1(whichMonth, acct1, acct2, acct3) values('nov',0.00,340.00,900);
insert into @t1(whichMonth, acct1, acct2, acct3) values('dec',0.00,0.00,900);


I want to return the lowest saving. The lowest saving in above table is 136.00

How my SQL looks like to return the lowest saving? My expected result as following,

the lowest saving
--------------------
136.00


Really hope somebody can help me

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-12-03 : 06:38:25
Not clear what you want
maybe

select [the lowest saving] = min(acct2) from @t1
or maybe

select [the lowest saving] = min(s) from
(
select s = min(acct1) from @t1
union all
select s = min(acct2) from @t1
union all
select s = min(acct3) from @t1
) a

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-12-03 : 06:58:49
I think

select [the lowest saving] = min(s) from
(
select s = min(acct1) from @t1
union all
select s = min(acct2) from @t1
union all
select s = min(acct3) from @t1
) a

Go to Top of Page
   

- Advertisement -