sql - SQL query to return all columns of max date for group by but exclude records where a different column for that group is not null
问题描述
For my example data set in the image below, I need to return only records for the MAX DueDate while Grouping on CompletedCertificationChecklist_Id. But if there is data in the CompletedDate, filter that CompletedCertificationChecklist_Id out.
The report will show me only open records (not completed) with the most recent DueDate
Table Query
SELECT [Id_CertificationHandsOnAssesment]
,[CompletedCertificationChecklist_Id]
,[DueDate]
,[CompletedDate]
FROM [sccCertificationHandsOnAssesments]
解决方案
if I understand correctly :
select * from (
select *
, row_number() over (partition by CompletedCertificationChecklist_Id order by DueDate desc) rn
, max(case when CompletedDate is not null then 1 else 0 end) over (partition by CompletedCertificationChecklist_Id) IsCompleted
from sccCertificationHandsOnAssesments
) t
where IsCompleted = 0 and rn = 1
推荐阅读
- android - 使用导航组件时如何知道堆栈中的片段数?
- java - java中的Object[] obj和Object obj[]有什么区别?
- c# - NuGet 包如何知道它们的 `using` 指令?
- sql - 显示每个月之前销售日期 > 3 个月的客户数量以及在给定月份具有“销售日期”的客户数量
- ios - 在 InsertRows 之后 UItableView 滚动到底部并在 ios Xamarin 中的现有行上方插入动画
- c - mvwprintw 在 for 循环中意外动作
- php - Laravel 使用队列解析
- python - python在json中查找与值相关的值
- android - 需要在 Android 10.0 上为蓝牙低功耗扫描启用位置信息
- javascript - 如何从 datepicker Angular 材料中读取日期