sql - 如何计算不同的值和日期小于同一行
问题描述
我需要知道我今天有多少标题和字幕,有多少超过 60 天。我需要帮助计算超过 60 天的标题。想过使用这样的东西:count(distinct [Tracking ID1] case when [Date] < DATEADD(DAY, -60, '1/5/2021')
但这不起作用。
我不能使用 where 语句,因为它会影响 [Titles] 和 [Subtitles] 列。
select
[Line of Business],
count([Tracking ID]) as [Total # Subtitles],
count(distinct [Tracking ID1]) as [Total # Titles],
count(case when [Date] < DATEADD(DAY, -60, '1/5/2021') then 1 else 0 end) as [# Subtitles No Action Yet over 60 days],
count(distinct [Tracking ID1] case when [Date] < DATEADD(DAY, -60, '1/5/2021') as [# Titles No Action Yet over 60 days]
from table
where
[Status] like '%active%'
group by [Line of Business]
order by [Line of Business]
解决方案
使用 count 而不是 sum :
select
[Line of Business],
count([Tracking ID]) as [Total # Subtitles],
count(distinct [Tracking ID1]) as [Total # Titles],
COUNT(case when [Date] < DATEADD(DAY, -60, '1/5/2021') then 1 else NULL end) as [# Subtitles No Action Yet over 60 days],
COUNT(distinct CASE when [Date] < DATEADD(DAY, -60, '1/5/2021') THEN [Tracking ID1] ELSE NULL END) AS [# Titles No Action Yet over 60 days]
from table
where
[Status] like '%active%'
group by [Line of Business]
order by [Line of Business]
推荐阅读
- hash - 为什么几乎所有的网络服务器都不在客户端散列您的密码?
- google-apps-script - 从运行脚本的 Google 表格中格式化 Google Drive 搜索查询以获取名称 [first & last] 的最佳方法是什么?
- excel - 循环变量的 VBA
- vb.net - 我如何无法获得我所有的 combobox1 并发送到多行文本框
- youtube-api - 如何使用 angularfire2 FirebaseAuth Google 登录对 Youtube 进行身份验证?
- javascript - 异步调用
- sql - (QUERY) - 从特定范围中选择行
- c++ - C++:将 const 用于当前是枚举的函数参数
- angular - Angular:HTML 画布应该是组件还是服务?
- statnet - 节点动态——STATNET / TERGM 中的进入和退出