如果我有如下表格:
Source Event Date Qty
Site A Create Account 5/05/2018 6
Site B Create Account 4/05/2018 12
Site A Update Account 6/05/2018 1
Site A Update Notes 7/05/2018 2
Site B Add Dependant 5/05/2018 1
Site C Create Account 5/05/2018 14
另一个像这样:
Date OrdersRec
4/05/2018 162
5/05/2018 123
6/05/2018 45
7/05/2018 143
并希望构建一种矩阵表,以便能够反复使用各种日期范围的查询,如下所示:
Date Create Update UpdateNotes AddDependant OrdersRec
4/05/2018 12 0 0 0 162
5/05/2018 20 0 0 1 123
6/05/2018 0 1 0 0 45
7/05/2018 0 0 2 0 143
这样做的最佳方法是什么?
我开始使用INSERT进入日期和CreateAccount数字,但我不知道如何返回并更新特定日期的其他计数,或者即使那些愚蠢而且我遗漏了一些明显的东西。< / p>
谢谢!
答案 0 :(得分:1)
只需使用union all
或join
即可。没有理由为此创建一个新表:
select date, sum(v_create) as v_create, sum(v_update) as v_update,
sum(v_updatenotes) as v_updatenotes, sum(v_adddependent) as v_adddependent,
sum(orderrec) as orderec
from ((select t1.date, v.*, 0 as OrdersRec
from table1 t1 cross apply
(values ( (case when event = 'Create Account' then quantity else 0 end),
(case when event = 'Update Account' then quantity else 0 end),
(case when event = 'Update Notes' then quantity else 0 end),
(case when event = 'Add Dependent' then quantity else 0 end)
)
) v(v_create, v_update, v_updatenotes, v_adddependent)
) union all
(select date, 0, 0, 0, 0, orderrec
from table2
)
) t
group by date
order by date;
这会处理许多边缘情况,例如当一个或其他表中缺少日期时。
答案 1 :(得分:0)
为什么不创建包含所有日期的变量表,并将其用作主表。
然后你会这样的事情
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<div class="form-group">
<label for="expense-category">Kategoria</label>
<select-category @select-cat="chooseCategory($event)"></select-category>
</div>
</div>