我的目标是拥有相同的
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM CFE_DIR_PMOR INNER JOIN CFE_DIR_PPHY
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND CFE_DIR_PMOR.GESTDEL IS NULL
AND CFE_DIR_PPHY.GESTDEL = '1'
UNION ALL
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM table1
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND CFE_DIR_PMOR.GESTDEL = '1'
AND CFE_DIR_PPHY.GESTDEL IS NULL
UNION ALL
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM table1
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND CFE_DIR_PMOR.GESTDEL = '1'
AND CFE_DIR_PPHY.GESTDEL = '1'
AND CFE_DIR_PMOR.rep_legal= '1'
UNION ALL
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM table1
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND CFE_DIR_PMOR.GESTDEL = '1'
AND CFE_DIR_PPHY.GESTDEL = '1'
AND CFE_DIR_PMOR.rep_legal != 1
AND CFE_DIR_PPHY.ORDRE = '00'
正如您所看到的,它很麻烦,WHERE中的CASE可以是一个很好的解决方案。所以这就是我做的事情
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM table1
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND
CASE WHEN CFE_DIR_PMOR.GESTDEL IS NULL AND CFE_DIR_PPHY.GESTDEL = '1' THEN 1
WHEN CFE_DIR_PMOR.GESTDEL = '1' AND CFE_DIR_PPHY.GESTDEL IS NULL THEN 1
ELSE
CASE WHEN CFE_DIR_PMOR.GESTDEL = '1' AND CFE_DIR_PPHY.GESTDEL = '1' AND CFE_DIR_PMOR.rep_legal= 1 THEN 1
WHEN CFE_DIR_PMOR.GESTDEL = '1' AND CFE_DIR_PPHY.GESTDEL = '1' AND CFE_DIR_PMOR.rep_legal != 1 AND CFE_DIR_PPHY.ORDRE = '00' THEN 1
ELSE 0
END
END =1
问题:
由于
答案 0 :(得分:4)
使用OR
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM CFE_DIR_PMOR INNER JOIN CFE_DIR_PPHY
ON CFE_DIR_PMOR.[Something] = CFE_DIR_PPHY.[Something]
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND ((CFE_DIR_PMOR.GESTDEL IS NULL
AND CFE_DIR_PPHY.GESTDEL = '1')
OR (CFE_DIR_PMOR.GESTDEL = '1'
AND CFE_DIR_PPHY.GESTDEL IS NULL )
OR (CFE_DIR_PMOR.GESTDEL = '1'
AND CFE_DIR_PPHY.GESTDEL = '1'
AND CFE_DIR_PMOR.rep_legal= '1')
OR (CFE_DIR_PMOR.GESTDEL = '1'
AND CFE_DIR_PPHY.GESTDEL = '1'
AND CFE_DIR_PMOR.rep_legal != 1
AND CFE_DIR_PPHY.ORDRE = '00'))
答案 1 :(得分:2)
当case
执行时,您为什么要where
中的or
:
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM CFE_DIR_PMOR INNER JOIN
CFE_DIR_PPHY
ON ??
WHERE <cdt1> AND <cdt2> ... AND <cdt5> AND
( (CFE_DIR_PMOR.GESTDEL IS NULL AND CFE_DIR_PPHY.GESTDEL = '1') OR
(CFE_DIR_PMOR.GESTDEL = '1' AND CFE_DIR_PPHY.GESTDEL IS NULL) OR
. . .
);
此外,如果您使用JOIN
,则应该有ON
子句。我不确定那里有哪些条件。
答案 2 :(得分:2)
使用一些逻辑来减少可能减慢查询速度的OR
的数量
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM Table1
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND
(cast((isnull(@id2,'')+isnull(@id,'')) as int)=1)
OR
((CFE_DIR_PMOR.GESTDEL = '1'AND CFE_DIR_PPHY.GESTDEL = '1')
AND
(CFE_DIR_PMOR.rep_legal= '1' OR CFE_DIR_PPHY.ORDRE = '00'))
Table1与您想要执行的Inner Join
相同吗?
答案 3 :(得分:1)
**Combined common filters from the last two case statements**
SELECT CFE_DIR_PMOR.col1,CFE_DIR_PMOR.col2,CFE_DIR_PMOR.col3
FROM
CFE_DIR_PMOR INNER JOIN CFE_DIR_PPHY
ON CFE_DIR_PMOR.[Column] = CFE_DIR_PPHY.[Matching Column]
WHERE <cdt1> AND <cdt2> ... AND <cdt5>
AND
(
(CFE_DIR_PMOR.GESTDEL IS NULL AND
CFE_DIR_PPHY.GESTDEL = '1')
OR
(CFE_DIR_PMOR.GESTDEL = '1' AND
CFE_DIR_PPHY.GESTDEL IS NULL )
OR
(
(CFE_DIR_PMOR.GESTDEL = '1' AND
CFE_DIR_PPHY.GESTDEL = '1')
AND
(
(CFE_DIR_PMOR.rep_legal= '1')
0R
(CFE_DIR_PMOR.rep_legal != 1
AND CFE_DIR_PPHY.ORDRE = '00')
)
)
)