{"id":365377,"date":"2024-05-21T02:36:51","date_gmt":"2024-05-21T02:36:51","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=365377"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=365377","title":{"rendered":"<span>GROUPING SETS<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438\u00a0<a href=\"https:\/\/mssqlforever.blogspot.com\/2023\/04\/with-rollup.html\" rel=\"noopener noreferrer nofollow\">WITH ROLLUP<\/a>\u00a0\u0438\u00a0<a href=\"https:\/\/mssqlforever.blogspot.com\/2023\/12\/with-cub.html\" rel=\"noopener noreferrer nofollow\">WITH CUB<\/a>E.\u00a0\u0412 SQL Server 2008 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u043d\u043e\u0432\u044b\u0439, \u0431\u043e\u043b\u0435\u0435 \u043c\u043e\u0449\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 <a href=\"https:\/\/www.iso.org\/standard\/76583.html\" rel=\"noopener noreferrer nofollow\">ANSI SQL 2006<\/a>.\u00a0\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043d\u043e \u043e\u0431 \u044d\u0442\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b WITH ROLLUP \u0438 WITH CUBE, \u0435\u0441\u043b\u0438 \u043c\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0438\u0445 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ANSI-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441.\u00a0\u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0443 \u0436\u0435 \u0441\u0445\u0435\u043c\u0443 \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0447\u0442\u043e \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"pgsql\">CREATE TABLE Sales (EmpId INT, Yr INT, Sales MONEY) INSERT Sales VALUES(1, 2005, 12000) INSERT Sales VALUES(1, 2006, 18000) INSERT Sales VALUES(1, 2007, 25000) INSERT Sales VALUES(2, 2005, 15000) INSERT Sales VALUES(2, 2006, 6000) INSERT Sales VALUES(3, 2006, 20000) INSERT Sales VALUES(3, 2007, 24000)  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY EmpId, Yr WITH ROLLUP  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY EmpId, Yr WITH CUBE<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u044d\u0442\u0438 \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY ROLLUP(EmpId, Yr)  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY CUBE(EmpId, Yr)<\/code><\/pre>\n<p>\u042d\u0442\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c WITH ROLLUP \u0438 WITH CUBE, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435 \u043f\u043b\u0430\u043d\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.\u00a0\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e ANSI-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 ROLLUP \u0438 CUBE \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 100 \u0438 \u0432\u044b\u0448\u0435. \u0411\u043e\u043b\u0435\u0435 \u043e\u0431\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 GROUPING SETS, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445, \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438 \u0434\u043b\u044f \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043c\u0435\u043d\u044c\u0448\u0435 100.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/ru-ru\/sql\/t-sql\/queries\/select-group-by-transact-sql?view=sql-server-ver16#group-by-grouping-sets--\" rel=\"noopener noreferrer nofollow\">ANSI-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 GROUPING SETS<\/a> \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0449\u043d\u0435\u0435, \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0442\u043e\u0447\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c.\u00a0\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0438\u0436\u0435, \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u0447\u0435\u0442\u044b\u0440\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\" rowspan=\"2\" data-colwidth=\"76,61\" width=\"76\">\n<p align=\"left\">\u00a0<\/p>\n<\/td>\n<td colspan=\"4\">\n<p align=\"left\"><strong>Yr<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><strong>2005<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>2006<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>2007<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>ALL<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"4\" data-colwidth=\"76\" width=\"76\">\n<p align=\"left\"><strong>EmpId<\/strong><\/p>\n<\/td>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>1<\/strong><\/p>\n<\/td>\n<td colspan=\"3\" rowspan=\"3\">\n<p align=\"left\">GROUP BY (EmpId,   Yr)<\/p>\n<\/td>\n<td rowspan=\"3\">\n<p align=\"left\">GROUP BY (EmpId)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>2<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>3<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>ALL<\/strong><\/p>\n<\/td>\n<td colspan=\"3\">\n<p align=\"left\">GROUP BY (Yr)<\/p>\n<\/td>\n<td>\n<p align=\"left\">GROUP BY ()<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>WITH ROLLUP \u0438 WITH CUBE \u2014 \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u00ab\u043e\u0431\u0451\u0440\u0442\u043a\u0430\u00bb \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f GROUPING SETS.\u00a0\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 WITH ROLLUP \u0447\u0435\u0440\u0435\u0437 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043f\u043e \u0433\u043e\u0441\u0442\u0443:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId), ())<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 1           2006        18000.00 1           2007        25000.00 1           NULL        55000.00 2           2005        15000.00 2           2006        6000.00 2           NULL        21000.00 3           2006        20000.00 3           2007        24000.00 3           NULL        44000.00 NULL        NULL        120000.00<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u044f\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 SQL Server \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u043f\u043e \u0433\u043e\u0434\u0430\u043c.\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a () \u0431\u0435\u0437 GROUP BY \u043f\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443.\u00a0\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 WITH CUBE, \u0443\u043a\u0430\u0437\u0430\u0432 SQL Server \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId), (Yr), ())<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 2           2005        15000.00 NULL        2005        27000.00 1           2006        18000.00 2           2006        6000.00 3           2006        20000.00 NULL        2006        44000.00 1           2007        25000.00 3           2007        24000.00 NULL        2007        49000.00 NULL        NULL        120000.00 1           NULL        55000.00 2           NULL        21000.00 3           NULL        44000.00<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c GROUPING SETS \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c, \u043d\u043e \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044f \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u0437\u0430 \u0432\u0441\u0435 \u0433\u043e\u0434\u0430:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId))<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 1           2006        18000.00 1           2007        25000.00 1           NULL        55000.00 2           2005        15000.00 2           2006        6000.00 2           NULL        21000.00 3           2006        20000.00 3           2007        24000.00 3           NULL        44000.00<\/code><\/pre>\n<p>\u041f\u043e\u0445\u043e\u0436\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0440\u043e\u0432\u043d\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0437\u0430 \u0432\u0441\u0435 \u0433\u043e\u0434\u0430, \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044f \u043a\u0430\u043a\u0438\u0435-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), ())<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 1           2006        18000.00 1           2007        25000.00 2           2005        15000.00 2           2006        6000.00 3           2006        20000.00 3           2007        24000.00 NULL        NULL        120000.00<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432 \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f\u043c.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0433\u043e\u0434\u0430\u043c:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId), (Yr))<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- NULL        2005        27000.00 NULL        2006        44000.00 NULL        2007        49000.00 1           NULL        55000.00 2           NULL        21000.00 3           NULL        44000.00<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c GROUPING SETS (EmpId, Yr) \u0431\u0435\u0437 \u0432\u0442\u043e\u0440\u044b\u0445 \u043a\u0440\u0443\u0433\u043b\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a, \u043d\u043e \u0442\u0430\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u0433\u043e\u0434\u043e\u043c \u0432 \u043a\u0440\u0443\u0433\u043b\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0446\u0435\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043b\u043e\u0432\u0438\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr))<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 2           2005        15000.00 1           2006        18000.00 2           2006        6000.00 3           2006        20000.00 1           2007        25000.00 3           2007        24000.00<\/code><\/pre>\n<p>\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0436\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u0434\u043b\u044f \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 GROUPING SETS. \u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u043b\u044e\u0431\u044b\u043c \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430\u043c\u0438, \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 SELECT \u0438 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439, \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0433\u0434\u0435-\u0442\u043e \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 GROUP BY.\u00a0\u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId), ())<\/code><\/pre>\n<blockquote>\n<p>Msg 8120, Level 16, State 1, Line 1<br \/>Column &#8216;Sales.Yr&#8217; is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e GROUPING SET \u0438 \u0441\u0430\u043c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432 GROUPING SET \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.\u00a0\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043e\u0431\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043d\u0438\u0436\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 CUBE, \u0445\u043e\u0442\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0432 \u0432\u044b\u0432\u043e\u0434\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS ((EmpId, Yr), (EmpId), (Yr), ())  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS ((), (Yr), (EmpId), (Yr, EmpId))<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u043c \u0432\u044b\u0432\u043e\u0434\u0435 \u0437\u0430\u0434\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u044f\u0432\u043d\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c ORDER BY.<\/p>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 GROUPING SETS, \u043c\u043e\u0436\u043d\u043e \u0432\u043b\u043e\u0436\u0438\u0442\u044c CUBE \u0438 ROLLUP \u0432\u043d\u0443\u0442\u0440\u044c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f GROUPING SETS.\u00a0\u0422\u0430\u043a\u0438\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0431\u044b\u0432\u0430\u044e\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u044b, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0441\u0445\u0435\u043c\u0435 \u0431\u043e\u043b\u0435\u0435 \u0442\u0440\u0435\u0445 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0439.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0440\u043e\u0434\u0430\u0436 \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u0441 \u043c\u0435\u0441\u044f\u0446\u0435\u043c:<\/p>\n<pre><code class=\"pgsql\">CREATE TABLE Sales (EmpId INT, Month INT, Yr INT, Sales MONEY)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430 \u043f\u043e \u043c\u0435\u0441\u044f\u0446\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u043f\u043e \u0433\u043e\u0434\u0430\u043c \u0438 \u0432 \u0446\u0435\u043b\u043e\u043c.\u00a0\u041c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u044d\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044f\u0432\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 GROUPING SETS:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Month, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr, Month), (EmpId, Yr), (EmpId))<\/code><\/pre>\n<p>\u0418\u043b\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ROLLUP, \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Month, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS( ( EmpId, ROLLUP(Yr, Month)) )<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0440\u0443\u0433\u043b\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0440\u0435\u0448\u0430\u044e\u0449\u0435\u0435<br \/>\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.\u00a0\u0415\u0441\u043b\u0438 \u043c\u044b \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 \u0441\u043a\u043e\u0431\u043e\u043a \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0441\u043c\u044b\u0441\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f, \u0438 \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c ROLLUP \u0433\u043e\u0434\u0430 \u0438 \u043c\u0435\u0441\u044f\u0446\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/786554\/\"> https:\/\/habr.com\/ru\/articles\/786554\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438\u00a0<a href=\"https:\/\/mssqlforever.blogspot.com\/2023\/04\/with-rollup.html\" rel=\"noopener noreferrer nofollow\">WITH ROLLUP<\/a>\u00a0\u0438\u00a0<a href=\"https:\/\/mssqlforever.blogspot.com\/2023\/12\/with-cub.html\" rel=\"noopener noreferrer nofollow\">WITH CUB<\/a>E.\u00a0\u0412 SQL Server 2008 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u043d\u043e\u0432\u044b\u0439, \u0431\u043e\u043b\u0435\u0435 \u043c\u043e\u0449\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 <a href=\"https:\/\/www.iso.org\/standard\/76583.html\" rel=\"noopener noreferrer nofollow\">ANSI SQL 2006<\/a>.\u00a0\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043d\u043e \u043e\u0431 \u044d\u0442\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b WITH ROLLUP \u0438 WITH CUBE, \u0435\u0441\u043b\u0438 \u043c\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0438\u0445 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ANSI-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441.\u00a0\u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0443 \u0436\u0435 \u0441\u0445\u0435\u043c\u0443 \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0447\u0442\u043e \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0430\u043c \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"pgsql\">CREATE TABLE Sales (EmpId INT, Yr INT, Sales MONEY) INSERT Sales VALUES(1, 2005, 12000) INSERT Sales VALUES(1, 2006, 18000) INSERT Sales VALUES(1, 2007, 25000) INSERT Sales VALUES(2, 2005, 15000) INSERT Sales VALUES(2, 2006, 6000) INSERT Sales VALUES(3, 2006, 20000) INSERT Sales VALUES(3, 2007, 24000)  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY EmpId, Yr WITH ROLLUP  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY EmpId, Yr WITH CUBE<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u044d\u0442\u0438 \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY ROLLUP(EmpId, Yr)  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY CUBE(EmpId, Yr)<\/code><\/pre>\n<p>\u042d\u0442\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c WITH ROLLUP \u0438 WITH CUBE, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435 \u043f\u043b\u0430\u043d\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.\u00a0\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e ANSI-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 ROLLUP \u0438 CUBE \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 100 \u0438 \u0432\u044b\u0448\u0435. \u0411\u043e\u043b\u0435\u0435 \u043e\u0431\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 GROUPING SETS, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445, \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438 \u0434\u043b\u044f \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043c\u0435\u043d\u044c\u0448\u0435 100.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/ru-ru\/sql\/t-sql\/queries\/select-group-by-transact-sql?view=sql-server-ver16#group-by-grouping-sets--\" rel=\"noopener noreferrer nofollow\">ANSI-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 GROUPING SETS<\/a> \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0449\u043d\u0435\u0435, \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0442\u043e\u0447\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c.\u00a0\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0438\u0436\u0435, \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u0447\u0435\u0442\u044b\u0440\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\" rowspan=\"2\" data-colwidth=\"76,61\" width=\"76\">\n<p align=\"left\">\u00a0<\/p>\n<\/td>\n<td colspan=\"4\">\n<p align=\"left\"><strong>Yr<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><strong>2005<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>2006<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>2007<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\"><strong>ALL<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"4\" data-colwidth=\"76\" width=\"76\">\n<p align=\"left\"><strong>EmpId<\/strong><\/p>\n<\/td>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>1<\/strong><\/p>\n<\/td>\n<td colspan=\"3\" rowspan=\"3\">\n<p align=\"left\">GROUP BY (EmpId,   Yr)<\/p>\n<\/td>\n<td rowspan=\"3\">\n<p align=\"left\">GROUP BY (EmpId)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>2<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>3<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"61\" width=\"61\">\n<p align=\"left\"><strong>ALL<\/strong><\/p>\n<\/td>\n<td colspan=\"3\">\n<p align=\"left\">GROUP BY (Yr)<\/p>\n<\/td>\n<td>\n<p align=\"left\">GROUP BY ()<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>WITH ROLLUP \u0438 WITH CUBE \u2014 \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u00ab\u043e\u0431\u0451\u0440\u0442\u043a\u0430\u00bb \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f GROUPING SETS.\u00a0\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 WITH ROLLUP \u0447\u0435\u0440\u0435\u0437 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043f\u043e \u0433\u043e\u0441\u0442\u0443:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId), ())<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 1           2006        18000.00 1           2007        25000.00 1           NULL        55000.00 2           2005        15000.00 2           2006        6000.00 2           NULL        21000.00 3           2006        20000.00 3           2007        24000.00 3           NULL        44000.00 NULL        NULL        120000.00<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u044f\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 SQL Server \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u043f\u043e \u0433\u043e\u0434\u0430\u043c.\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a () \u0431\u0435\u0437 GROUP BY \u043f\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443.\u00a0\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 WITH CUBE, \u0443\u043a\u0430\u0437\u0430\u0432 SQL Server \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId), (Yr), ())<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 2           2005        15000.00 NULL        2005        27000.00 1           2006        18000.00 2           2006        6000.00 3           2006        20000.00 NULL        2006        44000.00 1           2007        25000.00 3           2007        24000.00 NULL        2007        49000.00 NULL        NULL        120000.00 1           NULL        55000.00 2           NULL        21000.00 3           NULL        44000.00<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c GROUPING SETS \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c, \u043d\u043e \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044f \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u0437\u0430 \u0432\u0441\u0435 \u0433\u043e\u0434\u0430:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId))<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 1           2006        18000.00 1           2007        25000.00 1           NULL        55000.00 2           2005        15000.00 2           2006        6000.00 2           NULL        21000.00 3           2006        20000.00 3           2007        24000.00 3           NULL        44000.00<\/code><\/pre>\n<p>\u041f\u043e\u0445\u043e\u0436\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0440\u043e\u0432\u043d\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0437\u0430 \u0432\u0441\u0435 \u0433\u043e\u0434\u0430, \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044f \u043a\u0430\u043a\u0438\u0435-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), ())<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 1           2006        18000.00 1           2007        25000.00 2           2005        15000.00 2           2006        6000.00 3           2006        20000.00 3           2007        24000.00 NULL        NULL        120000.00<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432 \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f\u043c.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e \u0433\u043e\u0434\u0430\u043c:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId), (Yr))<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- NULL        2005        27000.00 NULL        2006        44000.00 NULL        2007        49000.00 1           NULL        55000.00 2           NULL        21000.00 3           NULL        44000.00<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c GROUPING SETS (EmpId, Yr) \u0431\u0435\u0437 \u0432\u0442\u043e\u0440\u044b\u0445 \u043a\u0440\u0443\u0433\u043b\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a, \u043d\u043e \u0442\u0430\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u0433\u043e\u0434\u043e\u043c \u0432 \u043a\u0440\u0443\u0433\u043b\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0446\u0435\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043b\u043e\u0432\u0438\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr))<\/code><\/pre>\n<pre><code>EmpId       Yr          Sales ----------- ----------- --------------------- 1           2005        12000.00 2           2005        15000.00 1           2006        18000.00 2           2006        6000.00 3           2006        20000.00 1           2007        25000.00 3           2007        24000.00<\/code><\/pre>\n<p>\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0436\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u0434\u043b\u044f \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 GROUPING SETS. \u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u043b\u044e\u0431\u044b\u043c \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430\u043c\u0438, \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 SELECT \u0438 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439, \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0433\u0434\u0435-\u0442\u043e \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 GROUP BY.\u00a0\u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId), ())<\/code><\/pre>\n<blockquote>\n<p>Msg 8120, Level 16, State 1, Line 1<br \/>Column &#8216;Sales.Yr&#8217; is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e GROUPING SET \u0438 \u0441\u0430\u043c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432 GROUPING SET \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.\u00a0\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043e\u0431\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043d\u0438\u0436\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 CUBE, \u0445\u043e\u0442\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0432 \u0432\u044b\u0432\u043e\u0434\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS ((EmpId, Yr), (EmpId), (Yr), ())  SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS ((), (Yr), (EmpId), (Yr, EmpId))<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u043c \u0432\u044b\u0432\u043e\u0434\u0435 \u0437\u0430\u0434\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u044f\u0432\u043d\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c ORDER BY.<\/p>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 GROUPING SETS, \u043c\u043e\u0436\u043d\u043e \u0432\u043b\u043e\u0436\u0438\u0442\u044c CUBE \u0438 ROLLUP \u0432\u043d\u0443\u0442\u0440\u044c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f GROUPING SETS.\u00a0\u0422\u0430\u043a\u0438\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0431\u044b\u0432\u0430\u044e\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u044b, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0441\u0445\u0435\u043c\u0435 \u0431\u043e\u043b\u0435\u0435 \u0442\u0440\u0435\u0445 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0439.\u00a0\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0440\u043e\u0434\u0430\u0436 \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u0441 \u043c\u0435\u0441\u044f\u0446\u0435\u043c:<\/p>\n<pre><code class=\"pgsql\">CREATE TABLE Sales (EmpId INT, Month INT, Yr INT, Sales MONEY)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430 \u043f\u043e \u043c\u0435\u0441\u044f\u0446\u0430\u043c \u0438 \u0433\u043e\u0434\u0430\u043c, \u043f\u043e \u0433\u043e\u0434\u0430\u043c \u0438 \u0432 \u0446\u0435\u043b\u043e\u043c.\u00a0\u041c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u044d\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044f\u0432\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 GROUPING SETS:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Month, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr, Month), (EmpId, Yr), (EmpId))<\/code><\/pre>\n<p>\u0418\u043b\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ROLLUP, \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"pgsql\">SELECT EmpId, Month, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS( ( EmpId, ROLLUP(Yr, Month)) )<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0440\u0443\u0433\u043b\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0440\u0435\u0448\u0430\u044e\u0449\u0435\u0435<br \/>\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.\u00a0\u0415\u0441\u043b\u0438 \u043c\u044b \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 \u0441\u043a\u043e\u0431\u043e\u043a \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0441\u043c\u044b\u0441\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f, \u0438 \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c ROLLUP \u0433\u043e\u0434\u0430 \u0438 \u043c\u0435\u0441\u044f\u0446\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/786554\/\"> https:\/\/habr.com\/ru\/articles\/786554\/<\/a><br \/><\/br><\/br><\/p>\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-365377","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/365377","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=365377"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/365377\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}