三個sql語句

來源:互聯網
上載者:User

一、在c2中插入c1與c2的差集(c1-c2)。//sqlserver
insert c2 select * from c1 where not exists
  (select * from c2 where c2.id=c1.id)

二、建立表b,使其表結構與表資料都來自於表a //sqlserver
 select * into b from a

 

三、在樹形結構中逐級向上匯總:

 資料表nodes,其中path欄位表明了在樹形結構中該結點的路徑, 如結點n的路徑"1032"表示,結點n是根結點1的第0個子結點的第3個子結點的第2個子結點(顯然,根結點是n的父親的爺爺)。
 leaf欄位指出該結點是否葉子結點,acoumt指出了葉結點的數值。
 建表語句如下:
create table nodes (path varchar(20),leaf int,amount int );
insert into nodes values('1001',0,0);
insert into nodes values('10011001',0,0);
insert into nodes values('100110011001',1,50);
insert into nodes values('1002',0,0);
insert into nodes values('10021001',0,0);
insert into nodes values('100210011001',1,50);
insert into nodes values('1003',0,0);
insert into nodes values('10031001',0,0);
insert into nodes values('10011002',0,0);
insert into nodes values('100110021001',1,25);
select * from nodes order by path;
 要求寫一sql語句統計出各結點所下轄的葉子結點所有數值之和。
--mssql語句如下:
select T.path,amount
 =(select sum(amount) from nodes where path like T.path+'%' and leaf=1)
 from nodes T;

--mysql語句如下:
select T.path,amount
       =(select sum(amount) from  nodes where path like concat(T.path,'%') and leaf=1 )
   from nodes T;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.