背景:最近用到统计之类的复杂Sql比较多,有种"提笔忘字"的感觉,看书练习,举一反三,巩固加强. (一) <SQL进阶教程>学习记录--CASE (二) <SQL进阶教程>学习记录--GROUP BY.PARTITION BY 1.语法 两种写法:简单 CASE 表达式(simple case expression).搜索 CASE 表达式(searched case expression) -- 简单 CASE 表达式 CASE sex WHEN '1' TH…
写在前面:本文主要注重 SQL 的理论.主流覆盖的功能范围及其基本语法/用法.至于详细的 SQL 语法/用法,因为每家 DBMS 都有些许不同,我会在以后专门介绍某款DBMS(例如 PostgreSQL)的时候写到. 第 1 章 DBMS 与 SQL 1.DBMS 是什么 ? 数据库管理系统(Database Management System, DBMS) 是用来管理数据库的计算机系统. 本文采用 PostgreSQL 为 DBMS. 2.为什么要用 DBMS ? 问:为什么不用 文本文件 或…
SQL进阶一整个是根据我看了pdf版本的整理以及自己的见解整理.后期也方便我自己查看和复习. CASE 表达式 CASE 表达式是从 SQL-92 标准开始被引入的.可能因为它是相对较新的技术,所以尽管使用起来非常便利,但其真正的价值却并不怎么为人所知.很多人不用它,或者用它的简略版函数,例如 DECODE(Oracle).IF (MySQL)等.然而,正如 Joe Celko 所说,CASE表达式也许是 SQL-92 标准里加入的最有用的特性.如果能用好它,那么 SQL 能解决的问题就会更广泛…
一.CASE的两种用法 1.1 等值判断->相当于switch case (1)具体用法模板: CASE expression WHEN value1 THEN returnvalue1 WHEN value2 THEN returnvalue2 WHEN value3 THEN returnvalue3  ELSE defaultreturnvalue END (2)具体使用示例: 假设我们有一个论坛网站,其中有一张User表{ UId,Name,Level },Level是一个int类型,代…
select name from greatestsORDER BY case when name ='B' then 1 when name ='A' then 2 when name ='D' then 3 when name ='C' then 4 else 5 end…
select name, case when case when x > y then x else y end < z then z else case when x < y then y else x end end as greasterfrom greatests…
select case when sex = 1 then '男性' else '女性' end as '性别', sum(case when name='哈尔滨' THEN population else 0 end) as '哈尔滨', sum(case when name='大庆' then population else 0 end) as '大庆', sum(case when name='齐齐哈尔' then population else 0 end) as '齐齐哈尔', sum…
SQL 权威指南SQL 解惑在进行非定制化统计时,需要将已有编号方式转换为另外一种便于分析的方式进行统计需求 select case when name='哈尔滨' then '黑龙江' when name='大庆' then '黑龙江' when name='齐齐哈尔' then '黑龙江' when name='长春' then '吉林' when name='吉林' then '吉林' when name='沈阳' then '辽宁' when name='大连' then '辽宁' wh…
1.只加入一个社团的学生的社团id select std_id, max(club_id) from student_clubgroup by std_idhaving count(*) =1------------------------------2.加入多个社团的学生的主社团id select std_id,club_id from student_club where main_club_flag ='Y'-------------------------------------sele…
进行不同条件的统计是case表达式的著名用法之一 select name,sum(case when sex = 1 then population else 0 end) as cnt_m,sum(case when sex = 2 then population else 0 end) as cnt_ffrom city_population_copygroup by name 实现了行转列…