如何将一行中的不同计数器与SQL组合在一起

时间:2015-10-30 08:00:14

标签: sql union

我有一个mysql表,其中包含有关已成功登录的人数以及未登录的人数的信息。

以下SQL查询以次优的方式提供正确的值。

SELECT 	Year(Time), 
		Month(Time), 
		DayOfMonth(Time),
		AccessType,
		Success,
		count(*) as Counter
FROM cas2015.TransactionLog 
WHERE AccessType = 1
GROUP BY 	Year(Time), 
		Month(Time), 
		DayOfMonth(Time),  
		AccessType,
		Success
ORDER BY Time ASC;

结果就是这个表

<table border="1">
  <tr BGCOLOR="#CCCCFF">
    <th>Year(Time)</th>
    <th>Month(Time)</th>
    <th>DayOfMonth(Time)</th>
    <th>AccessType</th>
    <th>Success</th>
    <th>Counter</th>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>15</td>
    <td>1</td>
    <td>true</td>
    <td>29624</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>15</td>
    <td>1</td>
    <td>false</td>
    <td>4449</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>16</td>
    <td>1</td>
    <td>true</td>
    <td>26339</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>16</td>
    <td>1</td>
    <td>false</td>
    <td>4069</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>17</td>
    <td>1</td>
    <td>true</td>
    <td>15732</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>17</td>
    <td>1</td>
    <td>false</td>
    <td>2508</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>18</td>
    <td>1</td>
    <td>true</td>
    <td>16475</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>18</td>
    <td>1</td>
    <td>false</td>
    <td>3138</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>19</td>
    <td>1</td>
    <td>true</td>
    <td>29701</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>19</td>
    <td>1</td>
    <td>false</td>
    <td>4785</td>
  </tr>
</table>

在一条线上彼此相邻的正面和负面价值会更有帮助。理想情况下,我可以得到第三列,其中有正数由负数下降。

这有可能吗?

<table border="1">
  <tr BGCOLOR="#CCCCFF">
    <th>Year(Time)</th>
    <th>Month(Time)</th>
    <th>DayOfMonth(Time)</th>
    <th>Positive</th>
    <th>Negative</th>
    <th>Aggregated</th>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>15</td>
    <td>29624</td>
    <td>4449</td>
    <td>6,6</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>15</td>
    <td>26339</td>
    <td>4069</td>
    <td>6,47</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>16</td>
    <td>15732</td>
    <td>2508</td>
    <td>6,27</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>16</td>
    <td>16475</td>
    <td>3138</td>
    <td>5,25</td>
  </tr>
  <tr>
    <td>2015</td>
    <td>10</td>
    <td>17</td>
    <td>29701</td>
    <td>4785</td>
    <td>6,21</td>
  </tr>
</table>

感谢您的提示

1 个答案:

答案 0 :(得分:1)

您需要一个条件聚合(由所有DBMS支持):

select dt.*,
   100 * FailCounter/SuccessCounter as FailedPercentage
from
 (   
   SELECT   Year(Time), 
        Month(Time), 
        DayOfMonth(Time),
        AccessType,
        SUM(case when Success = 'true'  then 1 else 0 end) as SuccessCounter,
        SUM(case when Success = 'false' then 1 else 0 end) as FailCounter,
   FROM cas2015.TransactionLog 
   WHERE AccessType = 1
   GROUP BY     Year(Time), 
        Month(Time), 
        DayOfMonth(Time),  
        AccessType
 ) as dt
ORDER BY 1,2,3;