2015-12-17 65 views
0

我有2個選擇返回2個表,每個表中我有12行和2列,現在我想只有一個表。MSSQL - 2在一個表中有多行合併表

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

的第一臺實際的輸出是:

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

和第二:

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

所有我想要的是合併這2個表,看起來像

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

你能解釋一下28和787月份的理性嗎? – HoneyBadger

+0

28從類別1中提取,787從類別2中提取 –

+0

仍然不明白;提取?類別?你能展示一個計算嗎? – HoneyBadger

回答

1

正如我在評論中所說的,一個簡單的joi ñ應該做的伎倆。喜歡的東西:

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 

編輯

順便說一句,這種查詢可以簡化,看起來像有在查詢有許多相似之處。