如何选择列的平均值" x"选择最多列" y"按15分钟间隔分组?

时间:2015-09-17 03:23:23

标签: mysql

我有一个看起来像这样的表:

CREATE TABLE `visits` (
  `idsite` int(10) unsigned NOT NULL,
  `idvisit` int(10) unsigned NOT NULL,
  `server_time` datetime NOT NULL,
  `actions` float DEFAULT NULL,
)

我希望在15分钟的间隔内获得actions的平均值,其中不同idvisit的总和最大。

我开始这样做:

select max(counted), server_time 
  from (select count(distinct idvisit) as counted, server_time 
     from visits group by year(server_time), month(server_time), day(server_time), ( 4 * HOUR( server_time ) + FLOOR( MINUTE( server_time ) / 15 )) ) as counts;

我获得了最多的访问次数,但错误server_time。我不知道如何获得有关孔的行,以便在正确的时间间隔内选择动作的平均值。

示例数据:

+--------+---------+--------------+---------------------+
| idsite | idvisit | actions      | server_time         |
+--------+---------+--------------+---------------------+
|      1 |       1 |           14 | 2015-09-15 22:06:57 |
|      1 |       2 |           60 | 2015-09-16 22:09:41 |
|      1 |       3 |            5 | 2015-09-16 22:09:54 |
|      1 |       3 |           40 | 2015-09-16 22:16:58 |
|      1 |       4 |            6 | 2015-09-16 22:19:04 |
|      1 |       5 |            7 | 2015-09-16 22:40:53 |
|      1 |       6 |            1 | 2015-09-16 23:01:41 |
|      1 |       6 |            5 | 2015-09-16 23:08:54 |
|      1 |       7 |           10 | 2015-09-16 23:19:58 |
|      1 |       8 |            6 | 2015-09-16 23:26:14 |
|      1 |       9 |            7 | 2015-09-16 23:48:53 |
+--------+---------+--------------+---------------------+

预期结果:

+--------------+---------------------+
| AVG(actions) | DATE(server_time)   |
+--------------+---------------------+
|        26.33 | 2015-09-15          |
+--------------+---------------------+

对应于最多有不同idvisit的前15分钟。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

如果要在固定的15分钟间隔内计算idVisits:

之类的东西
 package org.springframework.security.web.authentication;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.security.authentication.AuthenticationServiceException;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.AuthenticationException;
    import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
    import org.springframework.util.Assert;

    public class CustomUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
        public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username";
        public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password";
        /** @deprecated */
        @Deprecated
        public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";
        private String usernameParameter = "j_username";
        private String passwordParameter = "j_password";
        private String organisationParameter = 'j_organisation'
        private boolean postOnly = true;

        public UsernamePasswordAuthenticationFilter() {
            super("/j_spring_security_check");
        }

        public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
            if(this.postOnly && !request.getMethod().equals("POST")) {
                throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
            } else {
                String username = this.obtainUsername(request);
                String password = this.obtainPassword(request);
                String password = this.obtainOrganisation(request);

              //regular implementation in spring security plugin   /**
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
                this.setDetails(request, authRequest);
                return         this.getAuthenticationManager().authenticate(authRequest);
            }
**/

//Your custom implementation goes here(Authenticate on the basis of organisation as well). Here you need to customise authenticate as per your requirement so that it checks for organisation as well.
        }

       protected String obtainOrganisation(HttpServletRequest request) {
        return request.getParameter(this.organisationParameter);
    }    

        protected String obtainPassword(HttpServletRequest request) {
            return request.getParameter(this.passwordParameter);
        }

        protected String obtainUsername(HttpServletRequest request) {
            return request.getParameter(this.usernameParameter);
        }

        protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
            authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
        }

        public void setUsernameParameter(String usernameParameter) {
            Assert.hasText(usernameParameter, "Username parameter must not be empty or null");
            this.usernameParameter = usernameParameter;
        }

        public void setPasswordParameter(String passwordParameter) {
            Assert.hasText(passwordParameter, "Password parameter must not be empty or null");
            this.passwordParameter = passwordParameter;
        }

        public void setPostOnly(boolean postOnly) {
            this.postOnly = postOnly;
        }

        public final String getUsernameParameter() {
            return this.usernameParameter;
        }

        public final String getPasswordParameter() {
            return this.passwordParameter;
        }
    }

您可以使用以下sql:

00:00:00 - 00:14:59 -- x values
00:15:00 - 00:29:59 -- y values
00:30:00 - 00:44:59 -- z values
00:45:00 - 00:59:59 -- w values

示例:http://sqlfiddle.com/#!9/36a80f/30