sql-server - SQL查询在表中获得一个“行”
问题描述
我需要从 SQL 中的表中获取结果列表,以便我需要日期值紧接在 where 语句选择的行之前的行。在下图中,我会写:
select * from Table t where Color = 'blue'
但我不知道如何获得突出显示的蓝色行上方的行。示例是 Excel,但只是为了便于生成示例。现实生活中的“蓝色”是更多的连接和数据,但对于一个最低限度的完整示例,这是可行的。假设日期并不总是按日期时间排序,但我总是希望该条目紧挨在“蓝色”条目之前,日期时间明智。
解决方案
如果没有重复的日期,则使用 UNION ALL:
select * from Table where Color = 'blue'
union all
select * from Table t
where
t.Color is null
and
(
select color from Table where date = (
select min(date) from Table where date > t.date)
) = 'blue'
order by date
推荐阅读
- python - Pandas 在计算平均值时保留原始列
- css - Zurb Foundation 为数据库图像渲染二进制输出
- java - 在运行时编译 + 执行 Java Intellij Idea
- amp-html - Amp for Email 和 Google 字体验证错误
- reactjs - How to set aws amplify data in localStorage ReactJS
- python - 使用 OpenCV 确定一个点是否在 ROI 内
- pycharm - pycharm - 为什么解释器设置不正确?
- javascript - 有没有办法找出文本是否溢出并将其用作代码中的条件?
- angular - 标签没有打印任何东西
我正在制作一个应用程序来显示一些用户信息,并且我正在尝试使用角度/材料库来制作表格。由于某种原因,mat-table 甚至不会打印出列的标题。
我尝试更改数据源数组的类型,并尝试使用标记而不是使用标记的 mat 表的其他实现
我已将我的代码的简化版本放在 stackblitz 上, java - 如何使用 Java FB Business SDK 获得 Facebook 广告帐户的转化?