Home All Groups Group Topic Archive Search About
Author
7 Jul 2005 2:01 PM
RP
Hi,
This is my scenario .... I have 2 columns .. with values like this.
col1     col2
3432     545465
0          6545656
543        54654
54          6456456456
54            45

i want to find out the non-zero minimum out of 2 columns.
so i wrote a fn. which takes min(col1),min(col2) and gives me the output.
but in this case i dont want to send 0,45 but i want to send non-zero minumum
so it would be 54,45. how can  i do this ?

Thanks
RP

Author
7 Jul 2005 2:08 PM
Aaron Bertrand [SQL Server MVP]
What do you want to do if a column is all negative values?




Show quote
"RP" <R*@discussions.microsoft.com> wrote in message
news:6FD1E148-EB4D-4C83-B923-901EA14DD4FF@microsoft.com...
> Hi,
> This is my scenario .... I have 2 columns .. with values like this.
> col1     col2
> 3432     545465
> 0          6545656
> 543        54654
> 54          6456456456
> 54            45
>
> i want to find out the non-zero minimum out of 2 columns.
> so i wrote a fn. which takes min(col1),min(col2) and gives me the output.
> but in this case i dont want to send 0,45 but i want to send non-zero
> minumum
> so it would be 54,45. how can  i do this ?
>
> Thanks
> RP
>
Author
7 Jul 2005 3:11 PM
RP
By the way, i dont have any -ve numbers. it starts from 0

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> What do you want to do if a column is all negative values?
>
>
>
>
> "RP" <R*@discussions.microsoft.com> wrote in message
> news:6FD1E148-EB4D-4C83-B923-901EA14DD4FF@microsoft.com...
> > Hi,
> > This is my scenario .... I have 2 columns .. with values like this.
> > col1     col2
> > 3432     545465
> > 0          6545656
> > 543        54654
> > 54          6456456456
> > 54            45
> >
> > i want to find out the non-zero minimum out of 2 columns.
> > so i wrote a fn. which takes min(col1),min(col2) and gives me the output.
> > but in this case i dont want to send 0,45 but i want to send non-zero
> > minumum
> > so it would be 54,45. how can  i do this ?
> >
> > Thanks
> > RP
> >
>
>
>
Author
7 Jul 2005 2:17 PM
Itzik Ben-Gan
How about:

select top 1 *
from t1
where col1 <> 0 and col2 <> 0
order by col1, col2

--
BG, SQL Server MVP
www.SolidQualityLearning.com


Show quote
"RP" <R*@discussions.microsoft.com> wrote in message
news:6FD1E148-EB4D-4C83-B923-901EA14DD4FF@microsoft.com...
> Hi,
> This is my scenario .... I have 2 columns .. with values like this.
> col1     col2
> 3432     545465
> 0          6545656
> 543        54654
> 54          6456456456
> 54            45
>
> i want to find out the non-zero minimum out of 2 columns.
> so i wrote a fn. which takes min(col1),min(col2) and gives me the output.
> but in this case i dont want to send 0,45 but i want to send non-zero
> minumum
> so it would be 54,45. how can  i do this ?
>
> Thanks
> RP
>
Author
7 Jul 2005 2:44 PM
Aaron Bertrand [SQL Server MVP]
> select top 1 *
> from t1
> where col1 <> 0 and col2 <> 0
> order by col1, col2

This will only work if both min values are in the same row; I think the OP's
sample data was convenient but it sounded like he wanted the min in each
column individually.  If so, maybe we can do it easiest with a UNION:

CREATE TABLE blat
(
col1 BIGINT,
col2 BIGINT
)
GO
INSERT blat SELECT 3432,545465
INSERT blat SELECT 0   ,6545656
INSERT blat SELECT 543 ,54654
INSERT blat SELECT 54  ,6456456456
INSERT blat SELECT 54  ,450
INSERT blat SELECT 78  ,45
GO

SELECT MAX(col1), MAX(col2)
FROM
(
SELECT col1 = MIN(col1), col2 = 0 FROM blat WHERE col1>0
UNION ALL
SELECT col2 = 0, col2 = MIN(col2) FROM blat WHERE col2>0
) x
GO

DROP TABLE blat
Author
7 Jul 2005 3:08 PM
Itzik Ben-Gan
Oh, I thought RP was after the row with the min col1, and within it the min
col2. I might have been wrong.

--
BG, SQL Server MVP
www.SolidQualityLearning.com


Show quote
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:efwBmJwgFHA.2840@tk2msftngp13.phx.gbl...
>> select top 1 *
>> from t1
>> where col1 <> 0 and col2 <> 0
>> order by col1, col2
>
> This will only work if both min values are in the same row; I think the
> OP's sample data was convenient but it sounded like he wanted the min in
> each column individually.  If so, maybe we can do it easiest with a UNION:
>
> CREATE TABLE blat
> (
> col1 BIGINT,
> col2 BIGINT
> )
> GO
> INSERT blat SELECT 3432,545465
> INSERT blat SELECT 0   ,6545656
> INSERT blat SELECT 543 ,54654
> INSERT blat SELECT 54  ,6456456456
> INSERT blat SELECT 54  ,450
> INSERT blat SELECT 78  ,45
> GO
>
> SELECT MAX(col1), MAX(col2)
> FROM
> (
> SELECT col1 = MIN(col1), col2 = 0 FROM blat WHERE col1>0
> UNION ALL
> SELECT col2 = 0, col2 = MIN(col2) FROM blat WHERE col2>0
> ) x
> GO
>
> DROP TABLE blat
>
>
Author
7 Jul 2005 3:14 PM
Aaron Bertrand [SQL Server MVP]
Not sure, we could both be wrong.  Not enough sample data or clear specs.
Show quote
:-(



"Itzik Ben-Gan" <it***@REMOVETHIS.SolidQualityLearning.com> wrote in message
news:uR$ccWwgFHA.2840@tk2msftngp13.phx.gbl...
> Oh, I thought RP was after the row with the min col1, and within it the
> min col2. I might have been wrong.
Author
7 Jul 2005 3:10 PM
RP
AAron,
Thanks for the reply. What i really want to do is -
I want to take a  non-zero minimum out of both the columns .  I didnt know
what to do so i was taking min from both the columns and then sending that to
a function which will again give me the min of the result.
present scenarion

anotherfn(min(col1),min(col2))
another fn wiill give me the minimum of both the numbers.
Is there anyway i can do that in one shot ??? (Take NONZERO minimum from
both the columns ????

Thanks a bunch.
RP

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> > select top 1 *
> > from t1
> > where col1 <> 0 and col2 <> 0
> > order by col1, col2
>
> This will only work if both min values are in the same row; I think the OP's
> sample data was convenient but it sounded like he wanted the min in each
> column individually.  If so, maybe we can do it easiest with a UNION:
>
> CREATE TABLE blat
> (
>  col1 BIGINT,
>  col2 BIGINT
> )
> GO
> INSERT blat SELECT 3432,545465
> INSERT blat SELECT 0   ,6545656
> INSERT blat SELECT 543 ,54654
> INSERT blat SELECT 54  ,6456456456
> INSERT blat SELECT 54  ,450
> INSERT blat SELECT 78  ,45
> GO
>
> SELECT MAX(col1), MAX(col2)
> FROM
> (
>  SELECT col1 = MIN(col1), col2 = 0 FROM blat WHERE col1>0
>  UNION ALL
>  SELECT col2 = 0, col2 = MIN(col2) FROM blat WHERE col2>0
> ) x
> GO
>
> DROP TABLE blat
>
>
>

AddThis Social Bookmark Button