breaking out repeating groups

  • hello,

    I have a table with repeating groups

    ie

    user_name

    puller

    stocker

    data_entry

    verifier

    I want to normilize the table and put the different job types into job_type table. Is it possible to do this with one SQL statement

    currently I'm using several ie

    insert into job_type

    select puller

    from users

    where puller = 1

    thanks for any help

  • If puller is the information you want to insert, you could do the following:

    
    
    INSERT INTO job_type
    SELECT DISTINCT
    puller
    FROM users

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • WHAT I WANT TO DO IS MOVE THE INFORMATION IN

    PULLER,

    VERIFIER,

    DATA_ENTRY

    STOCKER,

    INTO THE WORK_TYPE TABLE WITH ONE QUERY

  • Can you post the CREATE TABLE statements for both tables and a handful of records?

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • You could use a construct like the following, extending the UNION subquery to all the different types.

    select p1.user_name, case p2.type when 1 then 'Puller' when 2 then 'Stocker' end

    from persons p1,

    (select user_name, 1 as type from persons where puller = 1

    UNION

    select user_name, 2 as type from persons where stocker = 1) p2

    where p1.user_name = p2.user_name

    Maybe someone can come up with a cleaner solution.

Viewing 5 posts - 1 through 4 (of 4 total)

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