惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 江宇旋

查询反模式 - GroupBy、HAVING的理解 T-SQL 公用表表达式(CTE) SQL语句 - 嵌套查询 SQL 操作结果集 -并集、差集、交集、结果集排序 - 江宇旋 SQL查询 - 表连接 SQL语句 - 数据操作 SQL语句 - 基本查询 SQL Server 事务语法 SQL 存储过程入门(事务)(四) SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因 SQL Server 的事务和锁(二)-Range S-S锁 SQL Server 的事务和锁(一) winform窗体间传值 C#获取存储过程的 Return返回值和Output输出参数值 SQL函数说明大全 - 江宇旋 C#操作字符串方法总结<转> C#调用存储过程带输出参数或返回值 车辆售票坐位图 C#操作SQL Server通用类
疑难杂症 - SQL语句整理
江宇旋 · 2016-02-12 · via 博客园 - 江宇旋

一、关联子查询-查日期最新列

  前天在工作中遇到一条非常有用的SQL语句,想了好久愣是没搞出来。今天将这个问题模拟出来:先看表

  

  需求是,对于每个人,仅显示时间最新的那一条记录。

  答案如下:

select * from record as a 
where not exists (select null from record as b where a.Name = b.Name and a.CreateTime < b.CreateTime)

  结果如下:

  

  这个问题的关键难点在于,既要去除重复,又要显示多个列。这样一来distinct就无效了,groupby又无效了。

  写成上面的样子,可能比较难看,但是写成下面这个样子应该就看得懂了:

select * from record as a 
where not exists 
(select * from record as b where a.Name = b.Name and b.CreateTime > a.CreateTime)

  整条SQL语句的意思可以描述为,查询表a的数据,当名字相同且存在时间更加新的,则不要这条(一直排除到时间是最新的)。exists运算符,只关注子SQL有没有结果集返回,因此在exists里select * 与 select null意义一样,也就是说,如果名字相等,且有创建时间比较新的,则不要这条,直到最新的。

  sql查询语句以select关键字开始,由各种字句组成。select语句的完整语法较复杂,常见的sql查询语句的语法结构如下。

  关联子查询只是此问题的其中一个解法,更多的解决方案在这个地址有:http://www.cnblogs.com/kissdodog/p/3365789.html

二、distinct关键字用于聚合函数中

  distinct关键字也能够用于聚合函数里面,意为在聚合之前将所有的重复行先排除,所以返回的结果会更少:

  select count(distinct person_name)

三、CASE...WHEN基于列的逻辑表达式

  关于这个东西,特别写了篇文章,地址如下:http://www.cnblogs.com/kissdodog/p/3154371.html

  现在来写个实例:先给出一张表:

    

    要求查出以下信息:

    

     SQL语句如下:

select Team,Rq, sum(case when winlose='胜' then 1 else 0 end) as 胜,sum(case when winlose='负' then 1 else 0 end) as 负
from test
group by Rq,Team
having Team = '曼联'

     再来一个,一张表只有Id,Sex两个字段,要求用一条SQL语句将Sex字段的'男'变'女','女'变'男'。

     

update table_1 
set sex = (case when sex='男' then '女' when sex='女' then '男' end)

    执行完SQL语句后,结果如下:

    

    再来一个有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。

    SQL语句如下:

复制代码

select Id,
    (case 
            when chinese >= 80 then '优秀' 
            when chinese >= 60 then '及格' 
            else  '不及格' 
    end) as 语文,
    (case 
            when math >= 80 then '优秀' 
            when math >= 60 then '及格' 
            else '不及格' 
    end) as 数学,
        (case 
            when english >= 80 then '优秀' 
            when english >= 60 then '及格' 
            else '不及格' 
    end) as 英语
from fenshu

复制代码