我通过ODBC
通过Excel
连接访问SQL和数据库。这样我就可以创建可刷新的报告。我在使用日期?
条件时使用between
,以便我可以将此报告用于不同的日期范围。
问题是,当我使用OR
条件时,我最终必须将日期范围放入4次,或输入8个不同的日期。有什么方法可以简化以下内容,这样我就不必多次输入日期了吗?
Select CFF2X AS TYPE, Count(*) AS COUNT
FROM ZBP602F.SIH
JOIN ZBP602UF.NRCMCVL1 ON SICUST=CUSTX
WHERE SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 and CFF2X <> (' ') AND IHOCLS IN (004, 800, 007, 100, 008, 102, 104, 140, 105, 110, 111,109)
OR SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 and CFF2X = (' ') AND IHOCLS IN (004, 800, 007, 008, 100, 102, 104, 140, 105, 110, 111)
OR SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 AND IHOCLS IN (114) and SIPAY in ('A', 'E')
OR SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 AND IHOCLS IN (114) and SIPAY = 'B' and CFF2X <> (' ')
Group by CFF2X
答案 0 :(得分:1)
WHERE SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) AND SITOT <> 0
AND (
(CFF2X <> ' ' AND
IHOCLS IN (004, 800, 007, 100, 008, 102, 104, 140, 105, 110, 111,109)
)
OR (CFF2X = ' ' AND
IHOCLS IN (004, 800, 007, 008, 100, 102, 104, 140, 105, 110, 111)
)
OR (IHOCLS = 114 and SIPAY in ('A', 'E'))
OR (IHOCLS = 114 and SIPAY = 'B' and CFF2X <> ' ')
)
您只需将不变的项目放入一次,然后将更改的位置作为几个OR语句。像这样简化布尔逻辑的过程称为逻辑简化(想想卡诺图)。您还可以在此处执行其他优化。
答案 1 :(得分:1)
我会尝试使用一些括号来防止重复性。
如果您将WHERE更改为:
WHERE (SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0) AND
(
(CFF2X <> (' ') AND IHOCLS IN (004, 800, 007, 100, 008, 102, 104, 140, 105, 110, 111,109)) OR
(CFF2X = (' ') AND IHOCLS IN (004, 800, 007, 008, 100, 102, 104, 140, 105, 110, 111)) OR
(IHOCLS IN (114) and SIPAY in ('A', 'E')) OR
(IHOCLS IN (114) and SIPAY = 'B' and CFF2X <> (' '))
)
;
这将首先检查所有记录的SIINVD,SICOMP和SITOT是否与所有记录相同。然后将匹配4个不同的案例。由于缺少括号,您所拥有的当前查询实际上可能无法实现您的想法。