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

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 飘扬De黑夜

SqlServer 批量修改某个字段类型 U8数据库表汇总 FastReport 使用JSON作为数据源,手工赋值和导出PDF elementui关于表单校验validator Vue.js 2 使用 extends 扩展 element-ui 组件 FastReport-Barcode/QRCode 控件总是存在白色空白区域 双网卡同时访问内外网 vs项目启动后总是提示ID为XXX的进程当前未运行 Vue2全局管理格式化器和Filter uniapp 以npm模式安装插件的项目,如何让自定义组件支持easycom方式自动导入 Http请求中 Content-Type 常用格式说明 WPF入门基础之双向数据绑定 WPF入门之设置样式 ElementUI-如何给 el-dropdown 的 command 事件传递多个参数 TortoiseGit使用 管理系统中台示例参照 淘宝源安装npm visual studio 2022 自定义组件和自定义控件和用户控件的区别 SqlServer GoupBy 分组后对于非分组的某个字符串进行拼接
SQL Server 实现类似CountIF的函数
飘扬De黑夜 · 2024-07-08 · via 博客园 - 飘扬De黑夜

需求说明:

我有一个客户表,还有一个客户的体验记录表,我需要汇总客户体验信息如下:

客户名称,本周体验次数,本月体验次数,本年度体验次数,累计体验次数,首次体验时间,最近体验时间  信息,如何用一个SQL语句搞定:

COUNT函数官方说明:指定 COUNT 返回唯一非 Null 值的数量。

所以变相实现如下:

SELECT
        me.CustomerCode,
        WeekExperienceCount = COUNT(CASE WHEN ExpDate>DATEADD(DAY,-7,GETDATE()) THEN 1 ELSE NULL END),
        MonthExperienceCount = COUNT(CASE WHEN ExpDate>DATEADD(MONTH,-1,GETDATE()) THEN 1 ELSE NULL END),
        YearExperienceCount = COUNT(CASE WHEN ExpDate>DATEADD(YEAR,-1,GETDATE()) THEN 1 ELSE NULL END),
        TotalExperienceCount = COUNT(me.RowUid),
        FirstExperienceDate = MIN(me.ExpDate),
        LastExperienceDate = MAX(me.ExpDate)
    FROM dbo.MD_Experience me
    GROUP BY me.CustomerCode