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.
Author |
Topic |
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-06-23 : 08:11:33
|
I work for a furniture manufacturer that makes similar desks in multiple sizes. Often the only difference between the desks are their width or depth. Our part numbers reflect this difference by including the dimension in them.Table: INPRSCField: FSCHECODE - This is a unique part numberField: FADJUST1 - Price of partThe following are some example part numbersThe following parts are identical except for the width.D2272L-7MC-P - Has a width of 72 inchesD2266L-7MC-P - Has a width of 66 inchesD2254L-7MC-P - Has a width of 54 inchesThe following parts are identical except for the widthCR2272-2MC-P - Has a width of 72 inchesCR2266-2MC-P - Has a width of 66 inchesCR2254-2MC-P - Has a width of 54 inchesI want to compare the prices of the similar desks to make sure that the larger desk always cost at least the as much as the smaller version. When they don’t the query would return the part number of at least one of the two desks. I can then manually figure out what is wrong on a case by case basis.For example a CR2272-7MC should cost at least as much as CR2266-7MC-P. However a CR2266-7MC-P might cost more or less then the other two because it is a different type of desk.The number of characters and/or numbers before and after the size dimension very depending on the type of desk. However all desks of the same type will have identical part numbers except for the dimension.Any suggestions would be greatly appreciated. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-23 : 08:56:42
|
[code]DECLARE @InPrices TABLE ( Code VARCHAR(200), Adjust MONEY )INSERT @InPricesSELECT 'D2272L-7MC-P', 200 UNION ALLSELECT 'D2266L-7MC-P', 150 UNION ALLSELECT 'D2254L-7MC-P', 100 UNION ALLSELECT 'CR2272-2MC-P', 200 UNION ALLSELECT 'CR2266-2MC-P', 125 UNION ALLSELECT 'CR2254-2MC-P', 130SELECT Code, AdjustFROM ( SELECT Code, Adjust, DENSE_RANK() OVER (PARTITION BY RIGHT(Code, 6) ORDER BY Code) AS Size, DENSE_RANK() OVER (PARTITION BY RIGHT(Code, 6) ORDER BY Adjust) AS Prize FROM @InPrices ) AS dWHERE Size <> Prize[/code] E 12°55'05.63"N 56°04'39.26" |
|
|
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-06-23 : 09:40:25
|
Thanks for the response. Your query is much more advance then anything I have written. When I ran it I got the following error.'DENSE_RANK' is not a recognized function name. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-23 : 09:44:26
|
Oh, my bad. I didn'r recognize this is a SQL Server 2000 forum. E 12°55'05.63"N 56°04'39.26" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-23 : 09:46:57
|
Here is a version for SQL Server 2000.SELECT ip.Code, ip.AdjustFROM @InPrices AS ipWHERE (SELECT COUNT(*) FROM @InPrices AS x WHERE RIGHT(x.Code, 6) = RIGHT(ip.Code, 6) AND ip.Code <= x.Code) <> (SELECT COUNT(*) FROM @InPrices AS x WHERE RIGHT(x.Code, 6) = RIGHT(ip.Code, 6) AND ip.Adjust <= x.Adjust) E 12°55'05.63"N 56°04'39.26" |
|
|
scott_hanebutt
Starting Member
10 Posts |
Posted - 2009-06-23 : 09:54:07
|
Your query works perfectly now except that it is only comparing the example parts I listed. My database has thousands of parts that I want to compare. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-23 : 10:24:24
|
Yes?You have to realize I don't have access to your data, so the first part is only for mimicing your environment.This is the part you need.SELECT ip.Code, ip.AdjustFROM INPRSC AS ipWHERE (SELECT COUNT(*) FROM INPRSC AS x WHERE RIGHT(x.Code, 6) = RIGHT(ip.Code, 6) AND ip.Code <= x.Code) <> (SELECT COUNT(*) FROM INPRSC AS x WHERE RIGHT(x.Code, 6) = RIGHT(ip.Code, 6) AND ip.Adjust <= x.Adjust) E 12°55'05.63"N 56°04'39.26" |
|
|
|
|
|
|
|