Results 1 to 9 of 9

Thread: Column to Row query

  1. #1

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

    Column to Row query

    i got this value

    KK + Q1 + Q2 + Q3
    01 + 1 + 2 + 3
    01 + 2 + 3 + 4
    02 + 5 + 6 + 7
    02 + 7 + 8 + 9

    i can do pivot query so the result will be something like this

    KK + Q1 + Q2 + Q3
    01 + 3 + 5 + 7
    02 12 + 14 + 16

    but the report need to be used reversal format from the result above (Q1, Q2, Q3 as row where KK as Column)

    Q1 + 3 + 12
    Q2 + 5 + 14
    Q3 + 7 + 16

    sorry, don't how to format it here so hope u get the idea

    i found a solution via UNION query before using pivot for each Q1, Q2, Q3 but just curious is there any other way

    any idea?

    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

  2. #2
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: Column to Row query

    What are you using to display the report. e.g. Excel, Crystal, just CSV... etc
    Also are you using any programming language which calls the query.

  3. #3

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

    Re: Column to Row query

    Quote Originally Posted by kevchadders
    What are you using to display the report. e.g. Excel, Crystal, just CSV... etc
    Also are you using any programming language which calls the query.
    sorry, forgot to mention i'm using Crystal Reports 10 (field definition only) and VB 6.0..so it need to pass as Recordset

    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
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Column to Row query

    is crystal report's Cross Tab report will help?
    __________________
    Rate the posts that helped you

  5. #5

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

    Re: Column to Row query

    Quote Originally Posted by riteshjain1982
    is crystal report's Cross Tab report will help?
    no, i don't think so..just like to figure it out in SQL query

    btw, another info i'm using SQL SERVER 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

  6. #6
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: Column to Row query

    As your using 2005 the PIVOT command may be the way to go if you want to do it via SQL. It was introduced in SQL Server 2005. Check link below and look it up in the help in your Management Studio.

    http://sqlserver2000.databases.aspfa...vot-query.html

    Code:
    CREATE TABLE dbo.SalesByQuarter 
    ( 
        Y INT, 
        Q INT, 
        sales INT, 
        PRIMARY KEY (Y,Q) 
    ) 
    GO 
     
    INSERT dbo.SalesByQuarter(Y,Q,Sales) 
        SELECT 2003, 2, 479000 
        UNION SELECT 2003, 3, 321000 
        UNION SELECT 2003, 4, 324000 
        UNION SELECT 2004, 1, 612000 
        UNION SELECT 2004, 2, 524000 
        UNION SELECT 2004, 3, 342000 
        UNION SELECT 2004, 4, 357000 
        UNION SELECT 2005, 1, 734000 
    GO 
     
    SELECT Y,  
        [1] AS Q1, 
        [2] AS Q2, 
        [3] AS Q3, 
        [4] AS Q4 
    FROM 
        (SELECT Y, Q, Sales 
            FROM SalesByQuarter) s PIVOT 
    ( 
        SUM(Sales) 
        FOR Q IN ([1],[2],[3],[4]) 
    ) p 
    ORDER BY [Y] 
    GO 
     
    DROP TABLE dbo.SalesByQuarter 
    GO
    ps. Though you should really try to do it on the client side.
    Last edited by kevchadders; Aug 5th, 2008 at 03:05 AM.

  7. #7

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

    Re: Column to Row query

    Quote Originally Posted by kevchadders
    As your using 2005 the PIVOT command may be the way to go if you want to do it via SQL. It was introduced in SQL Server 2005. Check link below and look it up in the help in your Management Studio.

    http://sqlserver2000.databases.aspfa...vot-query.html

    Code:
    CREATE TABLE dbo.SalesByQuarter 
    ( 
        Y INT, 
        Q INT, 
        sales INT, 
        PRIMARY KEY (Y,Q) 
    ) 
    GO 
     
    INSERT dbo.SalesByQuarter(Y,Q,Sales) 
        SELECT 2003, 2, 479000 
        UNION SELECT 2003, 3, 321000 
        UNION SELECT 2003, 4, 324000 
        UNION SELECT 2004, 1, 612000 
        UNION SELECT 2004, 2, 524000 
        UNION SELECT 2004, 3, 342000 
        UNION SELECT 2004, 4, 357000 
        UNION SELECT 2005, 1, 734000 
    GO 
     
    SELECT Y,  
        [1] AS Q1, 
        [2] AS Q2, 
        [3] AS Q3, 
        [4] AS Q4 
    FROM 
        (SELECT Y, Q, Sales 
            FROM SalesByQuarter) s PIVOT 
    ( 
        SUM(Sales) 
        FOR Q IN ([1],[2],[3],[4]) 
    ) p 
    ORDER BY [Y] 
    GO 
     
    DROP TABLE dbo.SalesByQuarter 
    GO
    ps. Though you should really try to do it on the client side.
    thanks, but sorry the data is not the one i used, maybe see attachment 1.png is the table

    where 2.png is the result..it used lengthly UNION ALL
    Attached Images Attached Images   

    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
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: Column to Row query

    There was also another poster trying to do a similar thing here.

    (Read first message)
    http://www.vbforums.com/showthread.php?t=533390

    My first post for it shows how you can do it using a temp table.
    http://www.vbforums.com/showpost.php...87&postcount=2

    is this the sort of thing you want?

  9. #9

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

    Re: Column to Row query

    thanks,

    i think it can done that way but it will involving many update

    i guess it need to be done the 'lengthly' way

    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