• 子分类的合计


            Groupby可以进行分类的合计,如果要进一步进行子分类的合计并在一行显示,关键问题是分类的字段如何被子查询使用。我试了一些办法,觉得这样可行,分类时,就把分类字段名字改了,这样就可以在子查询中使用而不会重复了。——说的比较绕,看例子还是比较清楚的。
     

    SELECT BJ,
              (SELECT COUNT(*)
             FROM [1]
             WHERE (性别 = '男') AND (班级 = bj)) AS 男生,
              (SELECT COUNT(*)
             FROM [1]
             WHERE (性别 = '女') AND (班级 = bj)) AS 女生,
              (SELECT COUNT(*)
             FROM [1]
             WHERE (班级 = bj)) AS 总计
    FROM (SELECT 班级 AS BJ, *
            FROM [1]) DERIVEDTBL
    GROUP BY BJ


    附,失败的尝试:

    SELECT 班级,
              (SELECT COUNT(*)
             FROM [1] b
             WHERE (性别 = '男') AND (b.班级 = 班级)) AS 男生
    FROM [1]
    GROUP BY 班级

    SELECT 班级,
    FROM [1]
    WHERE (班级 IN
              (SELECT DISTINCT 班级
             FROM [1]))

  • 相关阅读:
    蘑菇街
    康拓展开
    CSS学习笔记
    专业名词
    专业名字
    01背包问题
    将bbr功能合入到centos7.3
    How to Identify User&Password of DataBase safely in SQL statement?
    tips for private constructor
    all Key Word of C#
  • 原文地址:https://www.cnblogs.com/jetz/p/1147185.html
Copyright © 2020-2023  润新知