› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
wangjincp
V2EX  ›  MySQL

求一个 MSSQL 分组统计 SQL 查询

  •  
  •   wangjincp · May 28, 2019 · 5065 views
    This topic created in 2685 days ago, the information mentioned may be changed or developed.

    这儿没有 MS-SQLServer 可选,就选择了 mysql

    字段 A

    aaaa-cc-a

    bvdsv-bv-s

    vdsavd-cc-a

    csaccc-cc-c-c

    fsafe-ff-s

    csacsa-cc-a

    gafff-bv-s

    ff-s,cc-c-c,cc-a,bv-s,cc-a 这些是未知的,唯一特点就是第一个"-"后面的值。

    需要得到的数据就是分组统计-后面数据有多少

    最后统计得到的数据

    cc-a 2

    bv-s 2

    cc-a 1

    cc-c-c 1

    ff-s 1

    3 replies  •  2019-05-28 17:33:42 +08:00
    LeeSeoung
        1
    LeeSeoung  
       May 28, 2019
    正则+groupby 或者 index+substr+groupby
    wangjincp
        2
    wangjincp  
    OP
       May 28, 2019
    谢谢各位了,已经找到解决方案了!!!
    非常感谢各位!!!

    SET QUOTED_IDENTIFIER ON
    GO
    --创建函数
    Create FUNCTION [dbo].[GetSubStrBySpint]
    (
    @String NVARCHAR(300),
    @split NVARCHAR(10)
    )
    RETURNS NVARCHAR(1024)
    AS

    BEGIN
    DECLARE @location INT
    DECLARE @start INT
    DECLARE @seed INT

    SET @String = LTRIM(RTRIM(@String))
    SET @start = CHARINDEX(@split, @String)
    SET @seed = LEN(@split)
    SET @start += @seed
    SET @location = LEN(@String) - @start + @seed

    RETURN SUBSTRING(@String, @start, @location)
    END

    我自己写了个函数
    然后在查询就好了
    谢谢各位!!!
    然后直接调用
    select * from (select dbo.GetSubStrBySpint([fieldA] , '.') as hz,count(1) as num from Table group by dbo.GetSubStrBySpint([fieldA] , '.')) as c order by c.num desc
    chitanda
        3
    chitanda  
       May 28, 2019 via Android
    要想省事,升级到 8.0 或者换 Maria
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2291 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:34 · PVG 17:34 · LAX 02:34 · JFK 05:34
    ♥ Do have faith in what you're doing.