﻿
{"id":10729,"date":"2019-04-12T11:28:22","date_gmt":"2019-04-12T11:28:22","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=10729"},"modified":"2019-04-12T11:28:22","modified_gmt":"2019-04-12T11:28:22","slug":"sql-serverda-unpivot-table-kullanimi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/sql-serverda-unpivot-table-kullanimi\/","title":{"rendered":"SQL Server&#8217;da UnPivot Table Kullan\u0131m\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>\u00d6nceki yaz\u0131lar\u0131mdan <a href=\"https:\/\/www.gencayyildiz.com\/blog\/sql-serverda-pivot-table-kullanimi\/\" rel=\"noopener noreferrer\" target=\"_blank\">SQL Server\u2019da Pivot Table Kullan\u0131m\u0131<\/a> ba\u015fl\u0131kl\u0131 i\u00e7eri\u011fimde SQL Server&#8217;da veriler \u00fczerinden anlam ve okunabilirlik a\u00e7\u0131s\u0131ndan g\u00fc\u00e7l\u00fc rapor sunabilmemizi sa\u011flayan Pivot Table&#8217;\u0131n nas\u0131l kullan\u0131ld\u0131\u011f\u0131n\u0131 incelemi\u015ftik. Bu i\u00e7eri\u011fimizde ise Pivot neticesinde sonu\u00e7 d\u00f6nen sorgular\u0131m\u0131z\u0131n kolonlar\u0131n\u0131 ve o kolonlara kar\u015f\u0131l\u0131k gelen s\u00fct\u00fcnlar\u0131 dikey olarak birle\u015ftirmeyi ve sonucu iki kolona indirgeyerek normal tablo format\u0131nda d\u00fczenlemeyi sa\u011flayan UnPivot Table&#8217;\u0131n kullan\u0131m\u0131n\u0131 inceleyece\u011fiz.<\/p>\n<p>Bir sorguyu UnPivot Table ile normal tabloya getirebilmek i\u00e7in a\u015fa\u011f\u0131daki prototipte oldu\u011fu gibi \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirilecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nSELECT * FROM (\r\n\tSELECT Kolon1, Kolon2, Kolon3, Kolon4 FROM Table1 -- Pivot Table\r\n) Pivot_Table\r\nUNPIVOT\r\n(\r\n\tX FOR Y IN(Kolon1, Kolon2, Kolon3, Kolon4)\r\n) UNPivot_table\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funa dikkat edilirse e\u011fer subquery olan ve netice olarak pivot table d\u00f6nen sorgudaki her bir kolon UnPivot fonksiyonu i\u00e7erisinde IN fonksiyonu taraf\u0131ndan de\u011fer olarak belirtilmi\u015ftir. \u0130\u015fte bu de\u011ferlerin bulundu\u011fu kolon ad\u0131 &#8220;Y&#8221; olarak isimlendirilmi\u015ftir. T\u00fcm bunlara kar\u015f\u0131l\u0131k gelecek olan denk sat\u0131rlardaki de\u011ferler ise &#8220;X&#8221; kolonu alt\u0131nda dikey bir \u015fekilde ilgili kolonlar\u0131n kar\u015f\u0131lar\u0131na yerle\u015ftirilecektir.<\/p>\n<p>Anlayaca\u011f\u0131n\u0131z; Pivot t\u00fcrevse, UnPivot o t\u00fcrevin integralidir.<\/p>\n<p>\u015eimdi North-wind veritaban\u0131 \u00fczerinde a\u015fa\u011f\u0131daki gibi elde edilmi\u015f bir Pivot table&#8217;\u0131 ele alarak \u00f6rneklendirme yapal\u0131m.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131.png\" alt=\"SQL Server&#039;da UnPivot Table Kullan\u0131m\u0131\" width=\"626\" height=\"216\" class=\"aligncenter size-full wp-image-10737\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131.png 626w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-300x104.png 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/a><br \/>\nYukar\u0131daki g\u00f6rseli incelerseniz e\u011fer mavi ile \u00e7izilmi\u015f kolonlardaki verileri dikey eksende s\u0131ralataca\u011f\u0131z ve yanlar\u0131na k\u0131rm\u0131z\u0131 ile \u00e7izilmi\u015f sat\u0131rdaki de\u011ferleri hizal\u0131 bir \u015fekilde yerle\u015ftirece\u011fiz.<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-1.png\" alt=\"SQL Server&#039;da UnPivot Table Kullan\u0131m\u0131\" width=\"753\" height=\"401\" class=\"aligncenter size-full wp-image-10741\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-1.png 753w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-1-300x160.png 300w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/a><br \/>\nG\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi bir \u00f6nceki vermi\u015f oldu\u011fum sorguyu burada subquery \u015feklinde de\u011ferlendirerek UnPivot fonksiyonunu \u00fczerinde uygulam\u0131\u015f olduk.<\/p>\n<p>E\u011fer ki bu tarz bir yap\u0131lanmada daha okunabilir bir \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirmek istiyorsan\u0131z a\u015fa\u011f\u0131daki gibi &#8220;WITH AS&#8221; kal\u0131b\u0131ylada \u00e7al\u0131\u015fabilirsiniz.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-2.png\" alt=\"SQL Server&#039;da UnPivot Table Kullan\u0131m\u0131\" width=\"739\" height=\"467\" class=\"aligncenter size-full wp-image-10745\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-2.png 739w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/04\/SQL-Serverda-UnPivot-Table-Kullan\u0131m\u0131-2-300x190.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/a><\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, \u00d6nceki yaz\u0131lar\u0131mdan SQL Server\u2019da Pivot Table Kullan\u0131m\u0131 ba\u015fl\u0131kl\u0131 i\u00e7eri\u011fimde SQL Server&#8217;da veriler \u00fczerinden anlam ve okunabilirlik a\u00e7\u0131s\u0131ndan g\u00fc\u00e7l\u00fc rapor sunabilmemizi sa\u011flayan Pivot Table&#8217;\u0131n nas\u0131l kullan\u0131ld\u0131\u011f\u0131n\u0131 incelemi\u015ftik. Bu i\u00e7eri\u011fimizde ise Pivot neticesinde sonu\u00e7 d\u00f6nen&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":10741,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1082,1412],"tags":[2270,245,2817,2816],"class_list":["post-10729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server","category-sql-server-2016","tag-pivot-table","tag-sql-server","tag-sql-unpivot-table","tag-unpivot-table"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=10729"}],"version-history":[{"count":14,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10729\/revisions"}],"predecessor-version":[{"id":10746,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/10729\/revisions\/10746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/10741"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=10729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=10729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=10729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}