日期:2014-05-18  浏览次数:20348 次

链接三个表表查询,筛选问题
1、 个人基本表GRJBB  
(  
  BZH varchar2( 16 ), /* 保障号 */
  XM varchar2( 8 ) not null, /* 姓名 */
  CZLB number( 1) not null, /* 类别 */

 2、个人缴费表grjfb  
 (  
  BZH varchar2( 16 ), /* 保障号 */
  ND varchar2( 4 ) not null, /* 年度 */
  GZLJ1 number( 6 ), /* 工资累计1*/  
  GZLJ2 number( 6 ), /* 工资累计2*/  
  )  
3、缴费明表jfmxb
(  
  BZH varchar2( 16 ), /* 保障号 */
  ND varchar2( 4 ) not null, /* 年度 */
  JFGZ1 number( 4 ), /* 缴费工资1 */  
  dzrq1 string /*到账日期1*/

  JFGZ2 number( 4 ), /* 缴费工资2 */
  dzrq1 string /*到账日期2*/

  .............................
  JFGZ12 number( 4 ), /* 缴费工资12 */
  dzrq12 string /*到账日期12*/

  )

我要按年代排序查询出缴费表和明细表的上下半年的错误数据,GZLJ1<>JFGZ1+JFGZ2+JFGZ3+JFGZ4+JFGZ5+JFGZ6
语句是这样写的,也查出正确数据了

select a.bzh as 保障号,a.ztw as 状态,a.czlb as 财政类别, b.nd as 年代,b.GZLJ1 as 缴费表上半年,c.JFGZ1+c.JFGZ2+c.JFGZ3+c.JFGZ4+c.JFGZ5+c.JFGZ6 as 详细上半年
from grjbb as a,grjfb as b,jfmxb as c
where a.bzh=b.bzh and a.bzh=c.bzh 
and a.czlb=2 
and b.nd=c.nd
and b.GZLJ1<>c.JFGZ1+c.JFGZ2+c.JFGZ3+c.JFGZ4+c.JFGZ5+c.JFGZ6


但是现在需要多加一个条件筛选,就是凡是‘到账日期’是空的或者是‘注销’的,全部不要
如何添加条件呢?

------解决方案--------------------
是单个记录不要还是整个记录不要,如果是单个记录不要,可以参考一下语句,如果是整个记录不要,放到where里
SQL code
select a.bzh as 保障号,a.ztw as 状态,a.czlb as 财政类别, b.nd as 年代,b.GZLJ1 as 缴费表上半年,
case when len(isnull(c.dzrq1,''))=0 or c.dzrq1='注销' then 0 else c.JFGZ1 end
+case when len(isnull(c.dzrq2,''))=0 or c.dzrq2='注销' then 0 else c.JFGZ2 end
+case when len(isnull(c.dzrq3,''))=0 or c.dzrq3='注销' then 0 else c.JFGZ3 end
+case when len(isnull(c.dzrq4,''))=0 or c.dzrq4='注销' then 0 else c.JFGZ4 end
+case when len(isnull(c.dzrq5,''))=0 or c.dzrq5='注销' then 0 else c.JFGZ5 end
+case when len(isnull(c.dzrq6,''))=0 or c.dzrq6='注销' then 0 else c.JFGZ6 end
as 详细上半年
from grjbb as a,grjfb as b,jfmxb as c
where a.bzh=b.bzh and a.bzh=c.bzh  
and a.czlb=2  
and b.nd=c.nd
and b.GZLJ1<>c.JFGZ1+c.JFGZ2+c.JFGZ3+c.JFGZ4+c.JFGZ5+c.JFGZ6