{"id":3039,"date":"2019-04-05T22:05:00","date_gmt":"2019-04-05T20:05:00","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3039"},"modified":"2019-04-05T22:05:00","modified_gmt":"2019-04-05T20:05:00","slug":"changing-granularity","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3039","title":{"rendered":"Changing granularity"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Let us suppose, we have a table that has a granularity in two dimensions. Think of a customer and a product dimension. So each record has a combination of customer identification and product identification that is unique. Let us also assume that we would decrease that level of granularity into one dimension. Think of customer that should act as the identifying dimension. Each product identification should have its own column.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us provide an example. The start situation is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2019\/04\/Untitled-3-3.png\" alt=\"\" class=\"wp-image-3040\"\/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The target situation is:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2019\/04\/Untitled-3-1-1-1.png\" alt=\"\" class=\"wp-image-3041\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We have the code for the table here:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE pivot_test (<br>\n  id            NUMBER,<br>\n  customer_id   NUMBER,<br>\n  product_code  VARCHAR2(5),<br>\n  quantity      NUMBER<br>\n);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">INSERT INTO pivot_test VALUES (1, 1, 'A', 10);<br>\nINSERT INTO pivot_test VALUES (2, 1, 'B', 20);<br>\nINSERT INTO pivot_test VALUES (3, 1, 'C', 30);<br>\nINSERT INTO pivot_test VALUES (4, 2, 'A', 40);<br>\nINSERT INTO pivot_test VALUES (5, 2, 'C', 50);<br>\nINSERT INTO pivot_test VALUES (6, 3, 'A', 60);<br>\nINSERT INTO pivot_test VALUES (7, 3, 'B', 70);<br>\nINSERT INTO pivot_test VALUES (8, 3, 'C', 80);<br>\nINSERT INTO pivot_test VALUES (9, 3, 'D', 90);<br>\nINSERT INTO pivot_test VALUES (10, 4, 'A', 100);<br>\nCOMMIT;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There are two solutions for this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Solution 1 uses the pivot function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT \"CUSTOMER_ID\" , <br>\n    \"A_SUM_QUANTITY\" , <br>\n    \"B_SUM_QUANTITY\" , <br>\n    \"C_SUM_QUANTITY\"<br>\nFROM   (SELECT customer_id, product_code, quantity<br>\n        FROM   pivot_test)<br>\nPIVOT  (SUM(quantity) AS sum_quantity FOR (product_code) IN ('A' AS a, 'B' AS b, 'C' AS c))<br>\nORDER BY customer_id;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Solution 2 uses a decode function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customer_id,<br>\n       SUM(DECODE(product_code, 'A', quantity, 0)) AS a_sum_quantity,<br>\n       SUM(DECODE(product_code, 'B', quantity, 0)) AS b_sum_quantity,<br>\n       SUM(DECODE(product_code, 'C', quantity, 0)) AS c_sum_quantity<br>\nFROM   pivot_test<br>\nGROUP BY customer_id<br>\nORDER BY customer_id;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let us suppose, we have a table that has a granularity in two dimensions. Think of a customer and a product dimension. So each record has a combination of customer identification and product identification that is unique. Let us also assume that we would decrease that level of granularity into one dimension. Think of customer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3039","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3039"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3039\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3039"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}