作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试做这样的事情......
SELECT lc.Location, count(h.empID) as 'Count', lu.LBL
FROM swam.lookup lu
LEFT JOIN swam.empTable h
ON CASE
WHEN lu.sac is null
THEN lu.POS = h.POS
ELSE (
lu.POS = h.POS
and
lu.sac = h.sac)
INNER JOIN swam.Locations lc
ON h.LocationID = lc.LocationID
GROUP BY lc.Location, lu.LBL
ORDER BY lc.Location
因此,如果 lu.sac 为空,则仅在一个公共(public)列上连接表。如果不为空,则必须同时使用 POS 和 SAC 才能加入。
这可能吗?
最佳答案
不要使用大小写
。只需使用直接 bool 逻辑:
FROM swam.lookup lu LEFT JOIN
swam.empTable h
ON lu.POS = h.POS AND
(lu.sac is null OR lu.sac = h.sac) INNER JOIN
swam.Locations fs
ON h.LocationID = lc.LocationID
作为奖励,数据库引擎还可以在任一(或两个)表中利用 pos
上的索引。
关于sql - case within 加入 "ON"子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39850560/
我是一名优秀的程序员,十分优秀!