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
 General SQL Server Forums
 New to SQL Server Programming
 udf or nested case when's

Author  Topic 

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-03-16 : 09:50:43
I am looking for a way to check against 10 or so values. They are basically in sets of 2 (cola/colb go together, colc/cold, etc...).
I need to check the first columns to see if they are greater than 0, and depending which ones are, check the corresponding second columns to see which ones of those are greater than 0, and if there are any, take the minimum of those values. If this second check fails, I need to take a maximum of the second columns, otherwise if all checks fail, I need to return NULL.

I've never created a udf before but have used case when's. Just looking for some advice on an efficient way of doing these many checks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 09:52:02
can you show some sample data and explain what you want?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-03-16 : 10:02:14
This is how my table looks now.
a,b,c,d,e,f,g,h
0,0,0,0,0,0,0,0
5,3,7,0,4,8,1,3
0,0,5,7,2,0,4,8

This is how my table should look after (just adding a column)
a,b,c,d,e,f,g,h,i
0,0,0,0,0,0,0,0,NULL (or 0 if it can't be null)
5,3,7,0,4,8,1,3,1
0,0,5,7,2,0,4,8,4

a,c,e,g are prices, b,c,d,f,h are inventories if this helps (can't use a price if there is no inventory).
First thing - check if prices are 0 (if they are can't use)
Second thing - when price is >0, check if it has stock
Third thing - column i = minimum of all prices with stock
Fourth thing - if nothing has stock, but there are prices, column i = maximum of all prices
Fifth thing - if nothing has prices greater than 0, then column i= Null (or 0).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 10:05:16
how do you identify which price is for which stock?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-03-16 : 10:08:16
As in the first post, they are basically in sets of 2.
a,b - price,stock
b,c - price,stock
c,d - price,stock
e,f - price,stock
g,h - price,stock

It's arranged like this because there are 5 different suppliers. The prices and stocks are not stored in any other database/table (stupid design, but that's how I took it over).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 10:09:48
its not a good method to store it like this. have you heard of normalisation?why cant you store it row wise?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-03-16 : 10:17:39
I was hoping to not have to redo the design. Ya, I've heard of nomalization.

Oh well, guess I'm off to redesigning...
Go to Top of Page
   

- Advertisement -