基于另一个表中日期范围的总和列

时间:2019-11-26 15:29:39

标签: sql-server tsql join group-by

我有两个桌子。

交易

Transactions

日期时间范围

Date time range

我正在尝试对表2中每个开始时间和结束时间之间(包括该时间)之间的事务求和Col1。

我期望:

范围1 = 3
范围2 = 2
范围3 = 1

4 个答案:

答案 0 :(得分:2)

一种方法是相关子查询:

html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
</style>

<body>
    <Canvas></Canvas>
</body>
<script>

    class point
    {
        constructor(x,y)
        {
            this.x=x;
            this.y=y;
        }
    }

    class shape
    {
        constructor(value)
        {
            value=value;
            //this.value=value;
        }


        drawShape(c,value,len)
        {
            var i=0;
            c.beginPath()
            c.moveTo(value[0].x,value[0].y)
            for(i=1;i<len;i++)
            {
                c.lineTo(value[i].x,value[i].y)
                c.moveTo(value[i].x,value[i].y)
            }
            c.lineTo(value[0].x,value[0].y)
            c.stroke()
        }
    }
    var canvas = document.querySelector('canvas')

    // canvas.height = window.innerHeight;
    // canvas.width = window.innerWidth;

    canvas.height = window.innerHeight
    canvas.width = window.innerWidth
    var c = canvas.getContext('2d')

    var points = {};
    var points2 = {};
    var p = {};
    var t = {};
    var house = {};
    var i = 0;
    var j = 0;
    var delt = 0;
    var mousePos = {
        x: 0,
        y: 0
    };

    canvas.addEventListener("click",function(e){
        mousePos = getmouseposition(canvas, e);
        checkPoints(mousePos);
    },false);


    function checkPoints(mouse_pos) {
        if (i <= 9) {
            p[j] = new point(mouse_pos.x, mouse_pos.y);
            points[j] = p[j];
            i++;
            j++;
        } 
        else if (i == 10) {
            var house = {
                p: [0],
                p: [1],
                p: [2],
                p: [3],
                p: [4],
                p: [5],
                p: [6],
                p: [7],
                p: [8],
                p: [9]
            };

            shape = new shape(house);
            shape.drawShape(c, points, 9);
            j = 0;
            i++;
        } 
    }

    function getmouseposition(canvas, event) {
        var rect = canvas.getBoundingClientRect();
        return {
            x: event.clientX - rect.left,
            y: event.clientY - rect.top
        };
    }
</script>

</html>

答案 1 :(得分:1)

尝试一下:

select Start, End, sum(Col1) from Transactions t inner join [Date time range] d on [datetime] between Start and End
group by Start, End

答案 2 :(得分:1)

您可以使用简单的JOIN和GROUP BY:

SELECT ranges.id, ranges.start, ranges.end, SUM(transactions.col1)
FROM ranges
LEFT JOIN transactions ON transactions.datetime >= ranges.start AND
                          transactions.datetime <= ranges.end
GROUP BY ranges.id, ranges.start, ranges.end

答案 3 :(得分:0)

您可以尝试以下操作,如果需要,请替换LEFT连接。

UNINSTALL