{"id":324258,"date":"2021-06-03T09:00:37","date_gmt":"2021-06-03T09:00:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=324258"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=324258","title":{"rendered":"\u0412\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 \u0432 Google BigQuery (PIVOT)"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0421\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 Excel \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u043c\u043e\u0449\u043d\u044b\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0444\u043e\u0440\u043c\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0430\u0440\u0443 \u043a\u043b\u0438\u043a\u043e\u0432 \u043c\u044b\u0448\u043a\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u0442\u0447\u0451\u0442 \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u043c \u0437\u0430 \u0433\u043e\u0434, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e \u0431\u044b \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0431\u044b\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, \u0430 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u043c\u0435\u0441\u044f\u0446\u0430. \u041d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u043a\u0438\u0434\u0430\u0439 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u0432\u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u043b\u0435 \u043c\u0435\u0441\u044f\u0446, \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u043a \u043f\u043e\u043b\u0435 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u043e\u0440\u0433\u043e\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a, \u0430 \u0432 \u043f\u043e\u043b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0443\u043c\u043c\u0443 \u043f\u0440\u043e\u0434\u0430\u0436. \u041e\u0442\u0447\u0451\u0442 \u0433\u043e\u0442\u043e\u0432.<\/p>\n<p>\u0414\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u0438\u0445 \u043f\u043e\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432 SQL \u0431\u044b\u043b\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e, \u043d\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b Google BigQuery \u0431\u044b\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>PIVOT<\/code>, \u043e \u043d\u0451\u043c \u0438 \u043f\u043e\u0439\u0434\u0451\u0442 \u0440\u0435\u0447\u044c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d98\/b98\/5d5\/d98b985d578a41ac3856f2836774bd5e.png\" width=\"526\" height=\"394\"><figcaption><\/figcaption><\/figure>\n<h2>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<blockquote>\n<p><em>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442\u0435\u0441\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0430\u043c \u0431\u0443\u0434\u0443\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d\u044b \u043c\u043e\u0438 <\/em><a href=\"https:\/\/t.me\/R4marketing\" rel=\"noopener noreferrer nofollow\"><em>telegram<\/em><\/a><em> \u0438 <\/em><a href=\"https:\/\/www.youtube.com\/R4marketing?sub_confirmation=1\" rel=\"noopener noreferrer nofollow\"><em>youtube<\/em><\/a><em> \u043a\u0430\u043d\u0430\u043b. \u0411\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u044f\u0437\u044b\u043a\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f R. \u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c!<\/em><\/p>\n<\/blockquote>\n<ol>\n<li>\n<p><a href=\"#pivot_sintax\" rel=\"noopener noreferrer nofollow\">\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 PIVOT<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#dinlist\" rel=\"noopener noreferrer nofollow\">\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#dinlist\" rel=\"noopener noreferrer nofollow\">\u041e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f UNPIVOT<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#unpivot\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"pivot_sintax\" id=\"pivot_sintax\"><\/a><\/p>\n<h2>\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 PIVOT<\/h2>\n<p><code>PIVOT<\/code> \u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043f\u043e\u043b\u0435\u0439 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b. \u0422\u0435\u043c \u043a\u0442\u043e \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u043f\u043e\u043d\u044f\u0442\u0438\u0435\u043c Tidy Data, \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u0437 \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0448\u0438\u0440\u043e\u043a\u0438\u0439.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5aa\/99a\/3a9\/5aa99a3a9560723b7d231f443da231e0.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PIVOT\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PIVOT\" width=\"786\" height=\"308\"><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PIVOT<\/figcaption><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 Google BigQuery Standart SQL.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u043e\u0439 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">WITH sales_data AS (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129) SELECT * FROM sales_data<\/code><\/pre>\n<\/div>\n<\/details>\n<p><code>PIVOT<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u044c\u0441\u044f \u0447\u0430\u0441\u0442\u044c \u0431\u043b\u043e\u043a\u0430 <code>FROM<\/code> \u0438 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:<\/p>\n<pre><code class=\"sql\">FROM from_item[, ...] pivot_operator  pivot_operator:     PIVOT(         aggregate_function_call [as_alias][, ...]         FOR input_column         IN ( pivot_column [as_alias][, ...] )     ) [AS alias]  as_alias:     [AS] alias<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>PIVOT<\/code> \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0434\u0430\u0442\u044c 3 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430:<\/p>\n<ul>\n<li>\n<p><code>from_item<\/code> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u0438\u043b\u0438 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u0440\u0430\u0449\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p><code>aggregate_function_call<\/code> &#8212; \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u0436\u0438\u043c\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0438, \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u0441\u0436\u0438\u043c\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a \u0432 \u043e\u0434\u043d\u0443 \u044f\u0447\u0435\u0439\u043a\u0443. \u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043c\u0435\u0441\u044f\u0446\u0435 \u0438\u043c\u0435\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0441\u0443\u043c\u043c\u0443 \u043f\u0440\u043e\u0434\u0430\u0436 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043c\u0435\u0441\u044f\u0446\u0435. \u0422.\u0435. \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p><code>FOR<\/code> &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u0443\u044e \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<pre><code class=\"sql\">WITH sales_data AS (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129)  SELECT * FROM    (   -- #1 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c   SELECT * FROM sales_data   )      PIVOT(   -- #2 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435   SUM(sales) as sum_of   -- #3 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432   FOR month IN ('jan', 'feb', 'mar')   )<\/code><\/pre>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0432 \u0431\u043b\u043e\u043a\u0435 <code>FOR<\/code> \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441, \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438.<\/p>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/strong><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f40\/3f9\/ec6\/f403f9ec6be61c5ee6de5ae33e932fad.png\" alt=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 PIVOT\" title=\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 PIVOT\" width=\"386\" height=\"208\"><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 PIVOT<\/figcaption><\/figure>\n<p>\u041d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 <code>FOR<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0444\u0435\u0432\u0440\u0430\u043b\u044e \u0438 \u043c\u0430\u0440\u0442\u0443 \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0445.<\/p>\n<pre><code class=\"sql\">SELECT * FROM   (SELECT * FROM sales_data)   PIVOT(SUM(sales) as sum_of FOR month IN ('feb', 'mar'))<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/264\/949\/8c0\/2649498c0cf68cdf3f11ef51b1650a10.png\" width=\"362\" height=\"213\"><figcaption><\/figcaption><\/figure>\n<p><a class=\"anchor\" name=\"dinlist\" id=\"dinlist\"><\/a><\/p>\n<h2>\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h2>\n<p>\u0412\u044b\u0448\u0435 \u044f \u043f\u0438\u0441\u0430\u043b \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432 \u0431\u043b\u043e\u043a\u0435 <code>FOR<\/code> \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u0430\u043a\u043e\u0439 \u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043d\u0435 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0432 \u0440\u0443\u043a\u0430\u043c\u0438 \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0441\u0442\u044c, \u0438 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u0431\u044b\u043b \u043f\u043e\u0434\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u041b\u0430\u043a\u0430 \u041b\u0430\u043a\u0448\u043c\u0430\u043d\u0430\u043d\u0430 <a href=\"https:\/\/towardsdatascience.com\/pivot-in-bigquery-4eefde28b3be\" rel=\"noopener noreferrer nofollow\">&#171;PIVOT in BigQuery&#187;<\/a>.<\/p>\n<p>\u041b\u0430\u043a \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u0435\u043a\u0441\u0442 \u0434\u043b\u044f \u0431\u043b\u043e\u043a\u0430 <code>FOR IN<\/code>. \u0414\u0430\u043b\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439:<\/p>\n<pre><code class=\"sql\">DECLARE months STRING; -- \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e  -- \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u0438 -- \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 (\"jan\", \"feb\", \"mar\") SET months = (   SELECT      CONCAT('(\"', STRING_AGG(DISTINCT month, '\", \"'), '\")'),   FROM    (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129) );<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c5a\/ca0\/794\/c5aca0794928357baa86be00c1cace54.png\" width=\"500\" height=\"214\"><figcaption><\/figcaption><\/figure>\n<p>\u0422.\u0435. \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438 \u0432 \u0431\u043b\u043e\u043a\u0435 <code>FOR IN<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>months<\/code> \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"sql\">-- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e months \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 EXECUTE IMMEDIATE format(\"\"\" WITH sales_data as  (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129)  SELECT * FROM   (SELECT * FROM sales_data)   PIVOT(SUM(sales) as sum_of FOR month IN %s) \"\"\", months);<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>format()<\/code> \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>months<\/code> \u0432 \u0437\u0430\u043f\u0440\u043e\u0441, \u0432 \u0431\u043b\u043e\u043a <code>FOR IN<\/code>.<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">DECLARE months STRING; -- \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e  -- \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u0438 -- \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 (\"jan\", \"feb\", \"mar\") SET months = (   SELECT      CONCAT('(\"', STRING_AGG(DISTINCT month, '\", \"'), '\")'),   FROM    (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129) );  -- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e months \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 EXECUTE IMMEDIATE format(\"\"\" WITH sales_data as  (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129)  SELECT * FROM   (SELECT * FROM sales_data)   PIVOT(SUM(sales) as sum_of FOR month IN %s) \"\"\", months);<\/code><\/pre>\n<\/div>\n<\/details>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/strong><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6a0\/baa\/584\/6a0baa58420453f133f3c309365433a3.png\" width=\"470\" height=\"240\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432 \u0440\u0443\u0447\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u043c\u0435\u0441\u044f\u0446\u0430 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u041a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0432\u0441\u0435\u0433\u043e 3 \u043c\u0435\u0441\u044f\u0446\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u0451\u043c\u0430 \u043d\u0435 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0430 \u0435\u0441\u043b\u0438 \u0431\u044b \u0443 \u0432\u0430\u0441 \u0431\u044b\u043b\u0438 \u0442\u044b\u0441\u044f\u0447\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0432 \u0440\u0443\u0447\u043d\u0443\u044e \u0431\u044b\u043b \u0431\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u0435\u043d.<\/p>\n<p><a class=\"anchor\" name=\"unpivot\" id=\"unpivot\"><\/a><\/p>\n<h2>\u041e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f UNPIVOT<\/h2>\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 UNPIVOT \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u0445\u043e\u0442\u044c \u0432 \u0431\u0430\u0437\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u0440\u0430\u0439\u043d\u0435 \u0440\u0435\u0434\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u0448\u0438\u0440\u043e\u043a\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435. <\/p>\n<p><strong>\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 UNPIVOT:<\/strong><\/p>\n<pre><code class=\"sql\">FROM from_item[, ...] unpivot_operator  unpivot_operator:     UNPIVOT [ { INCLUDE NULLS | EXCLUDE NULLS } ] (         { single_column_unpivot | multi_column_unpivot }     ) [unpivot_alias]  single_column_unpivot:     values_column     FOR name_column     IN (columns_to_unpivot)  multi_column_unpivot:     values_column_set     FOR name_column     IN (column_sets_to_unpivot)  values_column_set:     (values_column[, ...])  columns_to_unpivot:     unpivot_column [row_value_alias][, ...]  column_sets_to_unpivot:     (unpivot_column [row_value_alias][, ...])  unpivot_alias and row_value_alias:     [AS] alias<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">WITH sales_data as  (   SELECT 'shop_1' as shop_name, 216 as jan, 555 as feb, 498 as mar UNION ALL   SELECT 'shop_2', 123, 517, 717 UNION ALL   SELECT 'shop_3', 267, 437, 435   )  SELECT * FROM sales_data<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0438\u0437 \u0448\u0438\u0440\u043e\u043a\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0434\u043b\u0438\u043d\u043d\u044b\u0439:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0e3\/1c7\/a28\/0e31c7a287270b6b9a653169d39719d6.png\" width=\"397\" height=\"252\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u044b \u0441\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0442.\u0435. \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043f\u044f\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0442\u0440\u0438, \u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u043c\u0435\u0441\u044f\u0446, \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u043e\u0434\u0430\u0436, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e <code>UNPIVOT<\/code>.<\/p>\n<pre><code class=\"sql\">WITH sales_data as  (   SELECT 'shop_1' as shop_name, 216 as jan, 555 as feb, 498 as mar UNION ALL   SELECT 'shop_2', 123, 517, 717 UNION ALL   SELECT 'shop_3', 267, 437, 435   )  SELECT * FROM sales_data UNPIVOT(sales FOR month IN (jan, feb, mar))<\/code><\/pre>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/strong><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ee1\/85b\/e0b\/ee185be0be8a496bf2d14c62926c7f29.png\" width=\"382\" height=\"383\"><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0435 <code>UNPIVOT<\/code> \u043c\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438:<\/p>\n<ul>\n<li>\n<p>\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 <code>sales<\/code><\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u0431\u0446\u0430 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043b\u0438 \u0438\u043c\u0435\u043d\u0430 \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 <code>month<\/code><\/p>\n<\/li>\n<li>\n<p>\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>(jan, feb, mar)<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"end\" id=\"end\"><\/a><\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>Standart SQL \u0432 Google BigQuery \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0438 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0434\u0430\u0451\u0442 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0443 \u0432\u0435\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u044c\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043b\u044e\u0431\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/netpeak.net\/ru\/blog\/kak-rabotat-s-okonnymi-funktsiyami-v-google-bigquery-podrobnoe-rukovodstvo\/\" rel=\"noopener noreferrer nofollow\">\u041e\u043a\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/532690\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f\u043c\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 UNNEST<\/a><\/p>\n<\/li>\n<li>\n<p><code>PIVOT<\/code> \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u043b \u0432\u0438\u0448\u0435\u043d\u043a\u043e\u0439 \u043d\u0430 \u0442\u043e\u0440\u0442\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442\u0435\u0441\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 \u043c\u043e\u0439 <a href=\"http:\/\/t.me\/R4marketing\" rel=\"noopener noreferrer nofollow\">Telegram<\/a> \u0438 <a href=\"https:\/\/www.youtube.com\/R4marketing\/?sub_confirmation=1\" rel=\"noopener noreferrer nofollow\">YouTube<\/a> \u043a\u0430\u043d\u0430\u043b\u044b.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/551368\/\"> https:\/\/habr.com\/ru\/post\/551368\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0421\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 Excel \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u043c\u043e\u0449\u043d\u044b\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0444\u043e\u0440\u043c\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0430\u0440\u0443 \u043a\u043b\u0438\u043a\u043e\u0432 \u043c\u044b\u0448\u043a\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u0442\u0447\u0451\u0442 \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u043c \u0437\u0430 \u0433\u043e\u0434, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e \u0431\u044b \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0431\u044b\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432, \u0430 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u043c\u0435\u0441\u044f\u0446\u0430. \u041d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u043a\u0438\u0434\u0430\u0439 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u0432\u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u043b\u0435 \u043c\u0435\u0441\u044f\u0446, \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u043a \u043f\u043e\u043b\u0435 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u043e\u0440\u0433\u043e\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a, \u0430 \u0432 \u043f\u043e\u043b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0443\u043c\u043c\u0443 \u043f\u0440\u043e\u0434\u0430\u0436. \u041e\u0442\u0447\u0451\u0442 \u0433\u043e\u0442\u043e\u0432.<\/p>\n<p>\u0414\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u0438\u0445 \u043f\u043e\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432 SQL \u0431\u044b\u043b\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e, \u043d\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b Google BigQuery \u0431\u044b\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>PIVOT<\/code>, \u043e \u043d\u0451\u043c \u0438 \u043f\u043e\u0439\u0434\u0451\u0442 \u0440\u0435\u0447\u044c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<blockquote>\n<p><em>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442\u0435\u0441\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0430\u043c \u0431\u0443\u0434\u0443\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d\u044b \u043c\u043e\u0438 <\/em><a href=\"https:\/\/t.me\/R4marketing\" rel=\"noopener noreferrer nofollow\"><em>telegram<\/em><\/a><em> \u0438 <\/em><a href=\"https:\/\/www.youtube.com\/R4marketing?sub_confirmation=1\" rel=\"noopener noreferrer nofollow\"><em>youtube<\/em><\/a><em> \u043a\u0430\u043d\u0430\u043b. \u0411\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u044f\u0437\u044b\u043a\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f R. \u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c!<\/em><\/p>\n<\/blockquote>\n<ol>\n<li>\n<p><a href=\"#pivot_sintax\" rel=\"noopener noreferrer nofollow\">\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 PIVOT<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#dinlist\" rel=\"noopener noreferrer nofollow\">\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#dinlist\" rel=\"noopener noreferrer nofollow\">\u041e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f UNPIVOT<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#unpivot\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"pivot_sintax\" id=\"pivot_sintax\"><\/a><\/p>\n<h2>\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 PIVOT<\/h2>\n<p><code>PIVOT<\/code> \u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043f\u043e\u043b\u0435\u0439 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b. \u0422\u0435\u043c \u043a\u0442\u043e \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u043f\u043e\u043d\u044f\u0442\u0438\u0435\u043c Tidy Data, \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u0437 \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0448\u0438\u0440\u043e\u043a\u0438\u0439.<\/p>\n<figure class=\"full-width\"><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PIVOT<\/figcaption><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 Google BigQuery Standart SQL.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u043e\u0439 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">WITH sales_data AS (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129) SELECT * FROM sales_data<\/code><\/pre>\n<\/div>\n<\/details>\n<p><code>PIVOT<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u044c\u0441\u044f \u0447\u0430\u0441\u0442\u044c \u0431\u043b\u043e\u043a\u0430 <code>FROM<\/code> \u0438 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:<\/p>\n<pre><code class=\"sql\">FROM from_item[, ...] pivot_operator  pivot_operator:     PIVOT(         aggregate_function_call [as_alias][, ...]         FOR input_column         IN ( pivot_column [as_alias][, ...] )     ) [AS alias]  as_alias:     [AS] alias<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>PIVOT<\/code> \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0434\u0430\u0442\u044c 3 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430:<\/p>\n<ul>\n<li>\n<p><code>from_item<\/code> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u0438\u043b\u0438 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u0440\u0430\u0449\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p><code>aggregate_function_call<\/code> &#8212; \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u0436\u0438\u043c\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0438, \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u0441\u0436\u0438\u043c\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a \u0432 \u043e\u0434\u043d\u0443 \u044f\u0447\u0435\u0439\u043a\u0443. \u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043c\u0435\u0441\u044f\u0446\u0435 \u0438\u043c\u0435\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0441\u0443\u043c\u043c\u0443 \u043f\u0440\u043e\u0434\u0430\u0436 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043c\u0435\u0441\u044f\u0446\u0435. \u0422.\u0435. \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p><code>FOR<\/code> &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u0443\u044e \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<pre><code class=\"sql\">WITH sales_data AS (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129)  SELECT * FROM    (   -- #1 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c   SELECT * FROM sales_data   )      PIVOT(   -- #2 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435   SUM(sales) as sum_of   -- #3 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432   FOR month IN ('jan', 'feb', 'mar')   )<\/code><\/pre>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0432 \u0431\u043b\u043e\u043a\u0435 <code>FOR<\/code> \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441, \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438.<\/p>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/strong><\/p>\n<figure class=\"\"><figcaption>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 PIVOT<\/figcaption><\/figure>\n<p>\u041d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 <code>FOR<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0444\u0435\u0432\u0440\u0430\u043b\u044e \u0438 \u043c\u0430\u0440\u0442\u0443 \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0445.<\/p>\n<pre><code class=\"sql\">SELECT * FROM   (SELECT * FROM sales_data)   PIVOT(SUM(sales) as sum_of FOR month IN ('feb', 'mar'))<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p><a class=\"anchor\" name=\"dinlist\" id=\"dinlist\"><\/a><\/p>\n<h2>\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h2>\n<p>\u0412\u044b\u0448\u0435 \u044f \u043f\u0438\u0441\u0430\u043b \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432 \u0431\u043b\u043e\u043a\u0435 <code>FOR<\/code> \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u0430\u043a\u043e\u0439 \u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043d\u0435 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0432 \u0440\u0443\u043a\u0430\u043c\u0438 \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0441\u0442\u044c, \u0438 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u0431\u044b\u043b \u043f\u043e\u0434\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u041b\u0430\u043a\u0430 \u041b\u0430\u043a\u0448\u043c\u0430\u043d\u0430\u043d\u0430 <a href=\"https:\/\/towardsdatascience.com\/pivot-in-bigquery-4eefde28b3be\" rel=\"noopener noreferrer nofollow\">&#171;PIVOT in BigQuery&#187;<\/a>.<\/p>\n<p>\u041b\u0430\u043a \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u0435\u043a\u0441\u0442 \u0434\u043b\u044f \u0431\u043b\u043e\u043a\u0430 <code>FOR IN<\/code>. \u0414\u0430\u043b\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439:<\/p>\n<pre><code class=\"sql\">DECLARE months STRING; -- \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e  -- \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u0438 -- \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 (\"jan\", \"feb\", \"mar\") SET months = (   SELECT      CONCAT('(\"', STRING_AGG(DISTINCT month, '\", \"'), '\")'),   FROM    (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129) );<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0422.\u0435. \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438 \u0432 \u0431\u043b\u043e\u043a\u0435 <code>FOR IN<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>months<\/code> \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"sql\">-- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e months \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 EXECUTE IMMEDIATE format(\"\"\" WITH sales_data as  (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129)  SELECT * FROM   (SELECT * FROM sales_data)   PIVOT(SUM(sales) as sum_of FOR month IN %s) \"\"\", months);<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>format()<\/code> \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>months<\/code> \u0432 \u0437\u0430\u043f\u0440\u043e\u0441, \u0432 \u0431\u043b\u043e\u043a <code>FOR IN<\/code>.<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">DECLARE months STRING; -- \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e  -- \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u0438 -- \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 (\"jan\", \"feb\", \"mar\") SET months = (   SELECT      CONCAT('(\"', STRING_AGG(DISTINCT month, '\", \"'), '\")'),   FROM    (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129) );  -- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e months \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 EXECUTE IMMEDIATE format(\"\"\" WITH sales_data as  (   SELECT 'shop_1' as shop_name, 'jan' as month, 105 as sales UNION ALL   SELECT 'shop_2', 'jan', 123 UNION ALL   SELECT 'shop_3', 'jan', 100 UNION ALL   SELECT 'shop_1', 'feb', 211 UNION ALL   SELECT 'shop_2', 'feb', 250 UNION ALL   SELECT 'shop_3', 'feb', 132 UNION ALL   SELECT 'shop_1', 'mar', 303 UNION ALL   SELECT 'shop_2', 'mar', 231 UNION ALL   SELECT 'shop_3', 'jan', 167 UNION ALL   SELECT 'shop_1', 'jan', 111 UNION ALL   SELECT 'shop_2', 'feb', 267 UNION ALL   SELECT 'shop_3', 'feb', 305 UNION ALL   SELECT 'shop_1', 'feb', 344 UNION ALL   SELECT 'shop_2', 'mar', 210 UNION ALL   SELECT 'shop_3', 'mar', 306 UNION ALL   SELECT 'shop_1', 'mar', 195 UNION ALL   SELECT 'shop_2', 'mar', 276 UNION ALL   SELECT 'shop_3', 'mar', 129)  SELECT * FROM   (SELECT * FROM sales_data)   PIVOT(SUM(sales) as sum_of FOR month IN %s) \"\"\", months);<\/code><\/pre>\n<\/div>\n<\/details>\n<p><strong>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/strong><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432 \u0440\u0443\u0447\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u043c\u0435\u0441\u044f\u0446\u0430 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u041a\u043e\u0433\u0434\u0430 \u0443 \u0432\u0430\u0441 \u0432\u0441\u0435\u0433\u043e 3 \u043c\u0435\u0441\u044f\u0446\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u0451\u043c\u0430 \u043d\u0435 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043e, \u0430 \u0435\u0441\u043b\u0438 \u0431\u044b \u0443 \u0432\u0430\u0441 \u0431\u044b\u043b\u0438 \u0442\u044b\u0441\u044f\u0447\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0432 \u0440\u0443\u0447\u043d\u0443\u044e \u0431\u044b\u043b \u0431\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u0435\u043d.<\/p>\n<p><a class=\"anchor\" name=\"unpivot\" id=\"unpivot\"><\/a><\/p>\n<h2>\u041e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f UNPIVOT<\/h2>\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 UNPIVOT \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u0445\u043e\u0442\u044c \u0432 \u0431\u0430\u0437\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u0440\u0430\u0439\u043d\u0435 \u0440\u0435\u0434\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u0448\u0438\u0440\u043e\u043a\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435. <\/p>\n<p><strong>\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 UNPIVOT:<\/strong><\/p>\n<pre><code class=\"sql\">FROM from_item[, ...] unpivot_operator  unpivot_operator:     UNPIVOT [ { INCLUDE NULLS | EXCLUDE NULLS } ] (         { single_column_unpivot | multi_column_unpivot }     ) [unpivot_alias]  single_column_unpivot:<\/code><\/pre>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-324258","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/324258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324258"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/324258\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}