昨天试了你写SQL应该对着呢(我对业务不太熟悉,请允许我这么说),在里非常感谢一下,但是还有一点需要改一下,谢谢。---------------------------------------
1.1 单位信息T_FSBASEUNIT序号 字段名称 数据类型 描述 备注
1 ItemId Number(9) 唯一标识号
2 UnitCode Varchar2(18) 单位(部门)编码
3 UnitName Varchar2(100) 单位(部门)名称
4 IsSector Varchar2(1) 是否部门
5 ParentUnitID Number(9) 上级单位 当为部门时是0
3.1 缴款书详细信息T_FSCHARGEBILL(Bill表) 序号 字段名称 数据类型 描述 备注 
1 ItemID Number(9) 流水号 
2 BillTypeID Number(9) 票据种类唯一标识号 
3 BillCode Varchar2(20) 单据号 
4 UnitCode Varchar2(18) 执收单位唯一标识号 
5 FillDate Varchar2(12) 填制日期 
3.2 缴款书细单T_   FSCHARGEBILLDETAIL(Bill与项目挂勾) 序号 字段名称 数据类型 描述 备注 
1 ItemID Number(9) 流水号 
2 BillheadID Number   (9) 缴款书流水号 
3 FromItemID Number   (9) 项目编码流水号 
4 Fund Number(15,2) 金额 
5 Amount Number(9) 数量 
1.6 项目信息T_FSITEM 序号 字段名称 数据类型 描述 备注 
1 ItemID Number(9) 唯一标识号 
2 ItemCode Varchar2(18) 项目编码 
3 ItemName Varchar2(100) 项目名称 
4 IncomeType Varchar2(4) 收入分类 
5 FundClass Varchar2(4) 资金性质 -----------------------------------------------------(你写的语句)
select a.UnitName,a.UnitCode,d.itemname,d.itemcode,
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '政府性基金收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '政府性基金收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '专项收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '专项收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '彩票资金收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '彩票资金收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '行政事业性收费收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '行政事业性收费收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '罚没收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '罚没收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '国有资本经营性收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '国有资本经营性收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '国有资源(资产)有偿使用收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '国有资源(资产)有偿使用收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入国库' and d.IncomeType = '其他收入' then c.fund else 0 end),
  sum(case when d.FundClass = '缴入专户' and d.IncomeType = '其他收入' then c.fund else 0 end)
from T_FSBASEUNIT a,T_FSCHARGEBILL b , T_FSCHARGEBILLDETAIL c,T_FSITEM d
where a.itemid = b.unitid and b.itemid = c.billheadid and c.fromitemid = d.itemno
group by a.UnitName,a.unitcode,d.itemname,d.itemcode
order by a.UnitName,a.unitcode,d.itemname,d.itemcode------------------------------------
再麻烦改一下,在 3.1 缴款书详细信息T_FSCHARGEBILL(Bill表) 里有个FillDate,这个要求是选择了日期范围(开始日期--结束日期)之后然后再实现后面的查询,所以应该是先在  3.1 缴款书详细信息T_FSCHARGEBILL(Bill表) 表里进行日期筛选之后,然后在用上面的语句查询2.就是还有一个部门汇总要求(和原来那个是分开的),上面的不是单位出来了吗?还有就是当选择单位/部门之后(从下拉框里选),单位应该和上面的是一样的.而部门会列出这个部门下的所有单位来,当然相同的项目也得Sum.
                      项目1 
              帐单1
                   项目2
      单位1 
                   项目1
            帐单2  
                   项目3    (这时候项目1就应该合并,产生三个项目,你上面的语句已经实现了)
 
部门
                  项目1 
              帐单3
                   项目4
      单位2 
                   项目5
             帐单2  
                   项目6  (这时候就应该是显示部门编码,部门名称,项目...也就是项目1(三个相加),实现产生6个项目)
非常感谢.一会再开贴..再给100分.

解决方案 »

  1.   

    在条件的地方加时间限制where   a.itemid   =   b.unitid   and   b.itemid   =   c.billheadid   and   c.fromitemid   =   d.itemno and FillDate >='2007-01-01' and FillDate < = '2007-12-08'
      

  2.   

    Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).在那个Where 那里加条件不行啊?
      

  3.   

    那那个部门汇总的呢.谢谢.--在group by .... 后加 with rollup这是sql server的写法.你这个库我就不知道了.
      

  4.   

    你Group By它怎么就知道哪个单位是哪个部门的呢? 我这按SQL  SERVER的写法写就可以了.
      

  5.   

    --这是SQL SERVER中的写法,如果出错,就是说你那个库不允许这么写,你自己找找对应的写法.
    where a.itemid=b.unitid and b.itemid=c.billheadid and c.fromitemid=d.itemno and b.FillDate>='2007-01-01' and b.FillDate<='2007-12-08' 
      

  6.   

    是啊,就是有两个日期下拉框,选择完日期之后然后在  3.1   缴款书详细信息T_FSCHARGEBILL(Bill表) 把日期过滤.然后在在这个过滤后的基础上进行你写的那些语句的查询...我按你说的加条件不行的啊.
    where       a.itemid       =       b.unitid       and       b.itemid       =       c.billheadid       and       c.fromitemid       =       d.itemno   and   FillDate   > ='2007-01-01'   and   FillDate   <   =   '2007-12-08' 错误提示:
    Invalid   expression   in   the   select   list   (not   contained   in   either   an   aggregate   function   or   the   GROUP   BY   clause). 因为:必须先过滤完日期之后再进行后面的查询统计,                                      项目1
                            帐单1  日期1
                                          项目2 
                单位1   
                                          项目1 
                            帐单2  日期2   
                                          项目3     这种情况下,项目1要合并的,但是分组的话,它不知道用哪个日期了啊.再次感谢   
      

  7.   

    大哥,我求你了,该说的我都说了.在sql server里面,我所给的代码肯定能行.至于你的那个库,我真不知道.
      

  8.   

    dqwugui兄,你看了我在10楼发的信息了吗?在哪个数据库都应该一样的吧.如果分组的话就要合并项目1,但是日期它知道用日期1还是日期2吗?难道要加在一起吗?真是不好意思,我真是不太懂,有什么不对的地方还请见谅.分一会我再开一贴,再给100,我是真的很着急.真的.如果能加你Q或者MSN可以吗?解决之后我都把分给你绝不食言的.谢谢,
      

  9.   

    主要是我没有最终结果的.不过我上面的应该说清楚了吧,你仔细看一下我10楼的信息.还有错误信息啊.真的,无论是SQL SERVER还是别的数据库肯定都会报错的(这一点应该对着呢),因为你分组了,项目要合并,但是日期怎么办呢?
      

  10.   

    主要是我没有最终结果的.我晕哦.真的,无论是SQL   SERVER还是别的数据库肯定都会报错的(这一点应该对着呢),因为你分组了,项目要合并,但是日期怎么办呢?分组和时间没任何关系,你加时间只是取你需要的记录,不加时间是取所有的记录.因为我前面已经sum()了.如果有怀疑:from   T_FSBASEUNIT   a,T_FSCHARGEBILL   b   ,   T_FSCHARGEBILLDETAIL   c,T_FSITEM   d 把T_FSCHARGEBILL b 这里改为你需要时间的子查询.
    (select 你需要的字段 from T_FSBASEUNIT where 时间...) t
      

  11.   

    你说的那个子查询应该可以,我试一试.
    你还是没明白我的意思,如果加时间限制是先从Bill表里选出这个时间限制的帐单来,然后再根据单位找项目去,而不是查询完了之后过滤,两者不一样的啊.
    先谢谢了.
      

  12.   

    你的说法是对的.不过,子查询慢.在where后加时间限制就行了.至于你那里为什么不行,我就不明白了.
      

  13.   

    恩,谢谢了,你说的那个子查询可以,非常辛苦,非常感谢..再帮忙弄一下部门吧.有点着急...如果你来石家庄我请我吃饭哟..帮了大忙了.
    那请问第二个问题,按果选择单位/部门的时候怎么办呢?如果选择了单位好办就是加个单位=''如果按部门的话呢,,因为部门也在Unit表里(根据IsSector Varchar2(1) 是否部门 来标识)必须按部门分类了.order by 应该不行吧,因为不知道哪个部门的,不过我想在你第一个基础上修改一下,当选出单位名称的时候,可以连它是不是部门选择出来,然后再order by 可以吗,我不懂..谢谢了..
      

  14.   

    那请问第二个问题同样可以在where后加条件限制.where ........... and (a.UnitName = '..' or a.unitcode = '...')如果不行,可按照上面的办法用子查询.
      

  15.   

    不客气.用子查询可以实现,但是速度应该会慢.如果把条件都放在where....后的那个地方,速度应该会快点.试试吧.
      

  16.   

    在分组的字段后加with rollup
    group by a.UnitName,a.unitcode,d.itemname,d.itemcode with rollup
      

  17.   

    select isnull(a.UnitName,'合计') UnitName,isnull(a.UnitCode , '合计') unitcode,isnull(d.itemname,'合计') itemname,isnull(d.itemcode,'合计') itemcode...... 
    group by a.UnitName,a.unitcode,d.itemname,d.itemcode with rollup
      

  18.   

    unit 表itemid  unitname unitcode issector
    1       财厅       100      1
    2       XX财政局   100100   0
    3       XX财政局   100101   0
    4       XX财政局   100102   0
    5       交通厅      200      1
    6       交通局      200100  0原来的语句出来的是:XX财政局   项目1
    XX财政局   项目2
    XX财政局   项目3
    交通局      项目4现在如果按部门汇总的话,项目1必须加上去的..我试着用  group by substr(a.UnitCode,1,3)可是不行.注:前三位是部门编码,后三位是单位.谢谢..
      

  19.   

    XX财政局       项目1 
    XX财政局       项目2 
    XX财政局       项目1
    交通局         项目4 说错了,应该是这样的.你看一下规则,谢谢了.
      

  20.   

    不支持的话,先用最初那个,然后union all下面这个.select 你需要的字段 , sum(统计字段) from .... group by ....注意,下面这个SQL语句的字段数量,内容,顺序必须和前面那个SQL一模一样.如果没有的,用空值或'合计'
    如: select col1 , '合计' col2, sum(col3) col3 from ... group by col1
      

  21.   

    不是太明白你的意思,能给写出来吗?SUM(统计字段)的意思是统计UnitCode吗?但那是Vchar的呀,
    谢谢了..
      

  22.   

    我好像明白了,你要统计的字段是:Fund吧,这样好像可以,但是得不出部门编码和部门名称来呀,那样得到的还是单位.
    我需要替换成部门的.
      

  23.   

    不支持的话,先用最初那个,然后union all下面这个.select 你需要的字段 , sum(c.fund) from .... group by ....注意,下面这个SQL语句的字段数量,内容,顺序必须和前面那个SQL一模一样.如果没有的,用空值或'合计'
    如: select col1 , '合计' col2, sum(col3) col3 from ... group by col1
      

  24.   

    select a.UnitName,a.UnitCode,d.itemname,d.itemcode,
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '政府性基金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '政府性基金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '专项收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '专项收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '彩票资金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '彩票资金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '行政事业性收费收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '行政事业性收费收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '罚没收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '罚没收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '国有资本经营性收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '国有资本经营性收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '国有资源(资产)有偿使用收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '国有资源(资产)有偿使用收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '其他收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '其他收入' then c.fund else 0 end),
      sum(c.fund)
    from T_FSBASEUNIT a,(select itemid, billtypeid, billcode, unitid, filldate from t_fschargebill where substr(filldate,1,10)>='2007-01-01' and substr(filldate,1,10)<='2007-02-01') b ,
     T_FSCHARGEBILLDETAIL c,T_FSITEM d
    where a.itemid = b.unitid and b.itemid = c.billheadid and c.fromitemid = d.itemno
    group by a.unitname,a.unitcode,d.itemname,d.itemcode
    order by a.UnitName,a.unitcode,d.itemname,d.itemcodeunion allselect a.UnitName,a.UnitCode,d.itemname,d.itemcode,
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '政府性基金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '政府性基金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '专项收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '专项收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '彩票资金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '彩票资金收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '行政事业性收费收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '行政事业性收费收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '罚没收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '罚没收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '国有资本经营性收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '国有资本经营性收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '国有资源(资产)有偿使用收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '国有资源(资产)有偿使用收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入国库' and d.IncomeType = '其他收入' then c.fund else 0 end),
      sum(case when d.FundClass = '缴入专户' and d.IncomeType = '其他收入' then c.fund else 0 end),
      '合计'
    from T_FSBASEUNIT a,(select itemid, billtypeid, billcode, unitid, filldate from t_fschargebill where substr(filldate,1,10)>='2007-01-01' and substr(filldate,1,10)<='2007-02-01') b ,
     T_FSCHARGEBILLDETAIL c,T_FSITEM d
    where a.itemid = b.unitid and b.itemid = c.billheadid and c.fromitemid = d.itemno
    group by a.unitname,a.unitcode,d.itemname,d.itemcode
    order by a.UnitName,a.unitcode,d.itemname,d.itemcode
    我这么写的,好像不对.
      

  25.   

    加个a.issector = 0,同理,如果不支持,用子查询.
    select   a.UnitName,a.UnitCode,d.itemname,d.itemcode, 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '政府性基金收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '政府性基金收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '专项收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '专项收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '彩票资金收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '彩票资金收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '行政事业性收费收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '行政事业性收费收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '罚没收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '罚没收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '国有资本经营性收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '国有资本经营性收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '国有资源(资产)有偿使用收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '国有资源(资产)有偿使用收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入国库'   and   d.IncomeType   =   '其他收入'   then   c.fund   else   0   end), 
        sum(case   when   d.FundClass   =   '缴入专户'   and   d.IncomeType   =   '其他收入'   then   c.fund   else   0   end), 
        sum(c.fund) 
    from   T_FSBASEUNIT   a,(select   itemid,   billtypeid,   billcode,   unitid,   filldate   from   t_fschargebill   where   substr(filldate,1,10)> ='2007-01-01'   and   substr(filldate,1,10) <='2007-02-01')   b   , 
      T_FSCHARGEBILLDETAIL   c,T_FSITEM   d 
    where   a.itemid   =   b.unitid   and   b.itemid   =   c.billheadid   and   c.fromitemid   =   d.itemno and a.issector = 0
    group   by   a.unitname,a.unitcode,d.itemname,d.itemcode 
    order   by   a.UnitName,a.unitcode,d.itemname,d.itemcode 
      

  26.   

    上面a.issector = 0显示有问题.
      

  27.   

    不错
    不管出于什么目的,现在好人不多了so as to 让我这个向来只看贴不回帖的 也冲动的 顶一把