Parse delimited data in column to multiple columns

  • Can someone post a code sample that shows a query working with two databases simultaneously?

  • ahmeterispaha (6/17/2008)


    Can someone post a code sample that shows a query working with two databases simultaneously?

    Sure...

    SELECT t1.*, t2.*

    FROM DatabaseOne.dbo.SomeTable1 t1

    INNER JOIN DatabaseTwo.dbo.SomeTable2 t2

    WHERE t1.SomeColumn = t2.SomeColumn

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ahmeterispaha (6/17/2008)


    I guess I'm looking for feedback on how people manage this kind of customization in a tightly controlled ERP environment.

    Thanks again.

    Ahmet

    As some others have already pointed out, including yourself, there's a couple of ways to do this...

    Some folks make a "Util" database to keep all such tools out of harms way. Others put them in the Master database (I like Util, better). Still, other's drop them right into the ERP database and keep a standard script that will reinstall all such tools in a matter of seconds (I still like the "Util", better).

    The cteTally I wrote is pretty cool and pretty darned fast to boot. It's a good "second" if you really don't want to take time for a "Util" or a standard tool script... but it's still second. I guess it depends on what you're doing and how much you really need to squeeze out of every millisecond of time.

    So, there's 3 methods... any one of the 3 are quite viable... sometimes a combination of the 3 has it's advantages... My recommendation would be to do a bit of testing and see what the performance differences on your box are, if any.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • bitbucket (6/15/2008)


    Knowing Jeff Moden's skill I strongly recommend you follow his suggestion on the use of a Tally Table.

    Sorry... I almost missed this entirely... thanks for the great compliment! 🙂

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Stephanie Giovannini (6/16/2008)


    I have copied queries directly from Jeff Moden's article at:

    http://www.sqlservercentral.com/articles/TSQL/62867

    and modified them to suit this purpose.

    Great stuff!

    I'd still like to modify this so the rep codes are represented in columns. To approach this incrementally, I thought I'd modify one of Jeff's Tally article demos but am struggling with the syntax. Here's what I have so far, followed by the error I'm getting:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'Element01,Element02,Element03,Element04,Element05'

    --===== Add start and end commas to the Parameter so we can handle

    -- single elements

    SET @Parameter = ','+@Parameter +','

    --===== Create a table to store the results in

    DECLARE @Elements TABLE

    (

    One VARCHAR(50), --The string value of the element

    Two VARCHAR(50), --The string value of the element

    Three VARCHAR(50), --The string value of the element

    Four VARCHAR(50), --The string value of the element

    Five VARCHAR(50) --The string value of the element

    )

    --===== Join the Tally table to the string at the character level and

    -- when we find a comma, insert what's between that command and

    -- the next comma into the Elements table

    INSERT INTO @Elements

    (One,Two,Three,Four,Five) SELECT SUBSTRING(@Parameter,N+1,CHARINDEX(',',@Parameter,N+1)-N-1)

    FROM dbo.Tally

    WHERE N < LEN(@Parameter)

    AND SUBSTRING(@Parameter,N,1) = ',' --Notice how we find the comma

    SELECT * FROM @Elements

    The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.

    Ahmet

  • INSERT statements that have a SELECT can't transform rows into columns, so the error you're getting is exactly what's wrong. Your SELECT statement that follows the INSERT statement needs to select 5 fields, as opposed to 5 records. With SQL 2K5, you might be able to use UNPIVOT to achieve your desired result, as you know the 5 field names your're looking for, but you'll need dynamic SQL to handle unknown field values, so you'll want to look that up here, as I know there are several topics on it. I have yet to fully understand that method, so I can't write that code as yet.

    Steve

    (aka smunson)

    :):):)

    ahmeterispaha (6/18/2008)


    Stephanie Giovannini (6/16/2008)


    I have copied queries directly from Jeff Moden's article at:

    http://www.sqlservercentral.com/articles/TSQL/62867

    and modified them to suit this purpose.

    Great stuff!

    I'd still like to modify this so the rep codes are represented in columns. To approach this incrementally, I thought I'd modify one of Jeff's Tally article demos but am struggling with the syntax. Here's what I have so far, followed by the error I'm getting:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'Element01,Element02,Element03,Element04,Element05'

    --===== Add start and end commas to the Parameter so we can handle

    -- single elements

    SET @Parameter = ','+@Parameter +','

    --===== Create a table to store the results in

    DECLARE @Elements TABLE

    (

    One VARCHAR(50), --The string value of the element

    Two VARCHAR(50), --The string value of the element

    Three VARCHAR(50), --The string value of the element

    Four VARCHAR(50), --The string value of the element

    Five VARCHAR(50) --The string value of the element

    )

    --===== Join the Tally table to the string at the character level and

    -- when we find a comma, insert what's between that command and

    -- the next comma into the Elements table

    INSERT INTO @Elements

    (One,Two,Three,Four,Five) SELECT SUBSTRING(@Parameter,N+1,CHARINDEX(',',@Parameter,N+1)-N-1)

    FROM dbo.Tally

    WHERE N < LEN(@Parameter)

    AND SUBSTRING(@Parameter,N,1) = ',' --Notice how we find the comma

    SELECT * FROM @Elements

    The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.

    Ahmet

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • On my way to work... I'll try to take a whack at this tonight... but, the split must produce an "array" and then the array must be reassembled by a cross-tab or a pivot. See the following... the "2D Split" area is what I'm talking about... Whole table can be split at once, reassembled, and used in the insert.

    http://www.sqlservercentral.com/articles/T-SQL/63003/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I'm still testing but I believe I have a solution thanks to all your suggestions -- especially Jeff and Stephanie (many thanks). The reason I was trying to represent the RepCodes in multiple columns was to be consistent with the RepRate and RepSplit columns so that I could calculate the actual commission due each Rep. I ended up modifying Stephaine's solution to incorporate the rest of my data.

    Here is my solution if anyone is interested. Please let me know if you see any opportunities for improvement. 😛

    --===== If the test table already exists, drop it

    IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL

    DROP TABLE #mytable

    --===== Load temp table with data joined from InvcHead and InvoiceDtl tables

    CREATE TABLE #mytable

    (

    InvoiceNum INT,

    InvoiceLine INT,

    InvoiceDate DATETIME,

    SalesRepList TEXT,

    RepRate1 DECIMAL(6,2),

    RepRate2 DECIMAL(6,2),

    RepRate3 DECIMAL(6,2),

    RepRate4 DECIMAL(6,2),

    RepRate5 DECIMAL(6,2),

    RepSplit1 INT,

    RepSplit2 INT,

    RepSplit3 INT,

    RepSplit4 INT,

    RepSplit5 INT,

    PartNum VARCHAR(50),

    SellingShipQty DECIMAL(16,2),

    UnitPrice DECIMAL(17,5),

    DiscountPercent DECIMAL(6,2)

    )

    INSERT INTO #mytable (

    InvoiceNum,

    InvoiceLine,

    InvoiceDate,

    SalesRepList,

    RepRate1,

    RepRate2,

    RepRate3,

    RepRate4,

    RepRate5,

    RepSplit1,

    RepSplit2,

    RepSplit3,

    RepSplit4,

    RepSplit5,

    PartNum,

    SellingShipQty,

    UnitPrice,

    DiscountPercent

    )

    SELECT '12425','1','May 1 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C109202','2.00','2500.00000','0.00' UNION ALL

    SELECT '12426','1','May 1 2008 12:00AM','AUTOSOL',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013H03','4.00','35.00000','30.00' UNION ALL

    SELECT '12426','2','May 1 2008 12:00AM','AUTOSOL',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013B14','4.00','50.00000','30.00' UNION ALL

    SELECT '12426','3','May 1 2008 12:00AM','AUTOSOL',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8020H01','4.00','45.00000','30.00' UNION ALL

    SELECT '12426','4','May 1 2008 12:00AM','AUTOSOL',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8020B03','4.00','55.00000','30.00' UNION ALL

    SELECT '12426','5','May 1 2008 12:00AM','AUTOSOL',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S105001','6.00','185.00000','30.00' UNION ALL

    SELECT '12429','1','May 2 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C4019705-2','1.00','7490.00000','0.00' UNION ALL

    SELECT '12430','1','May 2 2008 12:00AM','TS',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C40-04254-051292','1.00','5250.00000','0.00' UNION ALL

    SELECT '12430','2','May 2 2008 12:00AM','TS',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C40-04254-051292','1.00','5250.00000','0.00' UNION ALL

    SELECT '12435','1','May 2 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'006275074701','1.00','170.00000','0.00' UNION ALL

    SELECT '12436','1','May 2 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'0366A0701710','3.00','235.00000','0.00' UNION ALL

    SELECT '12440','1','May 5 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013H03','4.00','24.50000','0.00' UNION ALL

    SELECT '12440','2','May 5 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013B14','4.00','35.00000','0.00' UNION ALL

    SELECT '12440','3','May 5 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S105001','2.00','129.50000','0.00' UNION ALL

    SELECT '12440','4','May 5 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S125001','4.00','213.50000','0.00' UNION ALL

    SELECT '12456','1','May 7 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C4019705-5','2.00','8990.00000','0.00' UNION ALL

    SELECT '12460','1','May 8 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'006275074701','1.00','170.00000','0.00' UNION ALL

    SELECT '12466','1','May 8 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013B17','2.00','50.00000','0.00' UNION ALL

    SELECT '12467','1','May 8 2008 12:00AM','CK',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C123007','2.00','575.00000','0.00' UNION ALL

    SELECT '12468','1','May 8 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C123007','1.00','1300.00000','0.00' UNION ALL

    SELECT '12475','1','May 9 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C4019705-2','1.00','7490.00000','0.00' UNION ALL

    SELECT '12475','2','May 9 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C4019705-2','1.00','7490.00000','0.00' UNION ALL

    SELECT '12481','1','May 12 2008 12:00AM','IBSNE',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C125008','60.00','242.86000','0.00' UNION ALL

    SELECT '12481','3','May 12 2008 12:00AM','IBSNE',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C123502','120.00','195.24000','0.00' UNION ALL

    SELECT '12482','1','May 12 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8020H01','1.00','45.00000','30.00' UNION ALL

    SELECT '12482','2','May 12 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8020B03','1.00','55.00000','30.00' UNION ALL

    SELECT '12491','1','May 13 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S301201','15.00','137.50000','0.00' UNION ALL

    SELECT '12491','2','May 13 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C8075X09','15.00','325.00000','0.00' UNION ALL

    SELECT '12493','1','May 14 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C4019705-5','1.00','8990.00000','0.00' UNION ALL

    SELECT '12493','2','May 14 2008 12:00AM','TS',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C4019705-5','1.00','8990.00000','0.00' UNION ALL

    SELECT '12504','1','May 15 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S105001','3.00','185.00000','0.00' UNION ALL

    SELECT '12512','1','May 16 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C123007','2.00','1300.00000','0.00' UNION ALL

    SELECT '12513','1','May 16 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S301201','2.00','210.00000','30.00' UNION ALL

    SELECT '12513','2','May 16 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S102501','2.00','155.00000','30.00' UNION ALL

    SELECT '12514','1','May 16 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013B17','9.00','50.00000','30.00' UNION ALL

    SELECT '12514','2','May 16 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8013B16','3.00','50.00000','30.00' UNION ALL

    SELECT '12526','1','May 21 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8019P02','8.00','135.00000','20.00' UNION ALL

    SELECT '12526','2','May 21 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S8019E01','8.00','60.00000','20.00' UNION ALL

    SELECT '12529','1','May 21 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S8025S01','2.00','10.00000','20.00' UNION ALL

    SELECT '12529','2','May 21 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S8025B01','2.00','60.00000','20.00' UNION ALL

    SELECT '12529','3','May 21 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S90F064','1.00','4.00000','20.00' UNION ALL

    SELECT '12529','4','May 21 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S90F040','1.00','2.00000','20.00' UNION ALL

    SELECT '12529','5','May 21 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S90L008','1.00','150.00000','20.00' UNION ALL

    SELECT '12534','1','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S8020S01','8.00','10.00000','0.00' UNION ALL

    SELECT '12534','2','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S1012501','8.00','540.00000','0.00' UNION ALL

    SELECT '12534','3','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S106501','6.00','215.00000','0.00' UNION ALL

    SELECT '12534','4','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S207501','6.00','380.00000','0.00' UNION ALL

    SELECT '12534','5','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S8013B17','4.00','50.00000','0.00' UNION ALL

    SELECT '12534','6','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S302501','2.00','315.00000','0.00' UNION ALL

    SELECT '12534','7','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S8013S01','4.00','10.00000','0.00' UNION ALL

    SELECT '12534','8','May 22 2008 12:00AM','CK~AUTOSOL',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S8010F02','6.00','160.00000','0.00' UNION ALL

    SELECT '12535','1','May 22 2008 12:00AM','AUTOSOL~TS',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S300601','1.00','195.00000','0.00' UNION ALL

    SELECT '12538','1','May 22 2008 12:00AM','TS',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C225001','10.00','775.00000','0.00' UNION ALL

    SELECT '12543','1','May 23 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S90L006','1.00','225.00000','0.00' UNION ALL

    SELECT '12543','2','May 23 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S90L007','1.00','150.00000','0.00' UNION ALL

    SELECT '12543','3','May 23 2008 12:00AM','CK',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S90L009','2.00','30.00000','0.00' UNION ALL

    SELECT '12554','1','May 23 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S301901','8.00','265.00000','0.00' UNION ALL

    SELECT '12554','2','May 23 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S90S008-425','4.00','170.00000','0.00' UNION ALL

    SELECT '12559','1','May 27 2008 12:00AM','AUTOSOL~CK',5.00,5.00,0.00,0.00,0.00,100,100,0,0,0,'S205001','25.00','297.00000','0.00' UNION ALL

    SELECT '12575','1','May 29 2008 12:00AM','AUTOSOL',0.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C123001','5.00','250.00000','0.00' UNION ALL

    SELECT '12578','1','May 29 2008 12:00AM','CK',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'S2225401','13.00','1000.00000','30.00' UNION ALL

    SELECT '12579','1','May 30 2008 12:00AM','TS',0.00,0.00,0.00,0.00,0.00,0,0,0,0,0,'C4019705-2','1.00','7490.00000','0.00' UNION ALL

    SELECT '12580','1','May 30 2008 12:00AM','TS',5.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C4019705-5','1.00','8990.00000','0.00' UNION ALL

    SELECT '12591','1','May 30 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'S301201','3.00','137.50000','0.00' UNION ALL

    SELECT '12591','2','May 30 2008 12:00AM','AUTOSOL',10.00,0.00,0.00,0.00,0.00,100,0,0,0,0,'C8075X09','3.00','325.00000','0.00'

    --Create a table to store the results in

    DECLARE @Commissions TABLE

    (

    InvNum INT,

    InvLine INT,

    CommissionOrder INT,

    RepCode VARCHAR(8000),

    RepRate DECIMAL(6,2),

    RepSplit INT,

    InvoiceDate DATETIME,

    PartNum VARCHAR(50),

    SellingShipQty DECIMAL(16,2),

    UnitPrice DECIMAL(17,5),

    DiscountPercent DECIMAL(6,2)

    )

    ; WITH SalesReps (InvNum, InvLine, RepCode, RepOrder) AS

    (

    SELECT M.InvoiceNum, M.InvoiceLine,

    SUBSTRING('~' + CAST(M.SalesRepList AS VARCHAR(8000)) + '~', T.N+1, CHARINDEX('~','~' + CAST(M.SalesRepList AS VARCHAR(8000)) + '~',T.N+1)-(T.N+1)),

    ROW_NUMBER() OVER (PARTITION BY M.InvoiceNum,M.InvoiceLine ORDER BY T.N)

    FROM NWABUtil.dbo.Tally T

    CROSS JOIN #mytable M

    WHERE T.N < DATALENGTH(M.SalesRepList)

    AND SUBSTRING('~' + CAST(M.SalesRepList AS VARCHAR(8000)) + '~',T.N,1) = '~'

    AND SUBSTRING('~' + CAST(M.SalesRepList AS VARCHAR(8000)) + '~',T.N+1,1) <> '~'

    )

    INSERT INTO @Commissions

    (InvNum,

    InvLine,

    CommissionOrder,

    RepCode,

    RepRate,

    RepSplit,

    InvoiceDate,

    PartNum,

    SellingShipQty,

    UnitPrice,

    DiscountPercent

    )

    SELECTR.InvNum, R.InvLine, R.RepOrder, R.RepCode,

    CASE R.RepOrder

    WHEN 1 THEN M.RepRate1

    WHEN 2 THEN M.RepRate2

    WHEN 3 THEN M.RepRate3

    WHEN 4 THEN M.RepRate4

    WHEN 5 THEN M.RepRate5

    ELSE 0

    END,

    CASE R.RepOrder

    WHEN 1 THEN M.RepSplit1

    WHEN 2 THEN M.RepSplit2

    WHEN 3 THEN M.RepSplit3

    WHEN 4 THEN M.RepSplit4

    WHEN 5 THEN M.RepSplit5

    ELSE 0

    END,

    M.InvoiceDate,

    M.PartNum,

    M.SellingShipQty,

    M.UnitPrice,

    M.DiscountPercent

    FROMSalesReps R

    JOIN#mytable M ON M.InvoiceNum = R.InvNum

    AND M.InvoiceLine = R.InvLine

    SELECTInvNum,

    InvLine,

    CommissionOrder,

    RepCode,

    InvoiceDate,

    PartNum,

    SellingShipQty,

    UnitPrice,

    DiscountPercent,

    UnitPrice * (100-DiscountPercent)/100 AS DiscUnitPrc,

    SellingShipQty * UnitPrice * (100-DiscountPercent)/100 AS DiscExtPrc,

    RepRate,

    RepSplit,

    SellingShipQty * UnitPrice * (100-DiscountPercent)/100 * RepRate/100 * RepSplit/100 AS Commission

    FROM @Commissions

  • Replace

    WHERE T.N < DATALENGTH(M.SalesRepList)

    with

    WHERE T.N <= DATALENGTH(M.SalesRepList)

    There was an error in the query I originally wrote. I forgot to account for the tildes added at front and end. Sorry about that.

  • Thanks Stephanie.

    I'll make the change.

Viewing 10 posts - 16 through 24 (of 24 total)

You must be logged in to reply to this topic. Login to reply