sql - 查找数据行中的第一个空列
问题描述
我看到了类似的问题,大部分都变成了关于表设计、规范化等的争论。并不是我们所有人都有让我们的客户改变他们的数据库设计的奢侈。
这是我的困境。我的客户要求我让他们的员工能够增加前一天的呼叫时间。我将提供一个弹出窗口,允许他们输入前一天的 callout_start 和 callout_stop。许多人已经在前一天接到了电话。
CREATE TABLE MyTable(
empNo int NOT NULL,
workDate datetime NOT NULL,
callout_start1 datetime NULL,
callout_stop1 datetime NULL,
callout_start2 datetime NULL,
callout_stop2 datetime NULL,
callout_start3 datetime NULL,
callout_stop3 datetime NULL,
callout_start4 datetime NULL,
callout_stop4 datetime NULL,
callout_start5 datetime NULL,
callout_stop5 datetime NULL,
callout_start6 datetime NULL,
callout_stop6 datetime NULL,
callout_start7 datetime NULL,
callout_stop7 datetime NULL,
callout_start8 datetime NULL,
callout_stop8 datetime NULL,
callout_hours decimal(5, 2) NULL
)
我希望他们能够更新前一天的下一个可用标注。IOWs - 第一个为空的标注。SQL 必须是通用的。我正在写入一个 dbIsam 数据库,该数据库将通过 remObjects 同步到一个 MS/SQL DB。
谢谢
解决方案
好吧,您可以使用复杂的 SQL 语句来查找第一个null
值。然后使用条件逻辑更新所有列:
with toupdate as (
select t.*,
(case when callout_start1 is null then 1
when callout_start2 is null then 2
. . .
end) as which
from t
)
update toupdate
set callout_start1 = (case when which = 1 then @param else callout_start1 end),
callout_start2 = (case when which = 2 then @param else callout_start2 end),
. . .
where <whatever>;
推荐阅读
- flutter - 检测颤动按下的音量键
- python - Selenium python没有选择所有信息
- php - php8 “502 bad gateway” 安装 brew 的 MacOS 上的 php-fpm 在哪里
- python - pandas:在应用函数中使用 value_counts
- firebase - Firebase 实时数据库流 onchildchanged
- java - 使用 ssl 的 grpc 连接问题
- python - Python 3.0.9 中的非响应式二维散点图
- python - 绘制子图,隐藏 x 轴标签
- python - Pandas 与 MLLib 的协方差计算的确切 Apache-Spark NA 处理差异是什么?
- google-apps-script - 从 Google Apps 脚本访问 GoogleSheet 链接的 BigQuery 表时出现“获取驱动器凭据时权限被拒绝”错误