{"id":269593,"date":"2015-12-03T22:17:02","date_gmt":"2015-12-03T19:17:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=269593"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=269593","title":{"rendered":"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c TSQL \u0434\u043b\u044f \u0438\u0433\u0440\u044b \u0432 \u00ab\u0421\u0443\u0434\u043e\u043a\u0443\u00bb"},"content":{"rendered":"<p>       \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 TSQL \u0431\u044b\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u0430 \u0440\u0435\u0448\u0435\u043d\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/271795\/\">\u00ab\u0411\u0430\u043b\u0434\u0430\u00bb (\u0441\u0442\u0430\u0442\u044c\u044f)<\/a> \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0448\u0438\u0442\u044c \u043d\u0430 \u043d\u0435\u043c \u00ab\u0421\u0443\u0434\u043e\u043a\u0443\u00bb (\u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0438\u0434\u0435\u044e  <a href=\"http:\/\/habrahabr.ru\/users\/shavluk\/\" class=\"user_link\">shavluk<\/a>).<\/p>\n<p>  \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u0443\u0434\u043e\u043a\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c.<\/p>\n<p>  <b>\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:<\/b><br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/54f\/5a4\/402\/54f5a4402afd46258e11e94a17b29577.png\"\/><br \/>  <a name=\"habracut\"><\/a><br \/>  <b>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446:<\/b>  <\/p>\n<ul>\n<li>SudokuCell \u2013 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 (\u0440\u0435\u0433\u0438\u043e\u043d, \u0441\u0442\u0440\u043e\u043a\u0430, \u0441\u0442\u043e\u043b\u0431\u0435\u0446) \u0432\u0441\u0435\u0445 \u044f\u0447\u0435\u0435\u043a;<\/li>\n<li>SudokuValue \u2013 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u044f\u0447\u0435\u0439\u043a\u0438;<\/li>\n<li>SudokuField \u2013 \u043f\u043e\u043b\u0435, \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0446\u0438\u0444\u0440.<\/li>\n<\/ul>\n<p>  <b>\u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446:<\/b>  <\/p>\n<pre><code class=\"sql\">-- \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c IF EXISTS (SELECT * FROM sysobjects WHERE name='SudokuField') DROP TABLE SudokuField; IF EXISTS (SELECT * FROM sysobjects WHERE name='SudokuCell') DROP TABLE SudokuCell; IF EXISTS (SELECT * FROM sysobjects WHERE name='SudokuValue') DROP TABLE SudokuValue;    ---------------------------------------------- -- \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u044f\u0447\u0435\u0435\u043a \u0438 \u0438\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 ---------------------------------------------- CREATE TABLE SudokuCell(   ID int NOT NULL,       -- ID \u044f\u0447\u0435\u0439\u043a\u0438   RegionID int NOT NULL, -- \u0440\u0435\u0433\u0438\u043e\u043d   RowID int NOT NULL,    -- \u0441\u0442\u0440\u043e\u043a\u0430   ColID int NOT NULL,    -- \u0441\u0442\u043e\u043b\u0431\u0435\u0446 CONSTRAINT PK_SudokuCell PRIMARY KEY(ID) ) GO  -- \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 INSERT SudokuCell(ID,RegionID,RowID,ColID) SELECT   reg.ID*100+r.i*10+c.j,   reg.ID,   (reg.ID\/10-1)*3+r.i,   (reg.ID-1)%10*3+c.j FROM (VALUES (11),(12),(13),(21),(22),(23),(31),(32),(33)) reg(ID) CROSS JOIN (VALUES(1),(2),(3)) r(i) CROSS JOIN (VALUES(1),(2),(3)) c(j) GO    ---------------------------------------------- -- \u0446\u0438\u0444\u0440\u044b 1-9 (\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f) ---------------------------------------------- CREATE TABLE SudokuValue(   Value char(1) NOT NULL, CONSTRAINT PK_SudokuValue PRIMARY KEY(Value) ) GO  -- \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 INSERT SudokuValue(Value) VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9) GO    ---------------------------------------------- -- \u043f\u043e\u043b\u0435 ---------------------------------------------- CREATE TABLE SudokuField(   CellID int NOT NULL,   Value char(1) NOT NULL, CONSTRAINT PK_SudokuField PRIMARY KEY(CellID), CONSTRAINT FK_SudokuField_CellID FOREIGN KEY(CellID) REFERENCES SudokuCell(ID), CONSTRAINT FK_SudokuField_Value FOREIGN KEY(Value) REFERENCES SudokuValue(Value) ) GO <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c\u0438 \u0446\u0438\u0444\u0440\u0430\u043c\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/b>  <\/p>\n<pre><code class=\"sql\">-- \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0447\u0438\u0441\u0442\u043a\u0430 \u043f\u043e\u043b\u044f, \u043d\u0430 \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439 \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e TRUNCATE TABLE SudokuField GO  -- \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c\u0438 \u0446\u0438\u0444\u0440\u0430\u043c\u0438 INSERT SudokuField(CellID,Value)VALUES (1122,'3'),(1123,'4'), (1211,'1'),(1212,'5'), (1322,'8'),(1323,'9'),(1333,'3'),  (2112,'2'),(2122,'4'),(2123,'7'),(2133,'9'), (2212,'6'),(2223,'9'),(2232,'2'), (2311,'8'),(2333,'1'),  (3111,'1'), (3213,'2'),(3221,'9'), (3313,'5'),(3332,'7'),(3333,'4') GO <\/code><\/pre>\n<p>  \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u044f\u0447\u0435\u0439\u043a\u0438 (CellID) \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<ul>\n<li>\u041f\u0435\u0440\u0432\u0430\u044f \u0438 \u0432\u0442\u043e\u0440\u0430\u044f \u0446\u0438\u0444\u0440\u0430 \u0447\u0438\u0441\u043b\u0430 \u2013 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043a\u0430\u043a\u043e\u0439 \u044d\u0442\u043e \u0440\u0435\u0433\u0438\u043e\u043d (\u0441\u0442\u0440\u043e\u043a\u0430, \u0441\u0442\u043e\u043b\u0431\u0435\u0446);<\/li>\n<li>\u0422\u0440\u0435\u0442\u044c\u044f \u0438 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u0430\u044f \u0446\u0438\u0444\u0440\u0430 \u2013 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0435.<\/li>\n<\/ul>\n<p>  \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044f \u0431\u0440\u0430\u043b \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u2013 <a href=\"http:\/\/japonskie.ru\/index.php?place=sudoku&amp;slev=5&amp;lev=0&amp;sort=0\">\u0441\u0441\u044b\u043b\u043a\u0430<\/a>.<\/p>\n<p>  <b>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u043b\u0435:<\/b>  <\/p>\n<pre><code class=\"sql\">-- \u0432\u0438\u0434 \u043f\u043e\u043b\u044f \u0434\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f SELECT   ISNULL([1],'') [1],   ISNULL([2],'') [2],   ISNULL([3],'') [3],   ISNULL([4],'') [4],   ISNULL([5],'') [5],   ISNULL([6],'') [6],   ISNULL([7],'') [7],   ISNULL([8],'') [8],   ISNULL([9],'') [9] FROM   (     SELECT c.RowID,c.ColID,f.Value     FROM SudokuCell c     LEFT JOIN SudokuField f ON f.CellID=c.ID   ) q PIVOT(MAX(Value) FOR ColID IN([1],[2],[3],[4],[5],[6],[7],[8],[9])) p ORDER BY RowID <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/a97\/6fa\/ce5\/a976face51a34198930242245a5da51b.png\"\/><\/p>\n<p>  <b>\u0414\u0430\u043b\u044c\u0448\u0435 \u0438\u0434\u0435\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438:<\/b>  <\/p>\n<pre><code class=\"sql\">-- \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u0446\u0438\u0444\u0440\u044b \u0432 \u043f\u0443\u0441\u0442\u044b\u0445 \u044f\u0447\u0435\u0439\u043a\u0430\u0445 SELECT   -- \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 - \u0443\u043a\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430   CONCAT('&gt;',CAST(CellNo AS varchar(4)),CHAR(ASCII('a')+Value-1)) ID,   * INTO #SudokuVariant FROM   (     -- \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u044f\u0447\u0435\u0439\u043a\u0438     SELECT       ID CellID,RowID,ColID,RegionID,       -- \u043d\u0443\u043c\u0435\u0440\u0443\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0438       CAST(DENSE_RANK()OVER(ORDER BY ID) AS int) CellNo     FROM SudokuCell     WHERE ID NOT IN(SELECT CellID FROM SudokuField)   ) e CROSS APPLY   (     -- \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u0446\u0438\u0444\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043f\u0443\u0441\u0442\u0443\u044e \u044f\u0447\u0435\u0439\u043a\u0443     SELECT v.Value     FROM SudokuCell c     JOIN SudokuField f ON f.CellID=c.ID AND (c.ColID=e.ColID OR c.RowID=e.RowID OR c.RegionID=e.RegionID)     RIGHT JOIN SudokuValue v ON v.Value=f.Value     WHERE c.ID IS NULL -- \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0446\u0438\u0444\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0442 \u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0435\/\u0441\u0442\u0440\u043e\u043a\u0435\/\u0441\u0442\u043e\u043b\u0431\u0446\u0435   ) v  --SELECT * FROM #SudokuVariant   -- \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 CREATE TABLE #SudokuTree(   CellNo int NOT NULL,   VariantPath varchar(1000) NOT NULL,   PathLen int NOT NULL )  -- \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u0440\u043d\u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0438\u0437 \u044f\u0447\u0435\u0435\u043a \u0441 CellNo=1 INSERT #SudokuTree(CellNo,VariantPath,PathLen) SELECT CellNo,ID,1 FROM #SudokuVariant WHERE CellNo=1  --SELECT * FROM #SudokuTree    -- \u044d\u0442\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430 \u0446\u0435\u043f\u043e\u0447\u043a\u0438 DECLARE @MaxCellNo int=(SELECT MAX(CellNo) FROM #SudokuVariant)  -- \u043d\u043e\u043c\u0435\u0440\u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435\u0439 \u044f\u0447\u0435\u0435\u043a DECLARE @CurrCellNo int=1 DECLARE @NextCellNo int=@CurrCellNo+1  -- \u0441\u0442\u0440\u043e\u0438\u043c \u0434\u0435\u0440\u0435\u0432\u043e WHILE @CurrCellNo&lt;@MaxCellNo BEGIN    -- \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0440\u043e\u0441\u0442\u043a\u043e\u0432   INSERT #SudokuTree(CellNo,VariantPath,PathLen)   SELECT     v.CellNo,     CONCAT(t.VariantPath,v.ID),     t.PathLen+1   FROM #SudokuTree t   JOIN #SudokuVariant v ON t.CellNo=@CurrCellNo AND v.CellNo=@NextCellNo   \/*     \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u0443\u0434\u0443\u0442 \u0432\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0442 \u0432 \u0440\u0435\u0433\u0438\u043e\u043d\u0435\/\u0441\u0442\u0440\u043e\u043a\u0435\/\u0441\u0442\u043e\u043b\u0431\u0446\u0435     \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u0439, \u0442.\u043a. \u043c\u044b \u0443\u0436\u0435 \u043e\u0442\u0441\u0435\u043a\u043b\u0438 \u043d\u0435\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f     \u043f\u0440\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b #SudokuVariant   *\/    WHERE NOT EXISTS(                 SELECT *                 FROM #SudokuVariant i                 WHERE t.VariantPath LIKE '%'+i.ID+'%'                   AND (i.RegionID=v.RegionID OR i.RowID=v.RowID OR i.ColID=v.ColID)                   AND i.Value=v.Value               )    \/*     \u0442.\u043a. \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u0443 \u043d\u0430\u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 VariantPath,     \u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c   *\/   DELETE #SudokuTree WHERE CellNo=@CurrCellNo     -- \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0432\u044b\u0448\u0435   SET @CurrCellNo+=1   SET @NextCellNo+=1  END  --SELECT * FROM #SudokuTree    -- \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u043e\u043b\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 INSERT SudokuField(CellID,Value) SELECT v.CellID,v.Value FROM #SudokuVariant v JOIN   (     -- \u0435\u0441\u043b\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u0431\u0435\u0440\u0435\u043c \u0441\u0430\u043c\u043e\u0435 \u043f\u0435\u0440\u0432\u043e\u0435     SELECT TOP 1 *     FROM #SudokuTree   ) r ON r.VariantPath LIKE '%'+v.ID+'%'    -- \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b DROP TABLE #SudokuTree DROP TABLE #SudokuVariant <\/code><\/pre>\n<p>  \u041d\u0430 \u043c\u043e\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 10-15 \u0441\u0435\u043a\u0443\u043d\u0434 (\u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439).<\/p>\n<p>  <b>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043d\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/b>  <\/p>\n<pre><code class=\"sql\">-- \u0432\u0438\u0434 \u043f\u043e\u043b\u044f \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f SELECT   ISNULL([1],'') [1],   ISNULL([2],'') [2],   ISNULL([3],'') [3],   ISNULL([4],'') [4],   ISNULL([5],'') [5],   ISNULL([6],'') [6],   ISNULL([7],'') [7],   ISNULL([8],'') [8],   ISNULL([9],'') [9] FROM   (     SELECT c.RowID,c.ColID,f.Value     FROM SudokuCell c     LEFT JOIN SudokuField f ON f.CellID=c.ID   ) q PIVOT(MAX(Value) FOR ColID IN([1],[2],[3],[4],[5],[6],[7],[8],[9])) p ORDER BY RowID <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/9c8\/3c3\/ace\/9c83c3ace1974d5e88224233a8efb7cd.png\"\/><\/p>\n<p>  <b>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/b><br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/ea4\/1d0\/7d6\/ea41d07d6465452aa1f1c8a89ca80cfc.png\"\/><br \/>  \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u044b:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442&#8230;<\/b><\/p>\n<div class=\"spoiler_text\">\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"sql\">-- \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0447\u0438\u0441\u0442\u043a\u0430 \u043f\u043e\u043b\u044f, \u043d\u0430 \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439 \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e TRUNCATE TABLE SudokuField GO  -- \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c\u0438 \u0446\u0438\u0444\u0440\u0430\u043c\u0438 INSERT SudokuField(CellID,Value)VALUES (1112,'7'),(1113,'1'),(1131,'4'),(1132,'9'), (1212,'9'),(1221,'3'),(1223,'6'), (1311,'8'),(1331,'7'),(1333,'5'),  (2112,'1'),(2121,'9'),(2123,'2'), (2211,'9'),(2233,'8'), (2321,'6'),(2323,'3'),(2332,'2'),  (3111,'8'),(3113,'5'),(3133,'7'), (3221,'6'),(3223,'7'),(3232,'4'), (3312,'7'),(3313,'6'),(3331,'3'),(3332,'5') GO <\/code><\/pre>\n<p>  \u0422\u0440\u0435\u0442\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"sql\">-- \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0447\u0438\u0441\u0442\u043a\u0430 \u043f\u043e\u043b\u044f, \u043d\u0430 \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439 \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e TRUNCATE TABLE SudokuField GO  -- \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c\u0438 \u0446\u0438\u0444\u0440\u0430\u043c\u0438 INSERT SudokuField(CellID,Value)VALUES (1132,'4'), (1221,'9'),(1223,'4'),(1231,'8'),(1233,'5'), (1311,'7'),(1321,'2'),(1323,'3'),  (2113,'7'),(2121,'9'),(2133,'3'), (2212,'1'), (2311,'6'),(2332,'5'),(2333,'2'),  (3111,'6'),(3112,'2'),(3121,'7'), (3223,'3'),(3233,'8'), (3323,'9') GO <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u0441\u0435.<\/b><\/p>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0441\u044b\u043b\u043a\u0435 \u2013 <a href=\"http:\/\/buhgalter-online.kz\/files\/sql\/sudoku.sql\">\u0441\u043a\u0440\u0438\u043f\u0442<\/a>.<\/p>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u044b\u043b\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430.<\/p>\n<p>  \u0423\u0434\u0430\u0447\u0438 \u0438 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!               <\/p>\n<div class=\"clear\"><\/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\/272373\/\"> http:\/\/habrahabr.ru\/post\/272373\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 TSQL \u0431\u044b\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u0430 \u0440\u0435\u0448\u0435\u043d\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/271795\/\">\u00ab\u0411\u0430\u043b\u0434\u0430\u00bb (\u0441\u0442\u0430\u0442\u044c\u044f)<\/a> \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0448\u0438\u0442\u044c \u043d\u0430 \u043d\u0435\u043c \u00ab\u0421\u0443\u0434\u043e\u043a\u0443\u00bb (\u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0438\u0434\u0435\u044e  <a href=\"http:\/\/habrahabr.ru\/users\/shavluk\/\" class=\"user_link\">shavluk<\/a>).<\/p>\n<p>  \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u0443\u0434\u043e\u043a\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c.<\/p>\n<p>  <b>\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:<\/b><br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/54f\/5a4\/402\/54f5a4402afd46258e11e94a17b29577.png\"\/>  <\/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-269593","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269593","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=269593"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269593\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}