{"id":267876,"date":"2015-11-10T13:02:05","date_gmt":"2015-11-10T10:02:05","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=267876"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=267876","title":{"rendered":"\u041a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u0441\u0430\u043c\u044b\u0439 \u0434\u043b\u0438\u043d\u043d\u044b\u0439 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u0440\u044f\u0434 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 SQL"},"content":{"rendered":"<p>       \u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0442\u043e\u0447\u043d\u0438\u043c, \u0447\u0442\u043e \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u044d\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0451\u043c Stack Overflow. \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043b\u0451\u0432\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0440\u0435\u043f\u0443\u0442\u0430\u0446\u0438\u0438 \u0441 \u043d\u0430\u0433\u0440\u0430\u0434\u0430\u043c\u0438 \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f. \u041a\u0430\u043a \u0438 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u043e\u043d\u0438 \u043f\u043e\u043e\u0449\u0440\u044f\u044e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e \u043f\u043e\u0441\u0435\u0449\u0430\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441. \u041e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u0438 \u0434\u0432\u0435 \u043d\u0430\u0433\u0440\u0430\u0434\u044b:<\/p>\n<p>  <a href=\"http:\/\/stackoverflow.com\/help\/badges\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/94b\/f67\/7d5\/94bf677d5854489388e4d7763f0d3549.png\"\/><\/a><\/p>\n<p>  \u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442. \u0417\u0430\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u0430\u0439\u0442 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0434\u0435\u043d\u044c. \u0417\u0430\u0442\u0435\u043c \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0434\u0435\u043d\u044c. \u0417\u0430\u0442\u0435\u043c \u043d\u0430 \u0442\u0440\u0435\u0442\u0438\u0439 (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437, \u044d\u0442\u043e \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f). \u041d\u0435 \u0437\u0430\u0448\u043b\u0438 \u043d\u0430 \u0447\u0435\u0442\u0432\u0451\u0440\u0442\u044b\u0439? \u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u0430\u043d\u043e\u0432\u043e.<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h4><b>\u041a\u0430\u043a \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL?<\/b><\/h4>\n<p>  \u041d\u0430 <a href=\"http:\/\/blog.jooq.org\/\">\u0430\u0432\u0442\u043e\u0440\u0441\u043a\u043e\u043c \u0431\u043b\u043e\u0433\u0435<\/a>, \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL \u0438 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c. \u0414\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/data.stackexchange.com\/\">Stack Exchange Data Explorer<\/a>.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0434\u0430\u0442\u044b \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u043c \u0434\u0430\u0442\u044b \u0440\u0430\u0437\u043c\u0435\u0449\u0451\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439.<\/p>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f SQL Server, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">SELECT DISTINCT CAST(CreationDate AS DATE) AS date FROM Posts WHERE OwnerUserId = ##UserId## ORDER BY 1 <\/code><\/pre>\n<p>  \u2026 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0434\u0430\u0441\u0442 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435:<\/p>\n<pre><code class=\"diff\">date           ---------- 2010-11-26 2010-11-27 2010-11-29 2010-11-30 2010-12-01 2010-12-02 2010-12-03 2010-12-05 2010-12-06 2010-12-07 2010-12-08 2010-12-09 2010-12-13 2010-12-14 ... (769 rows) <\/code><\/pre>\n<p>  <a href=\"https:\/\/data.stackexchange.com\/stackoverflow\/query\/386932\/all-the-distinct-days-on-which-i-created-posts?opt.textResults=true&amp;UserId=521799\">(\u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0434\u0435\u0441\u044c)<\/a><\/p>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432:<\/p>\n<pre><code class=\"diff\">date           -------------------------------------- 2010-11-26 2010-11-27 &lt;---- \u041f\u0440\u043e\u043f\u0443\u0441\u043a \u043f\u043e\u0441\u043b\u0435 2 \u0434\u043d\u0435\u0439  2010-11-29 2010-11-30 2010-12-01 2010-12-02 2010-12-03 &lt;---- \u041f\u0440\u043e\u043f\u0443\u0441\u043a \u043f\u043e\u0441\u043b\u0435 5 \u0434\u043d\u0435\u0439  2010-12-05 2010-12-06 2010-12-07 2010-12-08 2010-12-09 &lt;---- \u041f\u0440\u043e\u043f\u0443\u0441\u043a \u043f\u043e\u0441\u043b\u0435 5 \u0434\u043d\u0435\u0439  2010-12-13 2010-12-14 ... <\/code><\/pre>\n<p>  \u0427\u0435\u043b\u043e\u0432\u0435\u043a\u0443 \u043b\u0435\u0433\u043a\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u043f\u043e\u0434\u0440\u044f\u0434 \u0431\u044b\u043b\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432. \u041d\u043e \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c SQL?<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u00ab\u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c\u00bb \u0438\u043d\u0434\u0438\u0432\u0438\u0434\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u043e\u0431\u0449\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u044b \u043d\u0430\u0437\u043e\u0432\u0451\u043c <b>dates<\/b>:<\/p>\n<pre><code class=\"sql\">WITH     -- This table contains all the distinct date    -- instances in the data set   dates(date) AS (     SELECT DISTINCT CAST(CreationDate AS DATE)     FROM Posts     WHERE OwnerUserId = ##UserId##   ) ... <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0446\u0435\u043b\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u2014 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u0442\u044b \u0432 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0433\u0440\u0443\u043f\u043f\u0443, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0438\u0445. \u0412\u043e\u0442 \u043a\u0430\u043a \u043c\u044b \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0435\u043c:<\/p>\n<pre><code class=\"sql\">SELECT   COUNT(*) AS consecutiveDates,   MIN(week) AS minDate,   MAX(week) AS maxDate FROM groups GROUP BY grp -- This &quot;grp&quot; value will be explained later ORDER BY 1 DESC, 2 DESC<\/code><\/pre>\n<p>  \u041c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043a\u0430\u0436\u0434\u0443\u044e \u0433\u0440\u0443\u043f\u043f\u0443 \u00abgrp\u00bb \u0438 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0430\u0442 \u0432 \u0433\u0440\u0443\u043f\u043f\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435.<\/p>\n<h4><b>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0433\u0440\u0443\u043f\u043f \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u0442<\/b><\/h4>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u0435\u0435, \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432 \u0432 \u0434\u0430\u0442\u0430\u0445:<\/p>\n<pre><code class=\"diff\">row number   date           -------------------------------- 1            2010-11-26 2            2010-11-27  3            2010-11-29 &lt;-- \u043f\u0440\u043e\u043f\u0443\u0441\u043a \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 4            2010-11-30 5            2010-12-01 6            2010-12-02 7            2010-12-03  8            2010-12-05 &lt;-- \u043f\u0440\u043e\u043f\u0443\u0441\u043a \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0430\u0437\u0440\u044b\u0432 \u043c\u0435\u0436\u0434\u0443 \u0434\u0430\u0442\u0430\u043c\u0438 (\u0434\u0432\u0435 \u0434\u0430\u0442\u044b \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438), \u0438\u0445 \u043d\u043e\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u043e\u043a \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 ROW_NUMBER():<\/p>\n<pre><code class=\"sql\">SELECT   ROW_NUMBER() OVER (ORDER BY date) AS [row number],   date FROM dates <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">WITH     -- This table contains all the distinct date    -- instances in the data set   dates(date) AS (     SELECT DISTINCT CAST(CreationDate AS DATE)     FROM Posts     WHERE OwnerUserId = ##UserId##   ),       -- Generate &quot;groups&quot; of dates by subtracting the   -- date's row number (no gaps) from the date itself   -- (with potential gaps). Whenever there is a gap,   -- there will be a new group   groups AS (     SELECT       ROW_NUMBER() OVER (ORDER BY date) AS rn,       dateadd(day, -ROW_NUMBER() OVER (ORDER BY date), date) AS grp,       date     FROM dates   ) SELECT * FROM groups ORDER BY rn <\/code><\/pre>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u0430\u0451\u0442 \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"diff\">rn  grp          date           --- ----------   ---------- 1   2010-11-25   2010-11-26 2   2010-11-25   2010-11-27  3   2010-11-26   2010-11-29 4   2010-11-26   2010-11-30 5   2010-11-26   2010-12-01 6   2010-11-26   2010-12-02 7   2010-11-26   2010-12-03  8   2010-11-27   2010-12-05 9   2010-11-27   2010-12-06 10  2010-11-27   2010-12-07 11  2010-11-27   2010-12-08 12  2010-11-27   2010-12-09  13  2010-11-30   2010-12-13 14  2010-11-30   2010-12-14 <\/code><\/pre>\n<p>  <a href=\"https:\/\/data.stackexchange.com\/stackoverflow\/query\/386940?opt.textResults=true&amp;UserId=521799\">(\u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0434\u0435\u0441\u044c)<\/a><\/p>\n<p>  \u0412\u0441\u0435, \u0447\u0442\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438, \u044d\u0442\u043e \u0432\u044b\u0447\u043b\u0438 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 \u0434\u043d\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0434\u0430\u0442\u0443 \u00abgrp\u00bb. \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u0430\u0442\u0430 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430, \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u0442, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00abgrp\u00bb \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0434\u0443\u0449\u0438\u0445 \u0434\u0430\u0442, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0432\u0430 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043d\u044b:<\/p>\n<pre><code class=\"diff\">date2 - date1 = 1 \/\/ \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0434\u043d\u044f\u0445 \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0434\u0430\u0442\u0430\u043c\u0438 rn2 - rn1 = 1 \/\/ \u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0446\u0438\u0444\u0440\u0430\u0445 \u0441\u0442\u0440\u043e\u043a <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043d\u0435\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u0442, \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u043d\u043e\u043c\u0435\u0440\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0436\u0435 1, \u043d\u043e \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0434\u043d\u044f\u0445 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0435\u0434\u0438\u043d\u0438\u0446\u044b. \u0413\u0440\u0443\u043f\u043f\u044b \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0442\u044c:<\/p>\n<pre><code class=\"diff\">rn  grp          date           --- ----------   ---------- 1   2010-11-25   2010-11-26 2   2010-11-25   2010-11-27  3   2010-11-26   2010-11-29 4   2010-11-26   2010-11-30 5   2010-11-26   2010-12-01 6   2010-11-26   2010-12-02 7   2010-11-26   2010-12-03  8   2010-11-27   2010-12-05 9   2010-11-27   2010-12-06 10  2010-11-27   2010-12-07 11  2010-11-27   2010-12-08 12  2010-11-27   2010-12-09  13  2010-11-30   2010-12-13 14  2010-11-30   2010-12-14 <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c:<\/p>\n<pre><code class=\"sql\">WITH     -- This table contains all the distinct date    -- instances in the data set   dates(date) AS (     SELECT DISTINCT CAST(CreationDate AS DATE)     FROM Posts     WHERE OwnerUserId = ##UserId##   ),       -- Generate &quot;groups&quot; of dates by subtracting the   -- date's row number (no gaps) from the date itself   -- (with potential gaps). Whenever there is a gap,   -- there will be a new group   groups AS (     SELECT       ROW_NUMBER() OVER (ORDER BY date) AS rn,       dateadd(day, -ROW_NUMBER() OVER (ORDER BY date), date) AS grp,       date     FROM dates   ) SELECT   COUNT(*) AS consecutiveDates,   MIN(week) AS minDate,   MAX(week) AS maxDate FROM groups GROUP BY grp ORDER BY 1 DESC, 2 DESC <\/code><\/pre>\n<p>  \u0418 \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"diff\">consecutiveDates minDate       maxDate        ---------------- ------------- -------------  14               2012-08-13    2012-08-26 14               2012-02-03    2012-02-16 10               2013-10-24    2013-11-02 10               2011-05-11    2011-05-20 9                2011-06-30    2011-07-08 7                2012-01-17    2012-01-23 7                2011-06-14    2011-06-20 6                2012-04-10    2012-04-15 6                2012-04-02    2012-04-07 6                2012-03-26    2012-03-31 6                2011-10-27    2011-11-01 6                2011-07-17    2011-07-22 6                2011-05-23    2011-05-28 ... <\/code><\/pre>\n<p>  <a href=\"https:\/\/data.stackexchange.com\/stackoverflow\/query\/386940\/all-the-distinct-days-on-which-i-created-posts?opt.textResults=true&amp;UserId=521799\">(\u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0434\u0435\u0441\u044c)<\/a><\/p>\n<h4><b>\u0411\u043e\u043d\u0443\u0441: \u043d\u0430\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043d\u0435\u0434\u0435\u043b\u044c<\/b><\/h4>\n<p>  \u0422\u043e, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0434\u043d\u0438 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430\u0448 \u0432\u044b\u0431\u043e\u0440. \u041c\u044b \u0432\u0437\u044f\u043b\u0438 \u0442\u043e\u0447\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438 \u043e\u043a\u0440\u0443\u0433\u043b\u0438\u043b\u0438 \u0435\u0433\u043e \u0434\u043e \u0434\u043d\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 CAST:<\/p>\n<pre><code class=\"sql\">SELECT DISTINCT CAST(CreationDate AS DATE) <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0437 \u043d\u0435\u0434\u0435\u043b\u044c, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043e\u043a\u0440\u0443\u0433\u043b\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0434\u043e \u043d\u0435\u0434\u0435\u043b\u044c:<\/p>\n<pre><code class=\"sql\">SELECT DISTINCT datepart(year, CreationDate) * 100                + datepart(week, CreationDate) <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0433\u043e\u0434\u0430 \u0438 \u043d\u0435\u0434\u0435\u043b\u0438 \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0447\u0438\u0441\u043b\u0430 \u0442\u0438\u043f\u0430 201503 \u0434\u043b\u044f \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043d\u0435\u0434\u0435\u043b\u0438 2015 \u0433\u043e\u0434\u0430. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"sql\">WITH   weeks(week) AS (     SELECT DISTINCT datepart(year, CreationDate) * 100                    + datepart(week, CreationDate)     FROM Posts     WHERE OwnerUserId = ##UserId##   ),   groups AS (     SELECT       ROW_NUMBER() OVER (ORDER BY week) AS rn,       dateadd(day, -ROW_NUMBER() OVER (ORDER BY week), week) AS grp,       week     FROM weeks   ) SELECT   COUNT(*) AS consecutiveWeeks,   MIN(week) AS minWeek,   MAX(week) AS maxWeek FROM groups GROUP BY grp ORDER BY 1 DESC, 2 DESC <\/code><\/pre>\n<p>  \u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c:<\/p>\n<pre><code class=\"diff\">consecutiveWeeks minWeek maxWeek  ---------------- ------- -------  45               201401  201445   29               201225  201253   25               201114  201138   23               201201  201223   20               201333  201352   16               201529  201544   15               201305  201319   12               201514  201525   12               201142  201153   9                201502  201510   7                201447  201453   7                201321  201327   6                201048  201053   4                201106  201109   3                201329  201331   3                201102  201104   2                201301  201302   2                201111  201112   1                201512  201512   <\/code><\/pre>\n<p>  <a href=\"https:\/\/data.stackexchange.com\/stackoverflow\/query\/386940\/all-the-distinct-days-on-which-i-created-posts?opt.textResults=true&amp;UserId=521799\">(\u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0434\u0435\u0441\u044c)<\/a><\/p>\n<p>  \u041d\u0435\u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043d\u0435\u0434\u0435\u043b\u0438 \u043e\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u0435 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b, \u043a\u0430\u043a \u0430\u0432\u0442\u043e\u0440 \u0447\u0430\u0441\u0442\u043e \u043f\u0438\u0448\u0435\u0442 \u043d\u0430 Stack Overflow.       <\/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\/270573\/\"> http:\/\/habrahabr.ru\/post\/270573\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0442\u043e\u0447\u043d\u0438\u043c, \u0447\u0442\u043e \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u044d\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0451\u043c Stack Overflow. \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043b\u0451\u0432\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0440\u0435\u043f\u0443\u0442\u0430\u0446\u0438\u0438 \u0441 \u043d\u0430\u0433\u0440\u0430\u0434\u0430\u043c\u0438 \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f. \u041a\u0430\u043a \u0438 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u043e\u043d\u0438 \u043f\u043e\u043e\u0449\u0440\u044f\u044e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u043e \u043f\u043e\u0441\u0435\u0449\u0430\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441. \u041e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u0438 \u0434\u0432\u0435 \u043d\u0430\u0433\u0440\u0430\u0434\u044b:<\/p>\n<p>  <a href=\"http:\/\/stackoverflow.com\/help\/badges\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/94b\/f67\/7d5\/94bf677d5854489388e4d7763f0d3549.png\"\/><\/a><\/p>\n<p>  \u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442. \u0417\u0430\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u0430\u0439\u0442 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0434\u0435\u043d\u044c. \u0417\u0430\u0442\u0435\u043c \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0434\u0435\u043d\u044c. \u0417\u0430\u0442\u0435\u043c \u043d\u0430 \u0442\u0440\u0435\u0442\u0438\u0439 (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437, \u044d\u0442\u043e \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f). \u041d\u0435 \u0437\u0430\u0448\u043b\u0438 \u043d\u0430 \u0447\u0435\u0442\u0432\u0451\u0440\u0442\u044b\u0439? \u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u0430\u043d\u043e\u0432\u043e.<\/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-267876","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267876","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=267876"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267876\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}