我有一个基本的交易表,下面的查询为我提供了最近一个月所有交易的计数:
Select count(status)
FROM TX
WHERE Date_Reported >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0)
AND Date_Reported < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
我想要的是将该值与前一个月进行比较,并找到增加或减少的百分比。
例如,我们在7月,上述查询将为我提供6月的交易计数(即100)。我需要计数整个5月(2个月前),并找到%差异。因此,如果5月为50,6月为100,则查询应返回100%
有什么想法吗?
答案 0 :(得分:2)
使用窗口功能:
with cte as(
select year(Date_reported) as [Year], month(Date_reported) as [Month], count(Status) as [StatusCount]
FROM TX
group by year(Date_reported), month(Date_reported)
)
Select [Year], [Month], [StatusCount]
, 100.0*([StatusCount] - lag([StatusCount]) OVER (ORDER BY [Year], [Month]))/lag([StatusCount]) OVER (ORDER BY [Year], [Month]) as [PercentageDiff]
from cte
答案 1 :(得分:1)
给出以下方法,应该可以正常工作,但是不要认为这是最佳解决方案:
declare @lastMonth int =
(Select count(status)
FROM TX
WHERE Date_Reported >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0)
AND Date_Reported < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))
declare @beforeLastMonth int =
(Select count(status)
FROM TX
WHERE Date_Reported >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-2,0)
AND Date_Reported < DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0))
declare @result int = (@lastMonth - @beforeLastMonth) * 100 / @beforeLastMonth
答案 2 :(得分:1)
如果仅需要2个月,则可以尝试以下查询。否则,您可以添加序列(1,2,3,4,5 ...),然后使用它代替固定整数:
CREATE TABLE tx
(
status CHAR(1)
, date_reported DATE
);
INSERT INTO tx
VALUES ('A', '20180709')
, ('B', '20180708')
, ('A', '20180701')
, ('B', '20180609')
, ('A', '20180608')
, ('A', '20180509')
, ('B', '20180501');
SELECT SUM(months.is_last_month)
, SUM(months.is_last2_month)
, (SUM(months.is_last_month) - SUM(months.is_last2_month)) * 100 / SUM(months.is_last2_month)
FROM tx AS t
OUTER APPLY ( SELECT CASE WHEN Date_Reported >= DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0)
AND Date_Reported < DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) THEN 1
END AS is_last_month
, CASE WHEN Date_Reported >= DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0)
AND Date_Reported < DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) THEN 1
END AS is_last2_month) AS months
WHERE t.date_reported >= DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0)
AND t.date_reported < DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0);
答案 3 :(得分:1)
尝试一下
with CurMon as (
Select count(status) CurStat
FROM TX
WHERE Date_Reported >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0)
AND Date_Reported < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
)
,
PreMon as (
Select count(status) PreStat
FROM TX
WHERE Date_Reported >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-2,0)
AND Date_Reported < DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0)
)
select case when PreStat>0 then 100*(CurStat- PreStat)/PreStat else 0 end as pct
from CurMon join PreMon on 1=1
答案 4 :(得分:1)
我建议您在pl / sql函数中执行此操作,因为它更容易:
if (!empty($array2)){
foreach ($array2 as $key => $value) { ?>
<div class="panel panel-default center-block developerinfo">
<div class="panel-body">
<div class=row>
<div class="col-md-12">
<h2 style="color:orangered" class="text-center">SIP</h2>
<img alt="" class="center-block"style="width:20%;"src="../../css/collecting/logo.png">
<h2 class="text-center"><?php echo $value['name_dvp']; ?></h2>
<h3 class="text-center">(<?php echo $value['region_dvp']; ?>)</h3>
<h3 class="text-center">D<?php echo $value['id_dvp']; ?></h3>
<h2 style="color:orangered" class="text-center"><?php echo $value['maintenancelocation_dvp']; ?></h2>
<img alt="" class=" center-block developerlocationselection check cursor selection-button" src="../../css/collecting/route-select.png" name="<?php echo $value['name_dvp']; ?>" region = "(<?php echo $value['region_dvp']; ?>)" id="D<?php echo $value['id_dvp']; ?>" location="<?php echo $value['maintenancelocation_dvp']; ?>" routeid="<?php echo $value['idroute']; ?>"sipid="<?php echo $value['id_sip']; ?>">
<ul id="developercheckbox">
<input type="checkbox" class="developernames" name="developernames" style="display:none;">
</ul>
</div>
</div>
</div>
</div>
<?php
}
}
没有表很难仅在一个查询中尝试解决方案