可以用SQL建这个表吗?
我有 2 个表,一个销售表和一个付款表,结构如下。
这两个值使用 ContractID 列连接起来。我想看到的是一个矩阵,在顶部显示每月(销售量)的总和。然后在左侧,我想按月查看付款日期,以及已付款的任何付款。我理想的输出如下所示。
黄线是按月、年销售的总金额,绿线是付款表中已支付的所有付款。我真的不知道从哪里开始,有人对如何实现这一目标有任何建议吗?我将首先取消旋转已售出的表格,以使我的日期位于顶部,只是考虑下一步将这张表格整合在一起?
I have 2 tables, a Sales table and a Payment table structured like the below.
The 2 are joined using the ContractID column. What I want to see is a matrix that shows me at the top, the sum of (sold amount) per monthyear. Then on the left, I want to see the payment dates by month year, and any payments that have been made. My ideal output would look like the below.
The yellow line being the total sold by month-year, and the green lines being all the payments that have been made from the payments table. I don't really know where to start with this one, does anyone have any advice on how to achieve this? I am going to unpivot the sold table first to get my dates across the top, just pondering the next step to pull this table together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
If I didn't understand wrong, it should be like this.
Fidler 示例
示例图片
If I didn't understand wrong, it should be like this.
Fidler Sample
Sample Image
我建议使用条件聚合和联合。
由于 PIVOT 语法更受限制。
在 db<>fiddle 此处
I suggest using conditional aggregation and a union.
Since the PIVOT syntax is more limited.
Test on db<>fiddle here