<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3398983424813933792</id><updated>2024-09-11T22:50:23.494-07:00</updated><category term="Ms SQL Server"/><title type='text'>YibNgernYibTong</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://yibngernyibtong.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://yibngernyibtong.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09824284100752750870</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3398983424813933792.post-3714527845824313284</id><published>2012-06-21T18:53:00.001-07:00</published><updated>2012-06-21T18:53:27.378-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Ms SQL Server"/><title type='text'>Ranking functions in SQL Server 2005</title><content type='html'>&lt;br /&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
Ranking function เป็นของเล่นใหม่อีกชิ้นบน MS SQL Server 2005 ครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ปกติเวลาเราจะสร้างคำสั่ง Select ที่พ่วงฟิลด์ที่ไว้แสดงลำดับของ Record&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
หรือการจัดลำดับข้อมูลตามเงื่อนไข บน MS SQL version ก่อนหน้านี้เราต้องทำบน Temp table ใช่ไหมครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ซึ่งค่อนข้างเสียเวลาในการ coding มา version 2005 ก็ได้มี feature ใหม่ให้ได้เรียกใช้นั่นก็คือ Ranking function&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ซึ่งแต่ละ function อธิบายคร่าว ๆ ได้ดังนี้ครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
Row_number(): ตัวนี้เบสิคสุด ใช้สำหรับสร้างหมายเลขลำดับเพื่อกำกับแต่ละ record ที่ทำการ select ขึ้นมาแสดง&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;ตัวอย่าง :&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
Query:&amp;nbsp;select row_number() over(order by sal desc) rank, empno,ename,sal from emp&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://warozz.files.wordpress.com/2010/09/row_number.jpg?w=229&quot; style=&quot;border: 0px; cursor: pointer; display: block; height: 285px; line-height: inherit; margin: 0px auto 10px; max-width: 600px; padding: 0px; text-align: center; width: 229px;&quot; /&gt;&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;Rank(): ใช้สำหรับจัดอันดับตามเงื่อนไข ของฟิลด์ที่กำหนด เช่นการจัดอับดับคะแนนของบุคคลในฝ่าย คะแนนซ้ำก็จะอยู่ในลำดับเดียวกัน&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ตัวอย่าง&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
Query:&amp;nbsp;select rank() over(order by sal desc) rank, empno,sal,ename from emp&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://warozz.files.wordpress.com/2010/09/rank.jpg?w=230&quot; style=&quot;border: 0px; cursor: pointer; display: block; height: 288px; line-height: inherit; margin: 0px auto 10px; max-width: 600px; padding: 0px; text-align: center; width: 230px;&quot; /&gt;&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;Dense_rank() ทำงานเหมือน Rank() แต่จะเรียงลำดับข้อมูลไปเลย เมื่อมีคะแนนซ้ำกันจะไม่เว้นลำดับว่างไว้&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ตัวอย่าง&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
Query:&amp;nbsp;select dense_rank() over(order by sal desc) rank, empno,sal,ename from emp&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://warozz.files.wordpress.com/2010/09/dense_rank.jpg?w=229&quot; style=&quot;border: 0px; cursor: pointer; display: block; height: 284px; line-height: inherit; margin: 0px auto 10px; max-width: 600px; padding: 0px; text-align: center; width: 229px;&quot; /&gt;&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;Ntile(n): เป็นฟังก์ชั่นในการจัดกลุ่มของข้อมูลใน Table ออกเป็นจำนวน Record ที่เท่า ๆ กัน เหมาะสำหรับการแบ่งข้อมูลเป็น Page เพื่อแสดง&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ตัวอย่าง&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
Query:&amp;nbsp;select ntile(2) over(order by sal desc) rank, empno,sal,ename from emp&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://warozz.files.wordpress.com/2010/09/ntile.jpg?w=231&quot; style=&quot;border: 0px; cursor: pointer; display: block; height: 289px; line-height: inherit; margin: 0px auto 10px; max-width: 600px; padding: 0px; text-align: center; width: 231px;&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
หวังว่าคงมีประโยชน์สำหรับนักพัฒนาทุก ๆ ท่านสำหรับไปประยุกต์ใช้ในงานต่าง ๆ นะครับ&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yibngernyibtong.blogspot.com/feeds/3714527845824313284/comments/default' title='ส่งความคิดเห็น'/><link rel='replies' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/ranking-functions-in-sql-server-2005.html#comment-form' title='0 ความคิดเห็น'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/3714527845824313284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/3714527845824313284'/><link rel='alternate' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/ranking-functions-in-sql-server-2005.html' title='Ranking functions in SQL Server 2005'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09824284100752750870</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3398983424813933792.post-6785037608489524661</id><published>2012-06-21T18:51:00.002-07:00</published><updated>2012-06-21T18:53:54.227-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Ms SQL Server"/><title type='text'>การสร้าง Partitioning Table บน MS Sql Server 2005</title><content type='html'>&lt;br /&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ว่าด้วยเรื่องการจัดเก็บข้อมูลบนฐานข้อมูล MS-SQL Sever 2005 ในกรณีที่องค์กรของท่าน ๆ ทั้งหลาย มีข้อมูลเป็นจำนวนมากเก็บสุม ๆ ไว้เป็นเวลานาน ไฟล์บน Database ที่แบ่งแค่ Data file กับ Log file ตาม default ของการติดตั้ง โตขึ้น ๆ ทุกวัน จน Database Server เริ่มอืด&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
MS SQL 2005 มี feature ให้เล่น ซึ่งผมคิดว่ามีประโยชน์มาก ๆ นั่นก็คือการจัดสรรค์ข้อมูลลงไฟล์แบบกระจายโดยอัตโนมัติ หรือเรียกว่าการทำ Partioning ครับ หลักการโดยทั่วไปก็คือ การกระจาย Record ออกในแนวราบ ลง File group หลาย ๆ ไฟล์ ที่เราสร้างไว้รองรับโดยอัตโนมัติ รับรองว่าข้อมูลที่เคยมีมากมายมหาศาล ข้อมูลที่ Access แต่ละทีอืดมากมาย จะเร็วขึ้นทันตาเห็น โดยไม่ต้องทำการเพิ่มทรัพยากรด้านฮาร์ดแวร์&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ขั้นตอนและวิธ๊การ ขอยกตัวอย่างจาก Adventureworks แล้วกันนะครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– ทำการสร้าง partitioning functions.&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;– ตัวอย่างนี้จะทำการสร้าง Partition function แยกส่วนของข้อมูลเป็น 2 ช่วงคือ ข้อมูลก่อนและหลังวันที่ ’2005-01-01′&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
create partition function YearPF(datetime)&lt;br /&gt;
as range right for values (’20050101′);&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– ต่อมาต้องทำการเพิ่ม file group ลงไปใน database ใครถนัด GUI ก็ใช้ GUI ได้นะครับ case นี้ใช้เป็น Command Line ดังนี้&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;alter database AdventureWorks add filegroup YearFG1;&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;alter database AdventureWorks add filegroup YearFG2;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– ต่อมาก็ต้องทำการ add file ลงแต่ละ file group&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;alter database AdventureWorks add file (name = ‘YearF1′, filename = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdvWorksF1.ndf’) to filegroup YearFG1;&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;alter database AdventureWorks add file (name = ‘YearF2′, filename = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdvWorksF2.ndf’) to filegroup YearFG2;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– ต่อมาก็ต้องทำการเชื่อม file group เข้ากับ partition function โดยใช้&amp;nbsp;Partitioning Scheme&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;create partition scheme YearPS&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;as partition YearPF to (YearFG1, YearFG2)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– สุดท้ายก็ทำการสร้าง Table โดยอ้างอิงถึง Partition Scheme ที่เราทำการสร้างไว้ ข้อมูลก็จะถูกกระจายโดยอัตโนมัติ&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;create table PartitionedOrders&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;(&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;Id int not null identity(1,1),&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DueDate DateTime not null,&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;) on YearPS(DueDate) –ข้อมูลจะถูกแบ่งลงแต่ละ file group โดยใช้ field ‘DueDate’ เป็นเงื่อนไขในการกระจาย Data&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– ทดสอบการ insert ข้อมูล&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;insert into PartitionedOrders values(’2010-09-01′)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;insert into PartitionedOrders values(’1900-02-07′)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;insert into PartitionedOrders values(getdate())&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;insert into PartitionedOrders values(’1998-10-30′)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;insert into PartitionedOrders values(’2005-01-01′)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– ทีนี้เรามาตรวจสอบดูว่าข้อมูลแต่ละ Record ไปหล่นใส่ partition ไหนบ้างด้วยคำสั่งนี้เลยครับ&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;select *, $partition.YearPF(DueDate) from PartitionedOrders&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;– และสามารถดูจำนวน Partition ที่สร้างไว้ด้วยคำสั่งนี้ครับ&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;select * from sys.partitions where object_id = object_id(‘PartitionedOrders’)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: inherit; margin: 0px; padding: 0px;&quot;&gt;จากการใช้งานที่ผ่านมาสรุปว่าการ ทำ Partition Table มีประโยชน์มาก ๆ ครับ เหมาะกับฐานข้อมูลที่มี Data เยอะไฟล์บวม Partition Table ช่วยท่านได้ แต่ถ้าท่านมี Storage ที่สามารถแยก แต่ละ Partition ลงฮาร์ดดิสก์คนละก้อนได้ละก็ ประสิทธิภาพของ Database โดยรวมจะเพิ่มขึ้นแบบเห็น ๆ โดยไม่ต้องลงทุนเปลี่ยน Database Server ใหม่ครับ&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yibngernyibtong.blogspot.com/feeds/6785037608489524661/comments/default' title='ส่งความคิดเห็น'/><link rel='replies' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/partitioning-table-ms-sql-server-2005.html#comment-form' title='0 ความคิดเห็น'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/6785037608489524661'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/6785037608489524661'/><link rel='alternate' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/partitioning-table-ms-sql-server-2005.html' title='การสร้าง Partitioning Table บน MS Sql Server 2005'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09824284100752750870</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3398983424813933792.post-7274093005439327497</id><published>2012-06-21T18:50:00.002-07:00</published><updated>2012-06-21T18:50:28.344-07:00</updated><title type='text'>User Defined Function สำหรับแปลงตัวเลข เป็น อักษรภาษาไทย</title><content type='html'>&lt;br /&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ปกติจะเคยเห็น source code ที่ใช้สำหรับแปลงตัวเลขเป็นตัวอักษรกันเยอะแล้วนะครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
แต่ที่ออฟฟิศผมใช้หลากหลายภาษากันมาก มีอย่างเดียวที่ใช้เหมือนกันก็คือ Database ที่เป็น MS SQL&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ทำอย่างไรจะได้ฟังก์ชั่นกลาง &amp;nbsp;ๆ ที่สามารถเรียกใช้ได้ทุกภาษา ก็ต้องแปลงทุกอย่างให้อยู่ในภาษา SQL ล่ะครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ตัวอย่างนี้เป็นตัวอย่างหนึ่งที่มีการเรียกใช้บ่อย ๆ ในการพัฒนาระบบงานทั่วไปคงจะใช้กันบ่อยนะครับ&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
ปล. source ตัวนี้โมมาจากภาษา php&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
CREATE FUNCTION dbo.udf_Num2Thai (@Number1 Money)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;RETURNS VARCHAR(8000)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;AS BEGIN&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @number Numeric(38 , 0)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @decimal INT&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @loops INT&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @bigLoops INT&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @counter INT&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @bigCount INT&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @mod INT&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @numbersTable TABLE (number CHAR(1), word VARCHAR(10))&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @numbersDigit TABLE (number CHAR(1), word VARCHAR(10))&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @inputNumber VARCHAR(38)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @inputNumber1 VARCHAR(38)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @inputDecimal VARCHAR(2)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @charNumber CHAR(1)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @outputString VARCHAR(8000)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @outputString1 VARCHAR(8000)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @outputChar VARCHAR(10)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @outputChar1 VARCHAR(10)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;DECLARE @nextNumber CHAR(1)&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
IF @number1 = 0 RETURN ‘ศูนย์บาท’&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
– insert data for the numbers and words&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;INSERT INTO @NumbersTable&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT ‘ ‘, ” UNION ALL SELECT ’0′, ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’1′, ‘หนึ่ง’ UNION ALL SELECT ’2′, ‘สอง’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’3′, ‘สาม’ UNION ALL SELECT ’4′, ‘สี่’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’5′, ‘ห้า’ UNION ALL SELECT ’6′, ‘หก’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’7′, ‘เจ็ด’ UNION ALL SELECT ’8′, ‘แปด’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’9′, ‘เก้า’&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
INSERT INTO @NumbersDigit&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT ’1′, ” UNION ALL SELECT ’2′, ‘สิบ’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’3′, ‘ร้อย’ UNION ALL SELECT ’4′, ‘พัน’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;UNION ALL SELECT ’5′, ‘หมื่น’ UNION ALL SELECT ’6′, ‘แสน’&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
SET @number = FLOOR(@number1)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @decimal = FLOOR((@number1 – FLOOR(@number1))* 100)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputNumber1 = CONVERT(VARCHAR(38), @number)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputDecimal = CONVERT(VARCHAR(2), @decimal)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @bigLoops = FLOOR(LEN(@inputNumber1) / 6) + 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @mod = LEN(@inputNumber1) % 6&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @bigCount = 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @outputString = ”&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
WHILE @bigCount &amp;lt;= @bigLoops BEGIN&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @bigCount = 1 BEGIN&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputNumber = LEFT(@inputNumber1,@mod)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputNumber1 = RIGHT(@inputNumber1,LEN(@inputNumber1)-@mod)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;END&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;ELSE BEGIN&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputNumber = LEFT(@inputNumber1,6)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @bigCount &amp;lt; @bigLoops&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputNumber1 = RIGHT(@inputNumber1,LEN(@inputNumber1)-6)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;END&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
SET @outputString1 = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @counter = 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @loops = LEN(@inputNumber)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @nextNumber = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;WHILE 1 &amp;lt;= @loops BEGIN&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @charNumber = SUBSTRING(@inputNumber,@loops,1)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @nextNumber = SUBSTRING(@inputNumber,@loops-1,1)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT @outputChar = word FROM @NumbersTable&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;WHERE @charNumber = number&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT @outputChar1 = word FROM @NumbersDigit&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;WHERE CONVERT(CHAR(1),@counter) = number&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’1′ AND LEN(@inputNumber) &amp;gt; 1 AND @counter = 1 AND @nextNumber &amp;gt; ’0′&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @outputChar = ‘เอ็ด’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’1′ AND LEN(@inputNumber) &amp;gt;= 2 AND @counter = 2 SET @outputChar = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’2′ AND LEN(@inputNumber) &amp;gt;= 2 AND @counter = 2 SET @outputChar = ‘ยี่’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’0′ SET @outputChar1 = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT @outputString1 = @outputChar + @outputChar1 + @outputString1,&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;@counter = @counter + 1,&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;@loops = @loops – 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;END&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
IF @bigCount &amp;lt; @bigLoops&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @outputString1 &amp;lt;&amp;gt; ” SET @outputString = @outputString + @outputString1 + ‘ล้าน’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @bigCount &amp;gt;= @bigLoops SET @outputString = @outputString + @outputString1 + ‘บาท’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @bigCount = @bigCount + 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;END&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;– Decimal&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF LEN(@inputDecimal)= 1 SET @inputDecimal = ’0′ + @inputDecimal&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @inputNumber = @inputDecimal&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @counter = 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @loops = LEN(@inputNumber)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @outputString1 = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @nextNumber = SUBSTRING(@inputNumber,@loops-1,1)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;WHILE 1 &amp;lt;= @loops BEGIN&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @charNumber = SUBSTRING(@inputNumber,@loops,1)&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT @outputChar = word FROM @NumbersTable&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;WHERE @charNumber = number&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT @outputChar1 = word FROM @NumbersDigit&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;WHERE CONVERT(CHAR(1),@counter) = number&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’1′ AND LEN(@inputNumber) &amp;gt; 1 AND @counter = 1 AND @nextNumber &amp;gt; ’0′&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @outputChar = ‘เอ็ด’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’1′ AND LEN(@inputNumber) &amp;gt;= 2 AND @counter = 2 SET @outputChar = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’2′ AND LEN(@inputNumber) &amp;gt;= 2 AND @counter = 2 SET @outputChar = ‘ยี่’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @charNumber = ’0′ SET @outputChar1 = ”&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SELECT @outputString1 = @outputChar + @outputChar1 + @outputString1,&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;@counter = @counter + 1,&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;@loops = @loops – 1&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;END&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;IF @inputDecimal = ’00′&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;SET @outputString = @outPutString + ‘ถ้วน’&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;ELSE SET @outputString = @outputString + @outputString1 + ‘สตางค์’&lt;/div&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
RETURN @outputString — return the result&lt;br style=&quot;line-height: inherit; margin: 0px; padding: 0px;&quot; /&gt;END&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yibngernyibtong.blogspot.com/feeds/7274093005439327497/comments/default' title='ส่งความคิดเห็น'/><link rel='replies' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/user-defined-function.html#comment-form' title='1 ความคิดเห็น'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/7274093005439327497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/7274093005439327497'/><link rel='alternate' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/user-defined-function.html' title='User Defined Function สำหรับแปลงตัวเลข เป็น อักษรภาษาไทย'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09824284100752750870</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3398983424813933792.post-7912714023253582547</id><published>2012-06-21T18:49:00.002-07:00</published><updated>2012-06-21T18:49:35.553-07:00</updated><title type='text'>Using PIVOT and UNPIVOT สำหรับ MS SQL Server นะครับ</title><content type='html'>&lt;span style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px;&quot;&gt;PIVOT&amp;nbsp; แปลง่าย ๆ คือ Table ที่มีอยู่ จะเอาแต่ละ Row ของมันมาแปลงเป็นColumn ซะ&lt;/span&gt;&lt;br style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px;&quot;&gt;ตัวอย่าง&lt;/span&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #f4f5f7; background-image: url(http://s2.wp.com/wp-content/themes/pub/inove/img/blockquote.gif); background-position: 3px 3px; background-repeat: no-repeat no-repeat; border: 1px dashed rgb(204, 204, 204); color: #555555; font-size: 12px; line-height: 17px; margin-bottom: 5px; margin-top: 5px; padding: 8px 12px 8px 36px;&quot;&gt;SE AdventureWorks;
GO
SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4, [233] AS Emp5
FROM 
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt
ORDER BY VendorID

ผลลัพธ์ที่ไ้ด้จะเป็นแบบข้างล่างนี้

VendorID    Emp1        Emp2        Emp3        Emp4        Emp5
1           4           3           5           4           4
2           4           1           5           5           5
3           4           3           5           4           4
4           4           2           5           5           4
5           5           1           5           5           5


ส่วนในทางกลับกัน ถ้าข้อมูลที่มีอยู่เก็บอยู่ในรูปแบบ Column 
แล้ว User ไม่พอใจอยากให้มันแสดงเป็น Row แทนซะงั้น (ความพอดีไม่มีในโลก) 
ไม่เป็นไร ให้ Unpivot ช่วย ตามตัวอย่าง
--Create the table and insert values as portrayed in the previous example.
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int)
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4)
INSERT INTO pvt VALUES (2,4,1,5,5,5)
INSERT INTO pvt VALUES (3,4,3,5,4,4)
INSERT INTO pvt VALUES (4,4,2,5,5,4)
INSERT INTO pvt VALUES (5,5,1,5,5,5)
GO
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM 
   (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
   FROM pvt) p
UNPIVOT
   (Orders FOR Employee IN 
      (Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
GO

ผลลัพธ์ที่ได้ ก็จะออกมาตามที่ท่าน User ต้องการครับ&lt;/pre&gt;
&lt;div style=&quot;color: #555555; font-family: Verdana, &#39;BitStream vera Sans&#39;, Helvetica, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 10px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: #f4f5f7; background-image: url(http://s2.wp.com/wp-content/themes/pub/inove/img/blockquote.gif); background-position: 3px 3px; background-repeat: no-repeat no-repeat; border: 1px dashed rgb(204, 204, 204); color: #555555; font-size: 12px; line-height: 17px; margin-bottom: 5px; margin-top: 5px; padding: 8px 12px 8px 36px;&quot;&gt;VendorID   Employee   Orders
1      Emp1         4
1      Emp2         3
1      Emp3         5
1      Emp4         4
1      Emp5         4
2      Emp1         4
2      Emp2         1
2      Emp3         5
2      Emp4         5
2      Emp5         5
...&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://yibngernyibtong.blogspot.com/feeds/7912714023253582547/comments/default' title='ส่งความคิดเห็น'/><link rel='replies' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/using-pivot-and-unpivot-ms-sql-server.html#comment-form' title='0 ความคิดเห็น'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/7912714023253582547'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3398983424813933792/posts/default/7912714023253582547'/><link rel='alternate' type='text/html' href='http://yibngernyibtong.blogspot.com/2012/06/using-pivot-and-unpivot-ms-sql-server.html' title='Using PIVOT and UNPIVOT สำหรับ MS SQL Server นะครับ'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09824284100752750870</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>