| 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. LikeSelect {some cols} From {table} Where size IN {size1, size2, sizen}confirm on this.Mahesh |
 |
|
|
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. LikeSelect {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 ozI 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. |
 |
|
|
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 |
 |
|
|
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 Keysizename char(5)price currencyCocaCola20250.5L1.00CocaCola202511.0L1.80CocaCola202521.5L2.00CocaCola202532.0L2.50GatorAde302511.0L1.80GatorAde302521.5L2.00GatorAde302532.0L2.50Orange Juice402503.5L1.00With 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. |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-04-18 : 00:04:13
|
| something like this?:select * from products p1where 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. |
 |
|
|
stixoffire
Starting Member
17 Posts |
Posted - 2008-05-12 : 13:48:31
|
quote: Originally posted by LoztInSpace something like this?:select * from products p1where 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 DrinkThe 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 = 100but 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 . |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-12 : 14:03:01
|
May be this:-SELECT t.ProdName,t.sizenameFROM(SELECT ROW_NUMBER() OVER(PARTITION BY ProdName ORDER BY CAST(LEFT(sizename,LEN(sizename)-1) AS float) ) AS RowNo, *FROM Table)tWHERE t.Category=100AND t.RowNo=1 |
 |
|
|
stixoffire
Starting Member
17 Posts |
Posted - 2008-05-12 : 14:31:28
|
quote: Originally posted by visakh16 May be this:-SELECT t.ProdName,t.sizenameFROM(SELECT ROW_NUMBER() OVER(PARTITION BY ProdName ORDER BY CAST(LEFT(sizename,LEN(sizename)-1) AS float) ) AS RowNo, *FROM Table)tWHERE t.Category=100AND 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 2005The 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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-12 : 14:35:53
|
| [code]SELECT t1.*FROM Table t1INNER JOIN (SELECT MIN(sizename) as MinSize,ProdName FROM Table WHERE sizename >=@size GROUP BY ProdName)t2ON t2.MinSize=t1.sizenameAND t2.ProdName=t1.ProdNameWHERE t1.Category=100[/code]If you dont want to input size, then remove the WHERE statement in dervived table t2 |
 |
|
|
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 PINNER 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. |
 |
|
|
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!!  |
 |
|
|
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 PINNER 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 |
 |
|
|
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 PINNER 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.DiffGROUP BY P.ProdName, P.sizename |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
|