[英]How to add variables in SQL
我有一個變量,需要按月顯示。 此變量是布爾值,如果將其設置為True,則表示已安裝該應用程序。
示例:變量=已安裝的應用
一月有20個已安裝的應用程序。 2月有10個已安裝的應用程序。 3月,安裝了5個應用程序。
我想在桌上顯示所有已安裝的應用程序(布爾值= True):
January: 20
February: 20 (January) + 10 = 30
March: 20 (January) + 10 (February) + 5 (March) = 35
有誰知道如何在Mysql中做到這一點?
謝謝
您可以使用子查詢按月對安裝進行分組。 然后,您可以在外部查詢中使用一個變量來計算累計和:
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
就像是:
Select COUNT(*),Month
From Apps
where installed = true
group by Month
如果我理解正確,那只是一個帶有排序依據的簡單查詢。
select COLUMNS_YOU_WANT from TABLE_NAME
where YOUR_BOOLEAN == True
order by SOME_DATE_COLUMN
希望您精通SQL,可以用所需的內容替換這些部分。
我假設您有一個“應用程序”表,其中包含“已安裝”列和“ InstalledDate”列。 如果是這樣的話...
SELECT Count(*)
FROM Applications
WHERE Installed = 1 -- Assumes a bit/boolean column.
AND InstalledDate BETWEEN @StartDate AND @EndDate;
史考特
我知道了! 我用了遞歸
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.