| Author |
Topic |
|
fawadafr
Starting Member
47 Posts |
Posted - 2010-03-10 : 18:05:23
|
I wrote the following SQL script to categorize products in OrderDetails table. I tried to use ProductCode since we follow a standard protocol when creating a new ProductCode for our items. Here are some basic info about ProductCode:- Category 1: ProductCodes that do not have any dashes or letters.
- Category 2: ProductsCodes that have dashes in position 5 (e.g. XXXX-XX) and may contain letters.
- Category 3: ProductCodes that start with LIT- (e.g. LIT-0001)
- Category 4: All ProductCodes that do not fall in three categories above.
SELECT ItemCode ,ItemName ,LEN(ItemName) AS [Length] ,(CASE WHEN PATINDEX('%-%', ItemName) = '5' THEN 'Prosthetic' WHEN PATINDEX('%-%', ItemName) = '0' AND LEN(ItemName) <> 4 THEN 'Implant' WHEN ItemName LIKE 'LIT-%' THEN 'Catalogs' WHEN LEN(ItemName) = 4 AND ItemName LIKE '1%' OR ItemName LIKE '2%' THEN 'SurgicalMotor' ELSE 'Surgical' END) AS Category FROM InventoryItem ORDER BY Category ASC, ItemNameI would like to complete this script so I can run a report to see where each sales order fall (category 1, 2, 3, and 4) or maybe in two or three categories.Could you provide me with your comments and feedback?Thank you,--Fawad Rashidi |
|
|
Malik_Alamin
Starting Member
3 Posts |
Posted - 2010-03-10 : 18:21:03
|
| Hi Fawad,What exactly are you trying to do? It looks like you have a small group of product codes in your data such as:Numeric Codes (only numbers),Codes with a dash in the 5th place,Codes witha dash in the 3rd place,Everything elseSo basically all you have are these four groups. Am I understanding this correctly?MalikMalik Al-AminSenior SQL Server & ETL DeveloperPh: 612.991.8116Malamin@FortitudeConsulting.Bizwww.FortitudeConsulting.Biz“All things are possible with fortitude”Add me on LinkedIn at: http://www.linkedin.com/pub/malik-al-amin/9/266/223 |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-11 : 04:11:54
|
This might not be right on the money but it'll give you the idea of how the concept works:SELECT ItemCode, Prosthetic = SUM(CASE WHEN PATINDEX('%-%', ItemName) = '5' THEN 1 ELSE 0 END), Implant = SUM(CASE WHEN PATINDEX('%-%', ItemName) = '0' AND LEN(ItemName) <> 4 THEN 1 ELSE 0 END), Catalogs = SUM(CASE WHEN ItemName LIKE 'LIT-%' THEN 1 ELSE 0 END), SurgicalMotor = SUM(CASE WHEN LEN(ItemName) = 4 AND ItemName LIKE '1%' OR ItemName LIKE '2%' THEN 1 ELSE 0 END), Surgical = SUM(CASE WHEN ... THEN 1 ELSE 0 END)FROM InventoryItemGROUP BY ItemCode- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
fawadafr
Starting Member
47 Posts |
Posted - 2010-03-11 : 12:56:48
|
| Thank you very much Lumbago. Your script really does most of the work but there are some instances that it returns 1 for the different categories.As an example, if an implant begin with number '2' your script return 1 both for implant and surgical motor category.How can we force the script to look for any four digits number that begins with 1 or 2 so we know all our Surgical Motors? Implants also begin with 1 and 2 but their length is actually six digits.Thank you,--Fawad Rashidi |
 |
|
|
fawadafr
Starting Member
47 Posts |
Posted - 2010-03-11 : 13:50:59
|
| Hi Malik,Here the the five different product family we have. In fact, I have about 1500 products in our inventory. My goal is to categorize our customer based on what they order by using the following rules:1) Products that only have four numeric digits (e.g. 1234)2) Products that only have six number digits (e.g. 123456)3) Products that have numbers and letter and dash at position 5 (e.g. 1234-12, 1234-12A, 1234-12PP, etc. Always starts with four numeric digits then dash.)4) Products that start with "LIT-"5) Product that start with letters (e.g. D123, LDR233, etc.)--Fawad Rashidi |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-12 : 02:03:02
|
| 1) SUM(CASE WHEN LEN(ItemName) = 4 AND ISNUMERIC(LEFT(ItemName, 4)) = 1 THEN 1 ELSE 0 END)2) SUM(CASE WHEN LEN(ItemName) = 6 AND ISNUMERIC(LEFT(ItemName, 6)) = 1 THEN 1 ELSE 0 END)3) SUM(CASE WHEN ISNUMERIC(LEFT(ItemName, 4)) = 1 AND ISNUMERIC(SUBSTRING(ItemName, 5, 1)) = 0 THEN 1 ELSE 0 END)4) SUM(CASE WHEN ItemName LIKE 'LIT-%' THEN 1 ELSE 0 END)5) SUM(CASE WHEN ISNUMERIC(LEFT(ItemName, 1)) = 0 THEN 1 ELSE 0 END)I think this should cover your requirements...- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
fawadafr
Starting Member
47 Posts |
Posted - 2010-03-12 : 12:09:37
|
Hello Lumbago,Once again, thank you very much for taking your time helping me with this project.I executed your script and was able to categorize most of our products. Here is the link if you would like to see the results:[url]http://www.implantdirect.com/Ahmad/msdn/xls/Results.xlsx[/url]I noticed your script for the second category did not return any results.SUM(CASE WHEN LEN(ItemName) = 6 AND ISNUMERIC(LEFT(ItemName, 6)) = 1 THEN 1 ELSE 0 END) I check the property of ItemName in SQL server and discovered it is nvarchar(100). I believe since you are using ISNUMERIC() function, it doesn't return any value. How can we fix this?There are still some products that are shown in two different categories. A product can only have one category.Thank you,--Fawad Rashidi |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-12 : 12:13:29
|
quote: Originally posted by fawadafr Hello Lumbago,Once again, thank you very much for taking your time helping me with this project.I executed your script and was able to categorize most of our products. Here is the link if you would like to see the results:[url]http://www.implantdirect.com/Ahmad/msdn/xls/Results.xlsx[/url]I noticed your script for the second category did not return any results.SUM(CASE WHEN LEN(ItemName) = 6 AND ISNUMERIC(LEFT(ItemName, 6)) = 1 THEN 1 ELSE 0 END) I check the property of ItemName in SQL server and discovered it is nvarchar(100). I believe since you are using ISNUMERIC() function, it doesn't return any value. How can we fix this?There are still some products that are shown in two different categories. A product can only have one category.Thank you,--Fawad Rashidi
ISNUMERIC checks if value in expression can be converted to any of numeric formats and returns 1 or 0. so even if source field is nvarchar it will return 1 provided its contents are numeric------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
fawadafr
Starting Member
47 Posts |
Posted - 2010-03-12 : 15:30:32
|
Got it... thank you. Now, I am trying to JOIN our InventoryItem tables to CustomerSalesOrderDetail >> CustomerSalesOrder >> Customer tables to categorize the customers based on what they purchase from us. As an example, I would like to go into every order a custom has place and flag 'Yes'/'No' for every category. Obviously, I only want to show his record once despite the number of orders he/she has placed.Here is what I have so far:SELECT Customer.CustomerCode ,Customer.CustomerName ,CustomerSalesOrder.SalesOrderCode AS Expr1 ,CustomerSalesOrder.SalesOrderDate ,CustomerSalesOrder.Total ,CustomerSalesOrderDetail.ItemCode ,InventoryItem.ItemNameFROM CustomerSalesOrderDetail INNER JOIN CustomerSalesOrder ON CustomerSalesOrderDetail.SalesOrderCode = CustomerSalesOrder.SalesOrderCode INNER JOIN InventoryItem ON CustomerSalesOrderDetail.ParentItemCode = InventoryItem.ItemCode INNER JOIN Customer ON CustomerSalesOrder.BillToCode = Customer.CustomerCode Could you please provide me with some hints and suggestion on how to get this portion done?Thank you,--Fawad Rashidi |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-13 : 00:56:45
|
| can you show some sample data from tables and explain what you want?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|