SQL DB2:使用min(date)的意外结果

时间:2013-02-14 16:07:22

标签: sql db2

这是令人讨厌的脚本:

select distinct person, min(pdate) as min_date from db
where ptype like 'A1%'
or (ptype like 'B1%'
and (pdate between '2000-01-01' and '2001-01-01'))
group by person

问题

min_date retreived还包括我指定的pdates的那些outisde。但是,当我取出其中一个ptypes时,我有:

select distinct person, min(pdate) as min_date from db
where ptype like 'A1%'
and (pdate between '2000-01-01' and '2001-01-01')
group by person
然后这个问题就消失了。为什么引入第二个ptype是在我指定的pdates之外重新调整实例?

感谢。

2 个答案:

答案 0 :(得分:5)

这是因为您现有的查询格式为:

IF A or (B and D)

- 所以如果A为真,则忽略D.您实际上想要查询表单:

IF (A or B) and D
像这样:

select distinct person, min(pdate) as min_date from db
where (ptype like 'A1%' or ptype like 'B1%') and 
      pdate between '2000-01-01' and '2001-01-01'
group by person

答案 1 :(得分:3)

select distinct person, min(pdate) as min_date from db
where (      ptype like 'A1%'
         or (ptype like 'B1%'))
and (pdate between '2000-01-01' and '2001-01-01')
group by person

一个人是falsh?