{"id":197476,"date":"2013-10-15T16:11:04","date_gmt":"2013-10-15T12:11:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=197476"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=197476","title":{"rendered":"<span class=\"post_title\">\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 UNPIVOT \u0438 \u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0417\u0430 \u0432\u0440\u0435\u043c\u044f \u043c\u043e\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u0438 DBA, \u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u0448\u0438\u0440\u043e\u043a\u0438\u043c \u043a\u0440\u0443\u0433\u043e\u043c \u0437\u0430\u0434\u0430\u0447. \u041e\u0434\u043d\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0438 \u043c\u043e\u043d\u043e\u0442\u043e\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u043a \u0447\u0438\u0441\u0442\u043e\u043c\u0443 \u043a\u0440\u0435\u0430\u0442\u0438\u0432\u0443. <\/p>\n<p>  \u0421\u0430\u043c\u044b\u0435 \u043a\u0440\u0435\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043c\u043e\u0433\u0443 \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0442\u0430\u043a \u0438\u043b\u0438 \u0438\u043d\u0430\u0447\u0435, \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u043b\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>  \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2013 \u044d\u0442\u043e, \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043f\u043e\u0438\u0441\u043a \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u0434\u043d\u0430\u043a\u043e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0432\u044b\u0434\u0430\u0435\u0442 \u043f\u043b\u0430\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0447\u0435\u043d\u044c \u0434\u0430\u043b\u0435\u043a \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e?<\/p>\n<p>  \u0418\u043c\u0435\u043d\u043d\u043e \u0441 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u044f \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e UNPIVOT \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>  \u0412\u044b\u0445\u043e\u0434 \u0431\u044b\u043b \u043e\u0434\u0438\u043d \u2013 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u043d\u0430\u0439\u0442\u0438 \u0434\u043b\u044f UNPIVOT \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0443\u2026<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0435 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0439, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u0430\u0441\u043f\u043e\u0440\u044f\u0436\u0435\u043d\u0438\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u0435\u0434\u0430\u043b\u0435\u0439 \u0441\u0440\u0435\u0434\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<pre><code class=\"sql\">IF OBJECT_ID('dbo.UserBadges', 'U') IS NOT NULL \tDROP TABLE dbo.UserBadges  CREATE TABLE dbo.UserBadges ( \t  UserID INT \t, Gold SMALLINT NOT NULL \t, Silver SMALLINT NOT NULL \t, Bronze SMALLINT NOT NULL \t, CONSTRAINT PK_UserBadges PRIMARY KEY (UserID) )  INSERT INTO dbo.UserBadges (UserID, Gold, Silver, Bronze) VALUES \t(1, 5, 3, 1), \t(2, 0, 8, 1), \t(3, 2, 4, 11) <\/code><\/pre>\n<p>  \u0412\u0437\u044f\u0432 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043b\u0430\u043d\u044b \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0431\u044b\u043b\u043e \u043b\u0438\u0448\u043d\u0438\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u0432 \u0434\u0443\u0448\u0435 \u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0435\u0440\u0444\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0441\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u043b\u0430\u043d\u0430\u043c\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u044f \u043f\u043e\u043b\u0443\u0447\u0430\u044e \u0432 <a href=\"http:\/\/www.devart.com\/ru\/dbforge\/sql\/studio\/\">dbForge Studio for SQL Server<\/a>. \u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435, \u0432\u0441\u0435 \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u044b \u043f\u043b\u0430\u043d\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u0430 \u043d\u0435 \u0432 SSMS.<\/p>\n<p>  \u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043e\u0442 \u0441\u043b\u043e\u0432 \u043a \u0434\u0435\u043b\u0443\u2026<\/p>\n<h4>1. UNION ALL<\/h4>\n<p>  \u0412 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, <i>SQL Server 2000<\/i> \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u043b \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0432 \u0441\u0442\u0440\u043e\u043a\u0438. \u0412\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u0448\u0438\u0440\u043e\u043a\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043d\u043e \u0441 \u0440\u0430\u0437\u043d\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <i>UNION ALL<\/i>:<\/p>\n<pre><code class=\"sql\">SELECT UserID, BadgeCount = Gold, BadgeType = 'Gold'  FROM dbo.UserBadges     UNION ALL SELECT UserID, Silver, 'Silver'  FROM dbo.UserBadges     UNION ALL SELECT UserID, Bronze, 'Bronze'  FROM dbo.UserBadges <\/code><\/pre>\n<p>  \u041e\u0433\u0440\u043e\u043c\u043d\u044b\u043c \u043c\u0438\u043d\u0443\u0441 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u2014 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0435 \u0447\u0442\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043d\u0438\u0436\u0430\u043b\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. <\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u043f\u043b\u0430\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u0441\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/f5e\/9dd\/135\/f5e9dd135a8a87457e67c5ee2381d7ff.png\"\/><\/p>\n<h4>2. UNPIVOT<\/h4>\n<p>  \u0421 \u0440\u0435\u043b\u0438\u0437\u043e\u043c <i>SQL Server 2005<\/i>, \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u044f\u0437\u044b\u043a\u0430 <i>T-SQL<\/i> \u2013 <i>UNPIVOT<\/i>. <\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u044f <i>UNPIVOT<\/i> \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0434\u043e:<\/p>\n<pre><code class=\"sql\">SELECT UserID, BadgeCount, BadgeType FROM dbo.UserBadges UNPIVOT (     BadgeCount FOR BadgeType IN (Gold, Silver, Bronze) ) unpvt <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043b\u0430\u043d:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/6ba\/726\/2ad\/6ba7262ad5f74ac2347b34b0a21d55ca.png\"\/><\/p>\n<h4>3. VALUES<\/h4>\n<p>  \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 <i>SQL Server 2008<\/i> \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <i>VALUES<\/i> \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0445 <i>INSERT <\/i>\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043d\u043e \u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u0430 <i>FROM<\/i>.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <i>VALUES<\/i>, \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">SELECT p.UserID, t.* FROM dbo.UserBadges p CROSS APPLY (     VALUES            (Gold,   'Gold')         , (Silver, 'Silver')         , (Bronze, 'Bronze') ) t(BadgeCount, BadgeType) <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u043f\u043e-\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 <i>UNPIVOT<\/i>, \u043f\u043b\u0430\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u0441\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/d4c\/3b7\/299\/d4c3b7299c420cbcfb7fc8d54ed808c4.png\"\/><\/p>\n<h4>4. Dynamic SQL<\/h4>\n<p>  \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 SQL, \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u00ab\u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439\u00bb \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u2014 \u0441\u0442\u043e\u043b\u0431\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0432\u0445\u043e\u0434\u044f\u0442 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430, \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0435 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u0423\u0437\u043d\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0430\u043a\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c:<\/p>\n<pre><code class=\"sql\">SELECT c.name FROM sys.columns c WITH(NOLOCK) LEFT JOIN ( \tSELECT i.[object_id], i.column_id \tFROM sys.index_columns i WITH(NOLOCK) \tWHERE i.index_id = 1 ) i ON c.[object_id] = i.[object_id] AND c.column_id = i.column_id WHERE c.[object_id] = OBJECT_ID('dbo.UserBadges', 'U') \tAND i.[object_id] IS NULL <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043f\u043b\u0430\u043d \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 <i>sys.index_columns<\/i> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u043e\u0439:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/a47\/3b3\/49b\/a473b349b30096669f04f2653172775b.png\"\/><\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <i>INDEX_COL<\/i>. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u0440\u0438\u043c\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:<\/p>\n<pre><code class=\"sql\">DECLARE @table_name SYSNAME SELECT @table_name = 'dbo.UserBadges'  DECLARE @SQL NVARCHAR(MAX) SELECT @SQL = ' SELECT *  FROM ' + @table_name + ' UNPIVOT (     value FOR code IN (         ' + STUFF((     SELECT ', [' + c.name + ']'     FROM sys.columns c WITH(NOLOCK)     WHERE c.[object_id] = OBJECT_ID(@table_name) \t\t\t\tAND INDEX_COL(@table_name, 1, c.column_id) IS NULL     FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') + '     ) ) unpiv'  PRINT @SQL EXEC sys.sp_executesql @SQL <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u043c:<\/p>\n<pre><code class=\"sql\">SELECT *  FROM &lt;table_name&gt; UNPIVOT ( \tvalue FOR code IN (&lt;unpivot_column&gt;) ) unpiv <\/code><\/pre>\n<p>  \u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0431\u0440\u0430\u0442\u044c \u0432\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u043b\u0438, \u0441\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>  \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443, \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 <i>UNPIVOT<\/i>, \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430 \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u0438 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u043e\u043a \u0447\u0435\u0440\u0435\u0437 <i>XML<\/i>:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/2a2\/d5c\/285\/2a2d5c28558ff960c7826b11fbb65b55.png\"\/><\/p>\n<h4>5. XML<\/h4>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 <i>UNPIVOT <\/i>\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0442\u0440\u044e\u043a \u0441 <i>XML<\/i>:<\/p>\n<pre><code class=\"sql\">SELECT       p.UserID     , BadgeCount = t.c.value('.', 'INT')      , BadgeType = t.c.value('local-name(.)', 'VARCHAR(10)')  FROM (     SELECT            UserID         , [XML] = (                 SELECT Gold, Silver, Bronze                 FOR XML RAW('t'), TYPE             )     FROM dbo.UserBadges ) p CROSS APPLY p.[XML].nodes('t\/@*') t(c) <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f <i>XML <\/i>\u0432\u0438\u0434\u0430:<\/p>\n<pre><code class=\"xml\">&lt;t Column1=&quot;Value1&quot; Column2=&quot;Value2&quot; Column3=&quot;Value3&quot; ... \/&gt; <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0430\u0440\u0441\u0438\u0442\u0441\u044f \u0438\u043c\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0438 \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <i>XML <\/i>\u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u043b\u0430\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u2013 \u044d\u0442\u043e \u0440\u0430\u0441\u043f\u043b\u0430\u0442\u0430 \u0437\u0430 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/cee\/32e\/3c5\/cee32e3c524e4fc8f34a610985909f4b.png\"\/><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/f27\/621\/961\/f27621961e4546c9ac3f3a1cc3b70b8a.png\"\/><\/p>\n<p>  \u041a\u0430\u0440\u0434\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u0432 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 <i>UNPIVOT <\/i>\u0438 <i>VALUES <\/i>\u043d\u0435 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0441\u044f. \u042d\u0442\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u043d\u043e, \u0435\u0441\u043b\u0438 \u0440\u0435\u0447\u044c \u0438\u0434\u0435\u0442 \u043e \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>  \u0423\u0441\u043b\u043e\u0436\u043d\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0440\u0443\u0433\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0433\u0434\u0435 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0442\u0438\u043f \u043c\u0435\u0434\u0430\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0443 \u043d\u0435\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e.<\/p>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <i>UNPIVOT<\/i>:<\/p>\n<pre><code class=\"sql\">SELECT        UserID     , GameType = (         SELECT TOP 1 BadgeType          FROM dbo.UserBadges b2          UNPIVOT (             BadgeCount FOR BadgeType IN (Gold, Silver, Bronze)         ) unpvt         WHERE UserID = b.UserID          ORDER BY BadgeCount DESC     )  FROM dbo.UserBadges b <\/code><\/pre>\n<p>  \u041d\u0430 \u043f\u043b\u0430\u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u043c \u0447\u0442\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0434\u043b\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/431\/ae9\/f0f\/431ae9f0fdd606ec169799fd49fa0ae2.png\"\/><\/p>\n<p>  \u0418\u0437\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u043e\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u0435\u0433\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0432 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438\u0437 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430:<\/p>\n<pre><code class=\"sql\">SELECT  \t  UserID \t, GameType = ( \t\tSELECT TOP 1 BadgeType \t\tFROM (SELECT t = 1) t  \t\tUNPIVOT ( \t\t\tBadgeCount FOR BadgeType IN (Gold, Silver, Bronze) \t\t) unpvt \t\tORDER BY BadgeCount DESC \t)  FROM dbo.UserBadges <\/code><\/pre>\n<p>  \u041f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0435 \u0447\u0442\u0435\u043d\u0438\u044f \u0443\u0448\u043b\u0438, \u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0438\u043a\u0443\u0434\u0430 \u043d\u0435 \u0434\u0435\u043b\u0430\u0441\u044c:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/e94\/feb\/14d\/e94feb14dae7d5cb9b447e802fa774e8.png\"\/><\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f <i>VALUES <\/i>\u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435:<\/p>\n<pre><code class=\"sql\">SELECT        UserID     , GameType = (             SELECT TOP 1 BadgeType             FROM (                 VALUES                       (Gold,   'Gold')                     , (Silver, 'Silver')                     , (Bronze, 'Bronze')             ) t (BadgeCount, BadgeType)             ORDER BY BadgeCount DESC         )  FROM dbo.UserBadges <\/code><\/pre>\n<p>  \u041f\u043b\u0430\u043d \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b\u0441\u044f, \u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u043f\u043b\u0430\u043d\u0435:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/516\/755\/875\/516755875f7b03d694648c238d2b1b60.png\"\/><\/p>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0431\u043e\u0439\u0442\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u0433\u0433\u0440\u0435\u0433\u0438\u0440\u0443\u044e\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"sql\">SELECT  \t  UserID \t, BadgeType = (             SELECT TOP 1 BadgeType             FROM (                 VALUES                       (Gold,   'Gold')                     , (Silver, 'Silver')                     , (Bronze, 'Bronze')             ) t (BadgeCount, BadgeType) \t\t\tWHERE BadgeCount = ( \t\t\t\tSELECT MAX(Value) \t\t\t\tFROM ( \t\t\t\t\tVALUES (Gold), (Silver), (Bronze) \t\t\t\t) t(Value) \t\t\t) \t\t)  FROM dbo.UserBadges <\/code><\/pre>\n<p>  \u041c\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/0e6\/63a\/caa\/0e663acaac785d9dd33545827ebe5aa6.png\"\/><\/p>\n<p>  <b>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438\u0442\u043e\u0433\u0438:<\/b><\/p>\n<p>  \u0412 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0438, \u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <i>UNPIVOT <\/i>\u0438\u043b\u0438 <i>VALUES<\/i>.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u043b\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <i>VALUES<\/i>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f, \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u043b\u0430\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0447\u0438\u0441\u043b\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u0447\u0438\u0432\u043e, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <i>XML<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e SQL, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439.<\/p>\n<p>  <b>P.S.<\/b><\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043f\u043e\u0434 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 <i>SQL Server 2005<\/i>, \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c <i>VALUES<\/i>:<\/p>\n<pre><code class=\"sql\">SELECT *  FROM ( \tVALUES (1, 'a'), (2, 'b') ) t(id, value) <\/code><\/pre>\n<p>  \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044e <i>SELECT UNION ALL SELECT<\/i>:<\/p>\n<pre><code class=\"sql\">SELECT id = 1, value = 'a' UNION ALL SELECT 2, 'b' <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<\/p><\/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=\"http:\/\/habrahabr.ru\/post\/197476\/\"> http:\/\/habrahabr.ru\/post\/197476\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0417\u0430 \u0432\u0440\u0435\u043c\u044f \u043c\u043e\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u043e\u0441\u0442\u0438 DBA, \u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u0448\u0438\u0440\u043e\u043a\u0438\u043c \u043a\u0440\u0443\u0433\u043e\u043c \u0437\u0430\u0434\u0430\u0447. \u041e\u0434\u043d\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0438 \u043c\u043e\u043d\u043e\u0442\u043e\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u043a \u0447\u0438\u0441\u0442\u043e\u043c\u0443 \u043a\u0440\u0435\u0430\u0442\u0438\u0432\u0443. <\/p>\n<p>  \u0421\u0430\u043c\u044b\u0435 \u043a\u0440\u0435\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043c\u043e\u0433\u0443 \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0442\u0430\u043a \u0438\u043b\u0438 \u0438\u043d\u0430\u0447\u0435, \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u043b\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>  \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2013 \u044d\u0442\u043e, \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043f\u043e\u0438\u0441\u043a \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u0434\u043d\u0430\u043a\u043e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0432\u044b\u0434\u0430\u0435\u0442 \u043f\u043b\u0430\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0447\u0435\u043d\u044c \u0434\u0430\u043b\u0435\u043a \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e?<\/p>\n<p>  \u0418\u043c\u0435\u043d\u043d\u043e \u0441 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u044f \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e UNPIVOT \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>  \u0412\u044b\u0445\u043e\u0434 \u0431\u044b\u043b \u043e\u0434\u0438\u043d \u2013 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u043d\u0430\u0439\u0442\u0438 \u0434\u043b\u044f UNPIVOT \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0443\u2026  <\/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-197476","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/197476","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=197476"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/197476\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=197476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=197476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=197476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}