SQL:对行或其他表中定义的日期范围之间的销售额求和

时间:2018-05-16 14:19:20

标签: sql hadoop cloudera impala

我正在尝试查询我的数据库(Cloudera Hadoop),并且无法解决我需要做的事情。我正在尽我所能地清楚地问这个问题,但我已经在脑海中了。在这种情况下,我添加了屏幕截图,以帮助可视化问题和预期结果。

如果需要任何其他信息,请发表评论。我试图尽可能地简化,但也许我错过了一些东西。

我有3张桌子。

Cal 表格包含2018年的所有不同周,格式为YYYYWW。 它还包含月初至今的开始和结束周。 它还包含其他日期间隔(季度至今,年初至今等),但为简单起见,我将其排除在问题之外。

Cal table

POS 表包含我的事实。

POS Table

Store 表包含商店属性。

enter image description here

我正在尝试获取以下输出:

desired_output table

我很难让MTD Sales和其他日期范围填充。

SELECT
    pos.corp_year_week,
    pos.store,
    store.rep,
    sum(pos.sales) sales,
    ??????
FROM
    pos inner join cal on pos.corp_year_week = cal.corp_year_week
    inner join store on pos.store = store.store
GROUP BY 
    pos.corp_year_week,
    pos.store,
    store.rep

我正在考虑为我的所有周工作大量CASE语句并定义每个corp_year_week的开始日期和结束日期的想法,但我确信这不是正确的方法。< / p>

任何帮助都会真的赞赏。谢谢

1 个答案:

答案 0 :(得分:0)

我使用子查询来获取h_0

中给出的时间段的总和
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Simon's Timesheet Signature Capture Test Page</title>
    <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/south-street/jquery-ui.css"
      rel="stylesheet">
<link href="../js/jquery.signature.css" rel="stylesheet">
<style>
    body > iframe {
        display: none;
    }

    .kbw-signature {
        width: 400px;
        height: 200px;
    }
</style>
<!--[if IE]>
<script src="../js/excanvas.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="../js/jquery.signature.js"></script>
<script src="../js/jquery.ui.touch-punch.min.js"></script>
</head>
<body>
<h1>jQuery UI Signature Basics v18.5.2018</h1>
<p>This page demonstrates the very basics of the
    <a href="http://keith-wood.name/signature.html">jQuery UI Signature plugin</a>.
It contains the minimum requirements for using the plugin and
can be used as the basis for your own experimentation.</p>
<p>For more detail see the <a href="http://keith-wood.name/signatureRef.html">documentation reference</a> page.</p>
<p>Default signature:</p>
<div id="sig"></div>
<p style="clear: both;">
    <button id="clear">Clear</button>
    <button id="json">To JSON</button>
    <button id="svg">To SVG</button>
</p>
<script>
    $(document).ready(function () {
        $('#sig').signature();
    $('#clear').click(function () {
        $('#sig').signature('clear');
    });
    $('#json').click(function () {
        alert($('#sig').signature('toJSON'));
    });
    $('#svg').click(function () {
        alert($('#sig').signature('toSVG'));
    });
});
</script>
</body>
</html>

这是在MariaDb(MySql)上测试的,但它是非常标准的SQL