tval:tva_id    tva_money    pos_id
pos:  pos_id    unit_id
unit: unit_id   unit_name结构应该都能看懂吧
希望得到每个单位下每台pos机的统计,查询结果希望是这样的:unit_id   unit_name    pos      money
   1         超市1      11       100
                        12       500
                        14       300
   2         超市2      21       100
                        25       400
                        26       700
   3         超市3      33       300       

解决方案 »

  1.   

    select a.unit_id,a.unit_name,b.pos_id,sum(tva_money) as money from unit a,pos b,tval c
    where a.unit_id =b.unit_id and b.pos_id=c.pos_id
    group by a.unit_id,a.unit_name,b.pos_id
      

  2.   

    如果想实现,unit_id  unit_name 如果下面一行与上面一行相同就为空,请参考代码create table ta(id int,name varchar2(10));
    insert into ta select 1,'a1' from dual;
    insert into ta select 1,'a2' from dual;
    insert into ta select 1,'a3' from dual;
    insert into ta select 1,'a4' from dual;
    insert into ta select 2,'a1' from dual;
    insert into ta select 2,'a2' from dual;
    insert into ta select 3,'a3' from dual;
    insert into ta select 3,'a4' from dual;
    commit;select a.*,case when lag(id)over(partition by id order by id) is null then a.id
    else null end as newid,a.name from ta a;
      

  3.   

    SELECT u.uuie_id ,u.unit_name,pt.pos,pt.money 
       FROM Unit u 
              RIGHT JOIN 
      (SELECT p.uuit_id,p.pos_id AS pos,t.tva_money AS money FROM Tval t ,Pos p WHERE t.pos_id = p.pos_id ORDER BY p.pos_id) pt 
            WHERE pt.uuit_id=u.uuit_id 
              ORDRE BY u.uuit_id;应该是这样!