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 2005 Forums
 Transact-SQL (2005)
 unary select on items IF then else outer join ??

Author  Topic 

stixoffire
Starting Member

17 Posts

Posted - 2008-04-17 : 01:34:05
I have a table of products, and many products have the same name only they are different sizes. I want to only select a particular size like 20 oz, but if it does not exist in that size I want to select the next appropriate size (and which case if it does not exist i wish to select the next appropriate size and so on ..
Does any one know how I can accomplish this and would be willing to help me out by posting some code, some direction ??

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2008-04-17 : 02:32:58
quote:
Originally posted by stixoffire

I have a table of products, and many products have the same name only they are different sizes. I want to only select a particular size like 20 oz, but if it does not exist in that size I want to select the next appropriate size (and which case if it does not exist i wish to select the next appropriate size and so on ..
Does any one know how I can accomplish this and would be willing to help me out by posting some code, some direction ??



and what if the size does exists then? whether search should continue? If not then try IN in your query. Like

Select {some cols} From {table} Where size IN {size1, size2, sizen}

confirm on this.

Mahesh
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-04-17 : 05:06:24
quote:
Originally posted by mahesh_bote

quote:
Originally posted by stixoffire

I have a table of products, and many products have the same name only they are different sizes. I want to only select a particular size like 20 oz, but if it does not exist in that size I want to select the next appropriate size (and which case if it does not exist i wish to select the next appropriate size and so on ..
Does any one know how I can accomplish this and would be willing to help me out by posting some code, some direction ??



and what if the size does exists then? whether search should continue? If not then try IN in your query. Like

Select {some cols} From {table} Where size IN {size1, size2, sizen}

confirm on this.

Mahesh



I wish to select the one size if it exists (this is easy) but if I have other products which do not exist in that size - I want to get them also but ONLY one size per item so if I have coca cola in 10oz, 12oz and 20 oz
I want to select always the 12 oz and if a product say Gatorade does not have a 12 oz but it does have a 20 oz - I would like to select that one in my result as well (Gatorade might have a 32oz but I dont want that one ).
I want to get only 1 item for each product: specifically first the 12 oz size if it exists, and for any product that does not come in the 12oz size I want to get that item in only one of its sizes say 20oz if it exists.
I have about 6 different sizes all together - so it is possible for 1 item to be listed 6 times.
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2008-04-17 : 05:16:30
If possible, post some real life data with desired o/p so that I can dig around.

Mahesh
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-04-17 : 11:55:19
quote:
Originally posted by mahesh_bote

If possible, post some real life data with desired o/p so that I can dig around.

Mahesh



OK the best I can do for a real world data is this table and ONE items info to fill it, a Second and third Item would follow the same pattern:

ProdName nvarchar(50)
Sku char(5) Unique Primary Key
sizename char(5)
price currency

CocaCola
20250
.5L
1.00

CocaCola
20251
1.0L
1.80

CocaCola
20252
1.5L
2.00

CocaCola
20253
2.0L
2.50

GatorAde
30251
1.0L
1.80

GatorAde
30252
1.5L
2.00

GatorAde
30253
2.0L
2.50

Orange Juice
40250
3.5L
1.00

With that Data - I am displaying a products listing. I do not wish to list ALL of the sizes - just the primary size first (.5L) and if a product does not have that size - I want to get the size it does have - but I only want one instance of any given product to be generated in the result. I hope this helps to get this solved because I am brain fried at the moment. I was thinking a temporary table for the sizes and performing joins etc.. but I am not sure and I have not accomplished that either - maybe I am looking at it with confusion glasses and there is an easy way.
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-04-18 : 00:04:13
something like this?:

select * from products p1
where sku=@x and size=(select max(@s) from products p2 where p1.sku=p2.sku)

replace max with min if you want to go up or down.
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-05-12 : 13:48:31
quote:
Originally posted by LoztInSpace

something like this?:

select * from products p1
where sku=@x and size=(select max(@s) from products p2 where p1.sku=p2.sku)

replace max with min if you want to go up or down.


This gives me the same results I was getting before. Whether I use a value in the @s field or ml in the @s field - I am not lookign for an individual sku - per se (only individual to that ProductName in the category of Drink


The result I am looking for is this:
All products of a given category according to the example data:
Category = Drinks (we use a number like 100)
I want all products where category = 100
but I also want ONLY in size of .5L , EXCEPT when the item does not have a.5L size I want the next "appropriate" size for that item which would be most likely be 1.0L or .350L

So in a nutshell the Prodnames in the result should be unique.
The results of the products should be those with size = .5L
The results should also include any products in the category that do not have a .5L - with sizes other than .5L .
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-12 : 14:03:01
May be this:-

SELECT t.ProdName,t.sizename
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY ProdName ORDER BY CAST(LEFT(sizename,LEN(sizename)-1) AS float) ) AS RowNo,
*
FROM Table
)t
WHERE t.Category=100
AND t.RowNo=1
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-05-12 : 14:31:28
quote:
Originally posted by visakh16

May be this:-

SELECT t.ProdName,t.sizename
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY ProdName ORDER BY CAST(LEFT(sizename,LEN(sizename)-1) AS float) ) AS RowNo,
*
FROM Table
)t
WHERE t.Category=100
AND t.RowNo=1




I get the error that Row_Number is not a recognized function or name - I am using SQL2000 / and will also need it to work in 2005
The Row_Number function appears only to be a 2005 element.
As soon as I get them to migrate from 2000 to 2005 I will keep this function in mind.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-12 : 14:35:53
[code]SELECT t1.*
FROM Table t1
INNER JOIN (SELECT MIN(sizename) as MinSize,ProdName
FROM Table
WHERE sizename >=@size
GROUP BY ProdName)t2
ON t2.MinSize=t1.sizename
AND t2.ProdName=t1.ProdName
WHERE t1.Category=100
[/code]

If you dont want to input size, then remove the WHERE statement in dervived table t2
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-05-12 : 16:09:25
Visakh's solution might work for you. But, I put this together real quick incase you are trying to get the next closest size:
SELECT 
P.*
FROM
@Product AS P
INNER JOIN
(
SELECT
ProdName,
MIN(Sku) AS Sku,
MIN(ABS(CAST(LEFT(sizename, LEN(sizename) - 1) AS FLOAT) - CAST(.5 AS FLOAT))) AS Diff
FROM
@Product
GROUP BY
ProdName
) AS T
ON T.ProdName = P.ProdName
AND T.Sku = p.Sku
Obviously, I left out the filter on category. But, I figure you cna add that if need be.
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-05-13 : 23:01:37
Thank you visakh16 and Lamprey, I appreciate your help very much as these two queries both mostly resolve my dilema - to a point. Lampreys version misses one of the preferred size products, but gives a large result set of 547 items. Visakh16's gives me only 247 items (maybe b/c of where size >= parameter? or the Group By. But it includes all of the preferred size, itdoes not give the next closest available size. Removing the Where in the Join Subquery - gives 553 results. I will continue playing with these two queries trying to work between the two to get the desired result. I am grateful to you both. Thanks.

Thank You both very much!!
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-05-14 : 16:27:39
quote:
Originally posted by Lamprey

Visakh's solution might work for you. But, I put this together real quick incase you are trying to get the next closest size:
SELECT 
P.*
FROM
@Product AS P
INNER JOIN
(
SELECT
ProdName,
MIN(Sku) AS Sku,
MIN(ABS(CAST(LEFT(sizename, LEN(sizename) - 1) AS FLOAT) - CAST(.5 AS FLOAT))) AS Diff
FROM
@Product
GROUP BY
ProdName
) AS T
ON T.ProdName = P.ProdName
AND T.Sku = p.Sku
Obviously, I left out the filter on category. But, I figure you cna add that if need be.


Strangely enough I managed to get this to work 100% by using an Order By clause with multiple order bys at first I did it by size and had 39 of 40 results of preferred size. when I put in the order by T.Prodname, T.Diff, T.Sku - the thing snapped up all 40 of them - must be a quirk in the SQL-Server..hmmm

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-05-14 : 17:31:58
Humm, looks like I cut-n-pasted the wrong query. I'm glad you worked out a solution. Here is another way, that I think weill actually work, as my previous example doesn't work correctly (as you found out).
SELECT
P.ProdName,
P.sizename,
MIN(P.sku)
FROM
@Product AS P
INNER JOIN
(
SELECT
ProdName,
MIN(ABS(CAST(LEFT(sizename, LEN(sizename) - 1) AS FLOAT) - CAST(.5 AS FLOAT))) AS Diff
FROM
@Product
GROUP BY
ProdName
) AS T
ON P.ProdName = T.ProdName
AND ABS(CAST(LEFT(P.sizename, LEN(P.sizename) - 1) AS FLOAT) - CAST(.5 AS FLOAT)) = t.Diff
GROUP BY
P.ProdName,
P.sizename
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-05-15 : 02:20:50
Just a quick question on the query , is there a simple way I can get not only this Result Set - but a second Result Set [Or Computed Column/s] - that has the "Alternate Sizes" for a given ProdName - the Database [VFoxPro] I import from into this one has no Parent Child relationships for the products ..
I thought to just query the table a second time and order by Prodname - and then manipulate that data in my .NET code file.

I would like to display the product , and then links for other sizes available...

I guess - I would need to be able to do a second query on the first result : Getting both Result Sets returned to me : But how to do this - I have searched for Query of Result Set and found nothing..perhaps I am going about the problem the wrong way ??

Select sizename from prods where firstquery.prodname=prods.prodname
Go to Top of Page

stixoffire
Starting Member

17 Posts

Posted - 2008-05-16 : 15:04:36
Well I have decided to fix the tables to better handle these situations - I just will have to rewrite code for getting data from the BM DB into the WebDB and of course the web APP ..all in all it is a much better solution b/c the management of the DB online will be easier and also the ability to do more with it - in a much easier fashion.
Go to Top of Page
   

- Advertisement -