{"id":1675,"date":"2021-11-05T12:30:02","date_gmt":"2021-11-05T03:30:02","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1675"},"modified":"2021-11-06T14:22:53","modified_gmt":"2021-11-06T05:22:53","slug":"tsql-to-query-monthly-sales-compared-to-last-year","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2021\/11\/05\/tsql-to-query-monthly-sales-compared-to-last-year\/","title":{"rendered":"T-SQL \u3067\u6708\u5225\u53d7\u6ce8\u306e\u6628\u5bfe\u6bd4\u3092\u62bd\u51fa"},"content":{"rendered":"<p>&nbsp;<br \/>\nMS SQL Server \u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u306e <a href=\"https:\/\/github.com\/Microsoft\/sql-server-samples\/releases\/tag\/wide-world-importers-v1.0\" rel=\"noopener\" target=\"_blank\">WideWorldImportersDW<\/a> \u30b9\u30ad\u30fc\u30de\u3092\u4f7f\u3063\u3066 T-SQL \u3092\u899a\u3048\u308b\u30e1\u30e2\u3002<\/p>\n<p>Fact.Sale \u3068 Dimension.Date \u3092\u4f7f\u3063\u3066\u304b\u3089\u6708\u9593\u53d7\u6ce8\u306e\u6628\u5bfe\u6bd4\u3092\u62bd\u51fa\u3059\u308b\u30af\u30a8\u30ea<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nDECLARE @end_date date;\r\nSELECT\r\n    @end_date = EOMONTH('2014-12-01');\r\n\r\nwith t1 as (\r\n    SELECT\r\n        s.&#x5B;Invoice Date Key] as sale_date,\r\n        d.&#x5B;Calendar Month Number] as sale_month,\r\n        d.&#x5B;Calendar Year] as sale_year,\r\n        sum(s.&#x5B;Total Including Tax]) as total_sale\r\n    FROM\r\n        &#x5B;WideWorldImportersDW].&#x5B;Fact].&#x5B;Sale] as s\r\n        inner join &#x5B;Dimension].&#x5B;Date] as d on s.&#x5B;Invoice Date Key] = d.&#x5B;Date]\r\n    where\r\n        s.&#x5B;Invoice Date Key] between '2013-01-01' and @end_date\r\n    group by\r\n        s.&#x5B;Invoice Date Key],\r\n        d.&#x5B;Calendar Month Number],\r\n        d.&#x5B;Calendar Year]\r\n)\r\nselect\r\n    t1.&#x5B;sale_month],\r\n    sum(\r\n        case t1.&#x5B;sale_year] when 2013 then t1.&#x5B;total_sale] end\r\n    ) as sale_2013,\r\n    sum(\r\n        case t1.&#x5B;sale_year] when 2014 then t1.&#x5B;total_sale] end\r\n    ) as sale_2014,\r\n    100.0 \r\n    * sum(\r\n        case t1.&#x5B;sale_year] when 2014 then t1.&#x5B;total_sale] end\r\n    )\r\n    \/ sum(\r\n        case t1.&#x5B;sale_year] when 2013 then t1.&#x5B;total_sale] end\r\n    ) as yoy\r\nfrom\r\n    t1\r\ngroup by\r\n    t1.&#x5B;sale_month]\r\norder by\r\n    t1.&#x5B;sale_month];\r\n<\/pre>\n<p>&nbsp;<br \/>\n\u7d50\u679c\u306f\u3053\u3046\u306a\u308b\u3002<br \/>\nyoy \u306f Year-over-Year \u306e\u4e8b\u3002\u3064\u307e\u308a\u6628\u5e74\u5bfe\u6bd4\u3002<\/p>\n<div id=\"attachment_1676\" style=\"width: 356px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1676\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-05.png\" alt=\"\" width=\"346\" height=\"282\" class=\"size-full wp-image-1676\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-05.png 346w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-05-300x245.png 300w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><p id=\"caption-attachment-1676\" class=\"wp-caption-text\">\u6708\u5225\u306e\u6628\u5bfe\u6bd4<\/p><\/div>\n<p>&nbsp;<br \/>\nPowerBI \u3067\u8868\u793a\u3059\u308b\u3068\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1677\" style=\"width: 760px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1677\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-05.png\" alt=\"\" width=\"750\" height=\"398\" class=\"size-full wp-image-1677\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-05.png 750w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-05-300x159.png 300w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-05-624x331.png 624w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><p id=\"caption-attachment-1677\" class=\"wp-caption-text\">PowerBI \u3067\u6628\u5bfe\u6bd4\u3092\u8868\u793a<\/p><\/div>\n<p>&nbsp;<\/p>\n<h3>\u53c2\u8003\u56f3\u66f8<\/h3>\n<p><a href=\"https:\/\/amzn.to\/3bLFEpv\" rel=\"noopener\" target=\"_blank\">\u30d3\u30c3\u30b0\u30c7\u30fc\u30bf\u5206\u6790\u30fb\u6d3b\u7528\u306e\u305f\u3081\u306eSQL\u30ec\u30b7\u30d4<\/a><br \/>\n<a href=\"https:\/\/amzn.to\/3nX2FLU\" rel=\"noopener\" target=\"_blank\">SQL Server 2016\u306e\u6559\u79d1\u66f8 \u958b\u767a\u7de8<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; MS SQL Server \u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u306e WideWorldImportersDW \u30b9\u30ad\u30fc\u30de\u3092\u4f7f\u3063\u3066 T-SQL \u3092\u899a\u3048\u308b\u30e1\u30e2\u3002 Fact.Sale \u3068 Dimension.Date \u3092\u4f7f\u3063\u3066\u304b\u3089\u6708\u9593\u53d7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,20,23],"tags":[],"class_list":["post-1675","post","type-post","status-publish","format-standard","hentry","category-azure","category-sql","category-t-sql"],"_links":{"self":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/comments?post=1675"}],"version-history":[{"count":4,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1675\/revisions"}],"predecessor-version":[{"id":1685,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1675\/revisions\/1685"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}