For XML Path,forxmlpath

來源:互聯網
上載者:User

For XML Path,forxmlpath
關於For XML Path的用法 建立的表資料
先看下for xml path的初始用法:select ClassID,Sex,Age,Name from #PersonInfo for xml path('PersonInfo')  從名字就看可以看出用法,for xml  !!!
在使用過程中感覺通常是惡group by 一起用的。舉例如下:

--統計每個班中超過22歲的男同學資訊select ClassID,COUNT(1) as '超過22歲個數',(select Name+',' from #PersonInfo where ClassID=p.ClassID for xml path ('')) as '姓名集合'from #PersonInfo p where Sex='男' and Age>22 group by ClassID order by ClassIDselect ClassID,Age,COUNT(1) as '超過22歲個數',(select Name+',' from #PersonInfo where ClassID=p.ClassID and Age=p.Age for xml path ('')) as '姓名集合'from #PersonInfo p where Sex='男' and Age>22 group by ClassID,Age order by ClassIDselect ClassID,Age,COUNT(1) as '超過22歲個數',(select Name+',' from #PersonInfo where ClassID=p.ClassID and Age=p.Age for xml path ('')) as '姓名集合'from #PersonInfo p where Sex='男' group by ClassID,Age having Age>22 order by ClassID

結果如下:
感覺having的用處沒有想象中那麼大。只有在group by後使用,並且是彙總函式是用處才大,不然直接寫在where後面即可。補充一下:姓名集合最後面的逗號可以通過stuff函數去除掉。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.