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
你能解釋一下28和787月份的理性嗎? – HoneyBadger
28從類別1中提取,787從類別2中提取 –
仍然不明白;提取?類別?你能展示一個計算嗎? – HoneyBadger