1.两个表中都有a,b,c,d,根据Group By a,b,c,d,如何把两个表中的其他字段的和值连接起来写成一条记录,sql语句就类似下面的:
Select Sum(a.x),Sum(a,xx),Sum(b.x),Sum(b,xx) from ……2.这个跟第一个差不多,也是两个表的和值连接起来写成一个记录,只是比第一个复杂,在第一个表中计算的和值要减去某一和值
详细说明一下:
在第一个表中有一个字段来标示该记录是否包含某记录的数据(就是类似父记录和子记录的关系),当统计和值的记录同时包含子父记录和子记录时,计算和值的时候要把子记录剔除掉。如何写sql? 并不是所有的时候都同时包含父子记录,只有判断当父记录含有子记录时,才要把子记录去掉不知道我说明白了没有,大家请帮帮忙

解决方案 »

  1.   

    select * from
    (
    select a,b,c,d,sum(e) e
    from table1
    group by a,b,c,d
    ) t1
    join
    (
    select a,b,c,d,sum(f) f
    from table2
    group by a,b,c,d
    ) t2 
    on t2.a = t1.a and t2.b=t1.b and t2.c = t1.c and t2.d = t1.d
      

  2.   

    select a,b,c,d, sum(case when 是子记录 then 0 else e end) e
    from table1
    group by a,b,c,d
      

  3.   

    1。两个表的外关联有4个键马?架设是的,语句如下:
       select Sum(a.x) as ax,Sum(a,xx) as axx,Sum(b.x) as bx,Sum(b,xx) as bxx from a inner join b on a.a = b.a and a.b=b.b and a.c=b.c and a.d=b.d group by a.a,a.b,a.c,a.d2. 参考语法:
       select (CASE WHEN a.fatherValue is not NULL and b.childValue is not Null
               THEN a.fatherValue ELSE b.childValue END) AS Value
       from a inner join b on a.a = b.a
      

  4.   

    第一个就是sql就是把两个表的结果合并到一起
    第一个表的结果Select a,b,c,d,Sum(ax),Sum(axx) form dd Group By a,b,c,d
    第二个表的结果Select a,b,c,d,Sum(bx),Sum(bxx) form cc Group By a,b,c,d
    现在我想要的记录结果就是:
    a,b,c,d,Sum(ax),Sum(axx),Sum(bx),Sum(bxx)
    就是在第一个记录结果的(每条记录)后面,根据a,b,c,d把Sum(bx),Sum(bxx)添加在一起,形成一条记录!
      

  5.   

    呵呵,我来回答LZ第一个问题:
    用存储过程吧:
    declare global temporary table 临时表
        (  
       a         char(4),
       b  char(6),
       c  char(4),
       d  char(4),
       ax        integer,
       bx        integer,
       axx       decimal(12,2),
       bxx  integer  
        )not logged with replace;数据类型你自己调整啊,我是从我的代码COPY出来的然后
    insert into session.临时表
    (
    a,b,c,d,ax,bx,axx,bxx
    )
    select
     a,b,c,d,Sum(ax),'0',Sum(axx),'0'
        from   第一个表dd 
    Group By a,b,c,d同理在把第二个表cc放进来
    然后把临时表一汇总就OK了,哈哈
      

  6.   

    有类似下面这样两个表:
    1.存储的是月工资和考核分数
    userid  monthpay monthscore2.存储的是季奖金和考核分数
    userid  quarterpay quarterscore现在,要把每个员工的工资总和,月平均考核分数,奖金总和,季平均考核分数统计出来sql语句怎么写?
    select a.userid,sum(a.monthpay),avg(a.monthscore),sum(b.quarterpay),avg(b.quarterscore) form ……
      

  7.   

    第一个问题解决了
    现在只剩第二个问题
    还有个小问题问一下,动态绑定到gridview的数据,怎么判断某列的数据为数字,并把该列的数据写成两位小数