Results 1 to 10 of 10

Thread: Quantity to Row

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Quantity to Row

    let say i have this Records

    Table1 = ItemCode + Quantity

    Table1
    ItemCode1, 2
    ItemCode2, 3

    when i select it, it make Quantity to Row value in the Result

    ItemCode1
    ItemCode1
    ItemCode2
    ItemCode2
    ItemCode2


    this is for Barcode program because we need to use it as a row not using Quantity

    thanks,
    erick

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Quantity to Row

    What database application are you using? Access, SQL Server, Oracle?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: Quantity to Row

    Sqlsever 2005

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Quantity to Row

    Have you considered that logic might belong in the UI?

    The DB says Item1 has a value of 2 - and you want the UI to show 2. Why not read the ITEM1 and 2 into variables in your logic building this display and simply put the row twice?

    I can't see a simply SQL join that would see the "2" and know to duplicate that 2 times. Oh...maybe I do...

    Try this - I don't have time to test it...

    Code:
    Declare @Copy Table (CP int)
    Insert into @Copy values (1)
    Insert into @Copy values (2)
    Insert into @Copy values (3)
    Insert into @Copy values (4)
    -- you need to do this up to the max value in your table
    -- or make it a loop in the final SPROC
    
    Select ItemCode From Table1 T1
    Left Join @Copy CO on CO.CP<=T1.Quantity

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: Quantity to Row

    thx sz

    Have you considered that logic might belong in the UI? -> twice, maybe more now

    i try using CURSOR and confuse

    -- or make it a loop in the final SPROC -> sz, i'm confuse about this part, can u elaborate more

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Quantity to Row

    have you tried my @Copy query?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: Quantity to Row

    yes i have

    i got 7 rows in Table1, the query give me 17 results..i'm not sure why

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Quantity to Row

    Works fine in this example - first I create some test data and then SELECT from that.

    Code:
    Declare @TestData Table (ItemNum varchar(10), Quantity int)
    
    Insert into @TestData values ('AAA',2)
    Insert into @TestData values ('BBB',1)
    Insert into @TestData values ('CCC',3)
    
    select * from @TestData
    
    Declare @Copy Table (CP int)
    Insert into @Copy values (1)
    Insert into @Copy values (2)
    Insert into @Copy values (3)
    Insert into @Copy values (4)
    -- you need to do this up to the max value in your table
    -- or make it a loop in the final SPROC
    
    Select * from @TestData
    Left Join @Copy CO on CP<=Quantity
    and that returns

    Code:
    ItemNum    Quantity
    ---------- -----------
    AAA        2
    BBB        1
    CCC        3
    
    (3 row(s) affected)
    
    ItemNum    Quantity    CP
    ---------- ----------- -----------
    AAA        2           1
    AAA        2           2
    BBB        1           1
    CCC        3           1
    CCC        3           2
    CCC        3           3
    
    (6 row(s) affected)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: Quantity to Row

    thanks sz


    sorry for late reply,

    yes it works..but i just realized about it that the ItemNum got child records for the Barcode things

    so it required new table
    Code:
    Declare @Details Table (ItemNum varchar(10), Barcode varchar(10), Quantity int)
    Insert into @Details values ('AAA', 'A1', 1)
    Insert into @Details values ('AAA', 'A2', 1)
    Insert into @Details values ('AAA', 'A3', 1)
    Insert into @Details values ('BBB', 'B1', 1)
    Insert into @Details values ('CCC', 'C1', 1)
    it need to select the Barcode in ASC Order, the quantity for the barcode is always 1

    thanks
    Last edited by erickwidya; May 15th, 2008 at 10:34 PM.

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: Quantity to Row

    i'm thinking about this, use new column like identity but it started from 1 whenever itemcode is changing

    Code:
    Declare @Details Table (Num int, ItemNum varchar(10), Barcode varchar(10), Quantity int)
    Insert into @Details values (1, 'AAA', 'A1', 1)
    Insert into @Details values (2, 'AAA', 'A2', 1)
    Insert into @Details values (3, 'AAA', 'A3', 1)
    Insert into @Details values (1, 'BBB', 'B1', 1)
    Insert into @Details values (1, 'CCC', 'C1', 1)
    and using the query and get the result
    Code:
    Select * from @TestData t1
    Left Join @Copy CO on CP<=t1.Quantity
    left join @details t2 on t1.ItemNum = t2.itemnum and co.cp = t2.num
    
    
    ItemNum    Quantity    CP          Num         ItemNum    Barcode    Quantity
    ---------- ----------- ----------- ----------- ---------- ---------- -----------
    AAA        2           1           1           AAA        A1         1
    AAA        2           2           2           AAA        A2         1
    BBB        1           1           1           BBB        B1         1
    CCC        3           1           1           CCC        C1         1
    CCC        3           2           NULL        NULL       NULL       NULL
    CCC        3           3           NULL        NULL       NULL       NULL
    
    (6 row(s) affected)
    but if anyone any other method, plz let me know

    thanks

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width