avg()一年中每月所选行数的计数

时间:2015-07-16 16:25:15

标签: sql ms-access

我需要从一个看起来像这样的Access查询中返回每个系统每个月每月所选行数的avg():

SELECT backlog.System, 
    DatePart('yyyy',[Create Date]) AS [Year], 
    DatePart('m',[Create Date]) AS [Month], 
    Count(backlog.[Ticket ID]) AS [Count of Registered] 
FROM backlog 
GROUP BY backlog.System, 
    DatePart('yyyy',[Create Date]), 
    DatePart('m',[Create Date]);

结果:

- Col1 | Year | Month | Count 
- System1 | 2013 | 1 | 25 
- System1 | 2014 | 1 | 12 
- System1 | 2014 | 2 | 6 
- System2 | 2013 | 1 | 4 
- System2 | 2013 | 2 | 56

我想得到这个:

- Col1 | Year | Month | Count | Average
- System1 | 2013 | 1 | 25 | 25
- System1 | 2014 | 1 | 12 | 9
- System1 | 2014 | 2 | 6 | 9
- System2 | 2013 | 1 | 4 | 30
- System2 | 2013 | 2 | 56 | 30

2 个答案:

答案 0 :(得分:1)

我发现ms-access查询可能很冗长乏味。你有时必须对他们有创意。通常帮助我的是使用子查询。如果这不起作用,您可能需要将其分解为几个不同的表并让它们相互访问数据。您可以将动态查询保存为表名,并在另一个查询中引用它们。

我认为您也可以使用YEAR()和MONTH()函数来清理SQL。例如:

  import java.util.Scanner;

 public class PrimeGenerator{

 public static void main(String[] args){

      Scanner userInput = new Scanner(System.in);
      String wordLength = new String();
      String output = new String();
      int linenumber = 1;


 int i = userInput.nextInt();


 while(i>=1||i<=1000){

 int wordlength;

     String dataset = userInput.next();
     String dataset2 = userInput.next();


     boolean haslowercase = !dataset2.equals(dataset2.toLowerCase());

     if(!haslowercase){
         break;
     }else{

     wordLength = String.valueOf(dataset);

     int dataset2length = dataset2.length();

    wordlength = Integer.parseInt(wordLength);
     if(wordlength>80){
         break;
     }
     else{

         if(wordlength>dataset2length){
             break;
         }else{

     output = dataset2.substring(0,wordlength-1)+ dataset2.substring(wordlength,dataset2.length());

     System.out.println(linenumber+" "+output);
     linenumber++;
         }
     }
     }
 }








}

答案 1 :(得分:1)

不知道的平均值,可能是:

SELECT 
    backlog.System, 
    DatePart('yyyy',[Create Date]) AS [Year], 
    DatePart('m',[Create Date]) AS [Month], 
    Count(*) AS [Count of Registered] 
    Avg([Registered]) AS AvgRegistered
FROM 
    backlog 
GROUP BY 
    backlog.System, 
    DatePart('yyyy',[Create Date]), 
    DatePart('m',[Create Date]);