使用任何方法添加Mysql累积频率字段

问题描述 投票:0回答:2

我有一个销售“表”,并据此创建了一个名为“每日活动”的“视图”,该视图提供了我每天的总销售额。我正在尝试添加一个累积频率字段,但它返回一个空的coloumn。任何人都可以将我指向正确的方向。这就是我所拥有的“在此处输入图像描述”

这就是我想要的“在此处输入图像描述”

mysql running-total cumulative-frequency
2个回答
0
投票

查询:

SELECT
d1.Date_TR,
sum(d1.Sales) AS Daily_Sales,
(SELECT SUM(d2.Sales)
 FROM dailyactivity d2
 WHERE d2.Date_TR <= d1.Date_TR) AS cumulative_sum
FROM dailyactivity d1
GROUP BY d1.Date_TR
ORDER BY d1.Date_TR ASC

0
投票

此脚本将为任何表创建所需的累积分布函数(CDF)查询:

    #!/bin/bash

tableName=$1
cdfParam=$2
otherParams=$3

usage(){
    echo "Usage: $0 <Table name> <Column/Columns for CDF> [Additional parameters to output]"
    echo ""
    echo "Get CDF of the Age column from the Persons table and print the Name and Gender of each person"
    echo "e.g., $0 Persons Age Name,Gender"
    echo ""
    echo "Get CDF of the difference between Salary & profil from the Persons table and print the Name and Gender of each person"
    echo "e.g., $0 Persons 'Salary-Profit' Name,Gender"
    echo ""
    echo "Get CDF of Age from a derived table called employeeTable"
    echo "e.g., $0 '(select * from Employees) as employeeTable' Age"
    exit 1
}

[[ $# -lt 2 ]] && usage

if [ -z "$otherParams" ]
then
    echo "SELECT $cdfParam as cdf_paramValue, (SELECT count(*) FROM $tableName WHERE $cdfParam <= cdf_paramValue) AS cumulative_sum FROM $tableName ORDER BY ($cdfParam) desc";
else
    echo "SELECT $otherParams,$cdfParam as cdf_paramValue, (SELECT count(*) FROM $tableName WHERE $cdfParam <= cdf_paramValue) AS cumulative_sum FROM $tableName ORDER BY ($cdfParam) desc";
fi
© www.soinside.com 2019 - 2024. All rights reserved.