Results 1 to 10 of 10

Thread: [RESOLVED] Combine rows from 2 tables?

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Resolved [RESOLVED] Combine rows from 2 tables?

    I want to be able to generate the following excel report and it appears I need to be able to combine rows from 2 tables, subjects and fees, is this possible? Or any other solution you could recommend?

    The database is mysql.

    Name:  Data.jpg
Views: 362
Size:  38.3 KB
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Combine rows from 2 tables?

    Here's my attempt at using UNION, how can I align their rows?
    Name:  attempt.JPG
Views: 261
Size:  61.8 KB
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Combine rows from 2 tables?

    That's not what a union is for... if you want the data on the same row, you use a JOIN of some sort... using a common field to join the two tables together.

    That said, I'm not sure that's what you want either. It looks like you're trying to dump this data all out in one shot to Excel. That may not be the best way to go. Dump it out in chunks. Do one query to get the class data, dump it to Excel, then get the Fee data and do a second dump to Excel.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Combine rows from 2 tables?

    Yes I am reconsidering just doing it in steps right now as it looks it may not really be possible to retrieve all those data in one sql. Just in case, I'm leaving this open for others to share their recommendations. Thanks TG!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: Combine rows from 2 tables?

    If you have a Row_Number() like function in MySql you could do it like this - here it is in MS SQL

    Code:
    Create Table #Tbl1 (SomeData varchar(15))
    Create Table #Tbl2 (OtherData varchar(15))
    Insert into #Tbl1 values ('Test Table 1'),('Test Again 1')
    Insert into #Tbl2 values ('Do Table 2'),('Test More 2')
    
    
    Select X1.*,X2.*
    	From (Select Row_Number() Over (Order by SomeData) "RowNum",SomeData
    				From #Tbl1) X1
    	Left Join (Select Row_Number() Over (Order by OtherData) "RowNum",OtherData
    				From #Tbl2) X2 on X2.RowNum=X1.RowNum
    
    Drop TAble #Tbl1
    Drop Table #Tbl2
    And that yields the following

    Code:
    RowNum	SomeData	RowNum	OtherData
    1	Test Again 1	1	Do Table 2
    2	Test Table 1	2	Test More 2
    I am not handling the situation of having different row numbers in each table. You could deal with that by doing a UNION of the two tables with DISTINCT and not UNION ALL so that becomes the FROM table.

    [edit] or maybe just a different join. I've done so many left joins at this point in my life I'm blinded! [/edit]

    *** 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

  6. #6

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Combine rows from 2 tables?

    I'm still double checking but this appears to be working, thank you so much for the idea SZ! Sadly, I still need to spread.
    Code:
    SELECT X1.*,X2.*
    	FROM (SELECT
    @row_number:=CASE
            WHEN @customer_no = studentID THEN @row_number + 1
            ELSE 1
        END AS num,
        @customer_no:=studentID AS CustomerNumber, unifastotherfees.*
    FROM
        `enrollmentsystem`.`unifastotherfees`) AS X1
    	LEFT JOIN (SELECT
    @row_number:=CASE
            WHEN @customer_no = studentID THEN @row_number + 1
            ELSE 1
        END AS num,
        @customer_no:=studentID AS CustomerNumber, unifastsubjects.*
    FROM
        `enrollmentsystem`.`unifastsubjects`) AS X2 ON X2.studentID=X1.studentID AND X2.num=X1.num
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: [RESOLVED] Combine rows from 2 tables?

    What do you mean by "still need to spread"?

    *** 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

  8. #8

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Combine rows from 2 tables?

    Name:  spread.JPG
Views: 190
Size:  12.4 KB
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: [RESOLVED] Combine rows from 2 tables?

    Oh - got it! Glad it worked for you!!

    *** 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

  10. #10

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Combine rows from 2 tables?

    Yes it did, I'm really glad you guys are still around to extend some help, it is much appreciated!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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