0

尊敬的所有成员,我在一个 SQL 表中有大约两年的数据,并使用 Select query 命令为该数据填充了一个网格视图(一个月)

<asp:SqlDataSource ID="SqlDataSource15" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PunctualitymainConnectionString2 %>"  SelectCommand="SELECT SUM(case when Dir_Ind = 'Dir' then 1 else 0 end ) AS &quot;Direct&quot;, 

Rly FROM  PunctualityMain  

WHERE  Rly IN ('CR', 'ER', 'ECR', 'ECoR', 'NR', 'NCR', 'NER', 'NFR', 'NWR', 'SR', 'SCR', 'SER', 'SECR', 'SWR', 'WR', 'WCR') AND Date &gt;= '4/1/2012' AND Date &lt;= '4/30/2012'  

GROUP BY Rly"></asp:SqlDataSource>

网格视图是

Rly  Direct
CR     5
ER     7
ECR    2

现在,我想将此网格视图显示为比较报表(月份与上一年同月相比),包括 %Improvement / Deterioration。像这样的东西

Rly    Current month Direct     Previous month Direct       %Improvement / Deterioration
CR         5                         8                           37.5
ER         7                         6                          -16.6
ECR        2                         2                            0.0

可能吗?如果是,请告知,如何轻松实现?

4

1 回答 1

0

根据我们上面的讨论,您可以像这样编写您的 t-sql 查询:

SELECT SUM(case when Dir_Ind = 'Dir' and Date between '4/1/2012' and '4/30/2012' then 1 else 0 end ) AS 'CurrentDirect', 
       SUM(case when Dir_Ind = 'Dir' and Date between '3/1/2012' and '3/31/2012' then 1 else 0 end ) AS 'PreviousDirect', 
       Rly
FROM  PunctualityMain  
WHERE  Rly IN ('CR', 'ER', 'ECR', 'ECoR', 'NR', 'NCR', 'NER', 'NFR', 'NWR', 'SR', 'SCR', 'SER', 'SECR', 'SWR', 'WR', 'WCR') 
 AND Date >= '3/1/2012' AND Date <= '4/30/2012'
GROUP BY Rly

这样,您可以将 GridView 的数据源设置为结果输出。您可以在 t-sql 或 GridView 的 RowDataBound 事件中计算改进和恶化列。

于 2013-05-03T20:20:31.327 回答