-1

我有一个变量需要按月显示。此变量是一个布尔值,如果它设置为 True,则表示该应用程序已安装。

示例:变量 = 已安装的应用程序

1 月份有 20 个安装的应用程序。2 月份安装了 10 个应用程序。3 月份安装了 5 个应用程序。

我想在表格上显示所有已安装的应用程序(布尔 = True):

January: 20

February: 20 (January) + 10 =  30

March: 20 (January) + 10 (February) + 5 (March) = 35

有谁知道我如何在 Mysql 中做到这一点?

谢谢

4

5 回答 5

1

您可以使用子查询按月对安装进行分组。然后你可以在外部查询中使用一个变量来计算累积和:

select  installation_year
,       installation_month
,       installation_count
,       (@running_sum:= @running_sum + installation_count) as cumulative_sum
from    (
        select  year(installation_date) as installation_year
        ,       month(installation_date) as installation_month
        ,       count(*) as installation_count
        from    YourTable
        group by
                installation_year
        ,       installation_month
        ) as SubQueryAlias
join    (select @running_sum := 0) as InitVars
order by 
        installation_year
,       installation_month

SQL Fiddle 的示例。

于 2013-03-05T19:31:50.967 回答
1

就像是:

Select COUNT(*),Month 
From Apps
where installed = true
group by Month
于 2013-03-05T19:33:09.837 回答
0

如果我理解正确,这只是一个带有 order by 的简单查询。

select COLUMNS_YOU_WANT from TABLE_NAME 
where YOUR_BOOLEAN == True 
order by SOME_DATE_COLUMN

希望您对 SQL 足够精通,可以将这些部分替换为您需要的部分。

于 2013-03-05T19:31:24.360 回答
0

我假设您有一个“应用程序”表,其中包含一个“已安装”列和一个“已安装日期”列。如果是这样的话...

SELECT Count(*)
FROM   Applications
WHERE  Installed = 1 -- Assumes a bit/boolean column.
AND    InstalledDate BETWEEN @StartDate AND @EndDate;

斯科特

于 2013-03-05T19:32:40.353 回答
0

我得到了它!我使用了递归

SELECT (select id_time from database.dim_time where id_time = a.fk_time) AS yearmonth, (select sum( if( installed in (1), 1,0)) as installed from database.facts_table where fk_time <= yearmonth ) as installed FROM database.facts_table AS a GROUP BY yearmonth ORDER BY yearmonth ASC

于 2013-03-08T20:40:09.067 回答