June 18, 2010 at 7:53 am
CREATE TABLE test(
ITEM VARCHAR(30),
QUANTITY numeric(38,5)
)
INSERT INTO TEST
SELECT 'RMA2052', 8.00000 UNION ALL
SELECT 'RMA2052', 10.00000 UNION ALL
SELECT 'RMA2052D', 0.50000 UNION ALL
SELECT 'RMAP20102', 8.00000 UNION ALL
SELECT 'RMAP20102', 10.00000 UNION ALL
SELECT 'RMAP20102', 8.00000 UNION ALL
SELECT 'RMA0151', 10.00000 UNION ALL
SELECT 'RMA0151D', 0.25000
Example of what i would like to see
ITEM QUANTITY
rma2052 18.50000
RMAP20102 26.00000
RMA0151 10.25000
June 18, 2010 at 8:00 am
Your output contains three rows, each with an 'Item' which is a prefix of the Item values in your input table. The length of the prefix part is variable. Can you describe the rules which determine how many characters - reading from the left - you want to use for the 'Item' prefix in the output table?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 18, 2010 at 8:05 am
This is what the output is of the sample code
Item Quanity
RMA2052 8.00000
RMA2052 10.00000
RMA2052D 0.50000
RMAP20102 8.00000
RMAP2010210.00000
RMAP201028.00000
RMA0151 10.00000
RMA0151D0.25000
But as you can see for an expample The RMA2052 has 3 and one them has a D at the end. I would like display RMA2052 without the D and the total of all 3 records
June 18, 2010 at 8:33 am
Try:
SELECT GroupItem AS Item
,SUM(QUANTITY) AS QUANTITY
FROM (
SELECT LEFT(ITEM, ISNULL(NULLIF(PATINDEX('%[0-9][A-Z]%', ITEM),0),30)) AS GroupItem, QUANTITY
FROM TEST
) a
GROUP BY GroupItem
This will also work:
SELECT LEFT(ITEM, ISNULL(NULLIF(PATINDEX('%[0-9][A-Z]%', ITEM),0),30)) AS Item, SUM(QUANTITY) AS QUANTITY
FROM TEST
GROUP BY LEFT(ITEM, ISNULL(NULLIF(PATINDEX('%[0-9][A-Z]%', ITEM),0),30))
June 18, 2010 at 8:41 am
Thanks that works perfect
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply