{"id":1708,"date":"2021-11-11T11:11:18","date_gmt":"2021-11-11T02:11:18","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1708"},"modified":"2021-11-16T11:22:55","modified_gmt":"2021-11-16T02:22:55","slug":"tsql-to-query-data-tarnsition-history-for-fanchart","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2021\/11\/11\/tsql-to-query-data-tarnsition-history-for-fanchart\/","title":{"rendered":"T-SQL \u3067\u30d5\u30a1\u30f3\u30c1\u30e3\u30fc\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u63a8\u79fb\u3092\u62bd\u51fa\u3059\u308b"},"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\/adventureworks\" rel=\"noopener\" target=\"_blank\">AdventureWorksDW2019<\/a> \u30b9\u30ad\u30fc\u30de\u3092\u4f7f\u3063\u3066 T-SQL \u3092\u899a\u3048\u308b\u30e1\u30e2\u3002<\/p>\n<p>\u30d5\u30a1\u30af\u30c8\u30c6\u30fc\u30d6\u30eb\u306e dbo.FactInternetSales \u3068\u95a2\u9023\u30c7\u30a3\u30e1\u30f3\u30b7\u30e7\u30f3\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3066\u3001\u88fd\u54c1\u5225\u306e\u58f2\u4e0a\u63a8\u79fb\u3092\u30d5\u30a1\u30f3\u30c1\u30e3\u30fc\u30c8\u3067\u8868\u793a\u3059\u308b\u305f\u3081\u306e\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3059\u308b\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nwith t1 as (\r\n    SELECT\r\n        s.&#x5B;ProductKey],\r\n        s.&#x5B;CustomerKey],\r\n        d.&#x5B;FullDateAlternateKey],\r\n        convert(varchar(7), format(d.&#x5B;FullDateAlternateKey], 'yyyy-MM')) as year_month,\r\n        s.&#x5B;SalesOrderNumber],\r\n        s.&#x5B;SalesOrderLineNumber],\r\n        s.&#x5B;OrderQuantity] * s.&#x5B;UnitPrice] as revenue,\r\n        pc.&#x5B;EnglishProductCategoryName] as category,\r\n        psc.&#x5B;EnglishProductSubcategoryName] as subcategory,\r\n        p.&#x5B;EnglishProductName] as productname\r\n    FROM\r\n        &#x5B;dbo].&#x5B;FactInternetSales] as s\r\n        inner join &#x5B;dbo].&#x5B;DimDate] as d on s.&#x5B;OrderDateKey] = d.&#x5B;DateKey]\r\n        inner join &#x5B;dbo].&#x5B;DimProduct] as p on s.&#x5B;ProductKey] = p.&#x5B;ProductKey]\r\n        left join &#x5B;dbo].&#x5B;DimProductSubcategory] as psc on p.&#x5B;ProductSubcategoryKey] = psc.&#x5B;ProductSubcategoryKey]\r\n        left join &#x5B;dbo].&#x5B;DimProductCategory] as pc on psc.&#x5B;ProductCategoryKey] = pc.&#x5B;ProductCategoryKey]\r\n    where\r\n        1 = 1\r\n        and s.&#x5B;OrderDate] between '2013-01-01' and '2013-12-31'\r\n),\r\nt2 as (\r\n    select\r\n        t1.&#x5B;year_month],\r\n        t1.&#x5B;category],\r\n        sum(t1.&#x5B;revenue]) as revenue\r\n    from\r\n        t1\r\n    group by\r\n        t1.&#x5B;year_month],\r\n        t1.&#x5B;category]\r\n)\r\nselect\r\n    t2.&#x5B;year_month],\r\n    t2.&#x5B;category],\r\n    t2.&#x5B;revenue],\r\n    first_value(t2.&#x5B;revenue]) over(partition by t2.&#x5B;category] order by t2.&#x5B;year_month]) as base,\r\n    100.0 * t2.&#x5B;revenue] \/ first_value(t2.&#x5B;revenue]) over(partition by t2.&#x5B;category] order by t2.&#x5B;year_month]) as rate\r\nfrom\r\n    t2\r\norder by\r\n    t2.&#x5B;year_month];\r\n<\/pre>\n<p>&nbsp;<br \/>\n\u7d50\u679c\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1713\" style=\"width: 465px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1713\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-11.png\" alt=\"\" width=\"455\" height=\"397\" class=\"size-full wp-image-1713\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-11.png 455w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-11-300x262.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><p id=\"caption-attachment-1713\" class=\"wp-caption-text\">T-SQL \u3067\u58f2\u4e0a\u30c7\u30fc\u30bf\u306e\u63a8\u79fb\u3092\u62bd\u51fa<\/p><\/div>\n<p>&nbsp;<br \/>\nPowerBI \u3067\u8868\u793a\u3059\u308b\u3068\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1714\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1714\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-11-1024x426.png\" alt=\"\" width=\"625\" height=\"260\" class=\"size-large wp-image-1714\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-11-1024x426.png 1024w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-11-300x125.png 300w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-11-768x320.png 768w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-11-624x260.png 624w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-11.png 1328w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-1714\" class=\"wp-caption-text\">PowerBI \u306e\u7dda\u30b0\u30e9\u30d5\u3067\u30d5\u30a1\u30f3\u30c1\u30e3\u30fc\u30c8\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 AdventureWorksDW2019 \u30b9\u30ad\u30fc\u30de\u3092\u4f7f\u3063\u3066 T-SQL \u3092\u899a\u3048\u308b\u30e1\u30e2\u3002 \u30d5\u30a1\u30af\u30c8\u30c6\u30fc\u30d6\u30eb\u306e dbo.FactInternetSales \u3068\u95a2\u9023\u30c7 [&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-1708","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\/1708","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=1708"}],"version-history":[{"count":6,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1708\/revisions"}],"predecessor-version":[{"id":1735,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1708\/revisions\/1735"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}