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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
S
SegmentFault 最新的问题
腾讯CDC
博客园 - 叶小钗
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Jina AI
Jina AI
A
About on SuperTechFans
博客园 - 司徒正美
C
Check Point Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
小众软件
小众软件
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
K
Kaspersky official blog
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
D
DataBreaches.Net
A
Arctic Wolf
I
InfoQ
量子位
IT之家
IT之家
Security Latest
Security Latest
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
P
Proofpoint News Feed
P
Privacy International News Feed
T
Threatpost
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Martin Fowler
Martin Fowler
C
Cyber Attacks, Cyber Crime and Cyber Security
PCI Perspectives
PCI Perspectives
F
Full Disclosure

博客园 - 江宇旋

查询反模式 - 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

复制代码