简体   繁体   中英

MSSQL - 2 Tables with multiple rows merge in one table

I have 2 selects which returns 2 tables, in each table I have 12 rows, and 2 column, now I want to have just one table.

---- First Table
    select f. date as Date, f.value as Month1 
        from
            (
                select b.date as date, sum(b.value) as value
                from business bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus2 b on b.id = buc.id
                where ct.id = 1
                    and b.value <> 0
                    and b.date between @start and @actual
                    and bu.name = @bu_name
                group by b.date, b.value

                union all

                select b5.date as date, sum(b5.value) as value
                from bus bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus3 b5 on b5.id = buc.id
                where ct.id = 1
                    and b5.value <> 0
                    and b5.date between @nextM and @endM
                    and bu.name = @bu_name
                group by b5.date, b5.value

            ) as f


---- Second Table   

        select f1. date as Date, f1.value as Month2 
        from
            (
                select b.date as date, sum(b.value) as value
                from business bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus2 b on b.id = buc.id
                where ct.id = 1
                    and b.value <> 0
                    and b.date between @start and @actual
                    and bu.name = @bu_name
                group by b.date, b.value

                union all

                select b5.date as date, sum(b5.value) as value
                from bus bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus3 b5 on b5.id = buc.id
                where ct.id = 1
                    and b5.value <> 0
                    and b5.date between @nextM and @endM
                    and bu.name = @bu_name
                group by b5.date, b5.value

            ) as f1

The actual output for first table is:

Date                Month1
2015-01-01        23
2015-01-01        77

and for second:

Date                Month2
2015-01-01        88
2015-01-01        90

All I want is to merge this 2 tables to look like

   Date                Month1      Date                Month2
    2015-01-01        23         2015-01-01             77          
    2015-01-01        28         2015-01-01             787

As I said in my comment, a simple join should do the trick. Something like:

SELECT      *
FROM        
            (
                select b.date as date, sum(b.value) as value
                from business bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus2 b on b.id = buc.id
                where ct.id = 1
                    and b.value <> 0
                    and b.date between @start and @actual
                    and bu.name = @bu_name
                group by b.date, b.value

                union all

                select b5.date as date, sum(b5.value) as value
                from bus bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus3 b5 on b5.id = buc.id
                where ct.id = 1
                    and b5.value <> 0
                    and b5.date between @nextM and @endM
                    and bu.name = @bu_name
                group by b5.date, b5.value

            ) as f
LEFT JOIN   (
                select b.date as date, sum(b.value) as value
                from business bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus2 b on b.id = buc.id
                where ct.id = 1
                    and b.value <> 0
                    and b.date between @start and @actual
                    and bu.name = @bu_name
                group by b.date, b.value

                union all

                select b5.date as date, sum(b5.value) as value
                from bus bu
                    join bus_cat buc on buc.id = bu.id
                    join cat ct on ct.id = buc.type_id
                    join bus3 b5 on b5.id = buc.id
                where ct.id = 1
                    and b5.value <> 0
                    and b5.date between @nextM and @endM
                    and bu.name = @bu_name
                group by b5.date, b5.value

            ) as f1
        ON  f.date = f1.date

edit

By the way, this query can be simplified, looks like there are many similarities in the queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM