每个局的邮政编码的MySQL查询频率

时间:2016-05-04 04:42:40

标签: mysql sql database count frequency

我正在尝试编写一个MySQL查询,以查看哪些邮政编码最常出现给我们的客户,并需要帮助才能完成。我的select语句末尾的 office 字段可以是每个邮政编码1到10种可能性。

我如何修复此查询以获取最常用的邮政编码,以及10个可能的办事处中哪些恰好在该邮政编码中有位置?

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/drawing.css" rel="stylesheet">
    <link href="css/result.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Pacifico" />

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="js/cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>

    <script src="js/drawing.js"></script>
    <script src="js/music_player.js"></script>
    <script src="js/coverConfetti.js"></script>
    <script src="js/dropdown.js"></script>

    <!-- JavaScript Libraries -->
    <script src="js/wad-master/build/wad.min.js"></script>
    <script src="lib/ngstorage/ngStorage.min.js"></script>

    <!-- Loki Libraries -->
    <script src="lib/lokijs/src/lokijs.js"></script>
    <script src="lib/lokijs/src/loki-angular.js"></script>
    <script src="../node_modules/loki-cordova-fs-adapter/bin/loki-cordova-fs-adapter.js"></script>
  </head>
  <body ng-app="starter">
    <!--
      The nav bar that will be updated as we navigate between views.
    -->
    <ion-nav-bar ng-controller="resultCtrl" class="bar-dark">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>

    <!--
      The views will be rendered in the <ion-nav-view> directive below
      Templates are in the /templates folder (but you could also
      have templates inline in this html file if you'd like).
    -->
    <ion-nav-view></ion-nav-view>
  </body>
</html>

我正在寻找的样本输出:

SELECT SUBSTRING(zip_code, 1, 5) AS zip, 
COUNT(*) AS freq, office
FROM customer_billing 
WHERE status != 'deleted'
GROUP BY zip
ORDER BY freq DESC

2 个答案:

答案 0 :(得分:1)

GROUP_CONCATDISTINCT一起使用。使用DISTINCT是必要的,因为对于给定的邮政编码,给定的办公室可能会多次出现。

SELECT SUBSTRING(zip_code, 1, 5), COUNT(*) AS freq,
    GROUP_CONCAT(DISTINCT office ORDER BY office DESC SEPARATOR ',')
FROM customer_billing
WHERE status != 'deleted'
GROUP BY SUBSTRING(zip_code, 1, 5)
ORDER BY freq DESC

答案 1 :(得分:1)

您可以使用group_concat

  

此函数返回带有连接非NULL的字符串结果   一组中的值。如果没有非NULL值,则返回NULL。   完整语法如下:

SELECT SUBSTRING(zip_code, 1, 5) AS zip, 
COUNT(*) AS freq, GROUP_CONCAT(office)
FROM customer_billing 
WHERE status != 'deleted'
GROUP BY zip
ORDER BY freq DESC