在Hive中滚动7天的总和

时间:2018-08-10 00:05:44

标签: hive hiveql

我有如下数据,并希望通过以下维度获取(1)今天的用户数和(2)7天过去的用户数。我的查询没有返回我想要的结果。有人可以修复我的Hive SQL代码吗?

例如今天)= 8/9/2018

过去7天= 8/2/2018-8/8/2018

  

维度:日期,网站,国家/地区ID,产品ID

     

指标:用户

SELECT 
             date, 
             website,
             country_id,
             product_id,
             SUM(user) AS today_users,
             SUM(user) OVER (
           PARTITION BY website, country_id, product_id 
           ORDER BY date ASC ROWS BETWEEN 8 PRECEDING AND 1 PRECEDING) as past_7D_users
           FROM  Table
    GROUP BY 
    date, website, country_id, product_id;

RawData:

    >t_date  |website |country_id|product_id|users
    >8/1/2018   Whatev.com  2     EFG        12 
    >8/2/2018   Whatev.com  2     EFG        23
    >8/3/2018   Whatev.com  2     EFG        34 
    >8/4/2018   Whatev.com  2     EFG        13
    >8/5/2018   Whatev.com  2     EFG        47 
    >8/6/2018   Whatev.com  2     EFG        39
    >8/7/2018   Whatev.com  2     EFG        17 
    >8/8/2018   Whatev.com  2     EFG        34
    >8/1/2018   Google.com  1     ABC        10 
    >8/2/2018   Google.com  1     ABC        20
    >8/3/2018   Google.com  1     ABC        30 
    >8/4/2018   Google.com  1     ABC        14
    >8/5/2018   Google.com  1     ABC        40 
    >8/6/2018   Google.com  1     ABC        35
    >8/7/2018   Google.com  1     ABC        15 
    >8/8/2018   Google.com  1     ABC        32

预期结果:

date      | website  |country_id|product_id|today_users|past_7D_users|
2018-08-08|Google.com|   1      | ABC      | 32        |164          |
2018-08-09|Whatev.com|   2      | EFG      | 34        |185          |

1 个答案:

答案 0 :(得分:0)

好..自我回答我的问题很奇怪,但是我找到了解决方法。如果有人知道更好,更简化的查询,请分享。

@{
    ViewBag.Title = "Editor";
}

<script>

    var table = function () {

        var t = document.getElementById('table');
        var selection = t.options[t.selectedIndex].value;

        if (selection == "emp") {
            $("#emp").show();
            $("#itm").hide();
            $("#ln").hide();
            $("#mo").hide();
            $("#shft").hide();
            $("#twi").hide();
        } else if (selection == "itm") {
            $("#emp").hide();
            $("#itm").show();
            $("#ln").hide();
            $("#mo").hide();
            $("#shft").hide();
            $("#twi").hide();
        } else if (selection == "ln") {
            $("#emp").hide();
            $("#itm").hide();
            $("#ln").show();
            $("#mo").hide();
            $("#shft").hide();
            $("#twi").hide();
        } else if (selection == "mo") {
            $("#emp").hide();
            $("#itm").hide();
            $("#ln").hide();
            $("#mo").show();
            $("#shft").hide();
            $("#twi").hide();
        } else if (selection == "shft") {
            $("#emp").hide();
            $("#itm").hide();
            $("#ln").hide();
            $("#mo").hide();
            $("#shft").show();
            $("#twi").hide();
        } else if (selection == "twi") {
            $("#emp").hide();
            $("#itm").hide();
            $("#ln").hide();
            $("#mo").hide();
            $("#shft").hide();
            $("#twi").show();
        } else {
            $("#emp").hide();
            $("#itm").hide();
            $("#ln").hide();
            $("#mo").hide();
            $("#shft").hide();
            $("#twi").hide();
        }
    }
</script>

<h2>Edits</h2>

<select id="table" name="table" onclick="table()">
    <option value="0">Select a table to edit</option>
    <option value="0"></option>
    <option value="emp">Employee</option>
    <option value="itm">Item</option>
    <option value="ln">Line</option>
    <option value="mo">MO</option>
    <option value="shft">Shift</option>
    <option value="twi">TWI</option>
</select>

<br /><br />

<span hidden class="emp">
    <select name="empid" id="empid">
        <option value="0">Select Employee</option>
        @foreach (var emp in ViewBag.EMP)
        {
            <option value="@emp.EmployeeID">@emp.EmployeeName</option>
        }
    </select>
</span>

<span hidden class="itm">
    <select name="itmid" id="itmid">
        <option value="0">Select Item</option>
        @foreach (var itm in ViewBag.ITM)
        {
            <option value="@itm.ItemID">@itm.ItemName</option>
        }
    </select>
</span>

<span hidden class="ln">
    <select name="lnid" id="lnid">
        <option value="0">Select Line</option>
        @foreach (var ln in ViewBag.LN)
        {
            <option value="@ln.LineID">@ln.LineName</option>
        }
    </select>
</span>

<span hidden class="mo">
    @{int count = 0;}
    <select name="bch" id="bch">
        <option value="0">Select MO</option>
        @foreach (var mo in ViewBag.MO)
        {
            <option value="@mo.NBMO">@ViewBag.fullMO[count]</option>
            count += 1;
        }
    </select>
</span>

<span hidden class="shft">
    <select name="shftid" id="shftid">
        <option value="0">Select Shift</option>
        @foreach (var shft in ViewBag.SHFT)
        {
            <option value="@shft.ShiftID">@shft.ShiftCode</option>
        }
    </select>
</span>

<span hidden class="twi">
    @{count = 0;}
    <select name="tnum" id="tnum">
        <option value="0">Select TWI</option>
        @foreach (var twi in ViewBag.TWI)
        {
            <option value="@twi.TWINumber">@ViewBag.fullTWI[count]</option>
            count += 1;
        }
    </select>
</span>