使用pentaho report参数使用嵌套的AND / OR修改SQL查询

时间:2014-09-18 21:00:45

标签: sql pentaho pentaho-design-studio

我正在尝试使用下拉菜单让报告像2个单独的报告。 这是一个名为rtype的参数,值为0-2。

数据是客户预约,我正在尝试报告他们保留约会的能力。

选项1和2我们正在寻找他们的第一次约会,因此在子查询寻找先前约会的位置中的'并且不存在'部分。

列表3本质上是list1 + list2(还有一点)但是我们不在乎它是否是初始约会

select sa.organization, sa.status, count(1)
 from mytable as sa
where  sa.service_date between  ${sdate} and ${edate}
 and (
	(${rtype}::int = 0 and sa.activity_id in (..) /*list 1*/ )
	OR(${rtype}::int = 1 and sa.activity_id in (..)  /*list 2*/)
	OR(${rtype}::int = 2 AND sa.activity_id not in (..) /*list 3*/)
)

and ( 
	(${rtype}::int !=2 AND not exists (select *
                     from rpt_scheduled_activities as sa2
                    where sa2.client_id = sa.client_id
                      and (   
							(${rtype}::int = 0 and sa.activity_id in (..)/*list 1*/ )
							OR
							(${rtype}::int = 1 and sa.activity_id in (..)  /*list 1*/ )
							)
                      and sa2.status in ('Kept')
					  and sa2.service_date <  sa.service_date
                      ))
       OR (${rtype}::int = 2))
     
group by 
		sa.organization,
          sa.status
order by 
		 sa.status

基本上我想在rtype = 2时“跳过”不存在的子查询。

有谁可以指出我做错了什么?

0 个答案:

没有答案