{"id":1718,"date":"2021-11-12T09:16:32","date_gmt":"2021-11-12T00:16:32","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1718"},"modified":"2021-11-16T11:23:09","modified_gmt":"2021-11-16T02:23:09","slug":"t-sql-%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e5%88%86%e5%b8%83%e3%82%92%e6%8a%bd%e5%87%ba%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2021\/11\/12\/t-sql-%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e5%88%86%e5%b8%83%e3%82%92%e6%8a%bd%e5%87%ba%e3%81%99%e3%82%8b\/","title":{"rendered":"T-SQL \u3067\u30c7\u30fc\u30bf\u5206\u5e03\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\u8cfc\u5165\u91d1\u984d\u306e\u30c7\u30fc\u30bf\u5206\u5e03(\u30d2\u30b9\u30c8\u30b0\u30e9\u30e0)\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        2500.0 as revenue_max,\r\n        0.0 as revenue_min,\r\n        2500.0 as revenue_range,\r\n        5 as buckets\r\n),\r\nt3 as (\r\n    select\r\n        t1.&#x5B;revenue],\r\n        t2.&#x5B;revenue_min],\r\n        t1.&#x5B;revenue] - t2.&#x5B;revenue_min] as revenu_diff,\r\n        1.0 * t2.&#x5B;revenue_range] \/ t2.&#x5B;buckets] as bucket_range,\r\n        floor(\r\n            1.0 * (t1.&#x5B;revenue] - t2.&#x5B;revenue_min]) \/ (1.0 * t2.&#x5B;revenue_range] \/ t2.&#x5B;buckets])\r\n        ) + 1 as bucket\r\n    from\r\n        t1,\r\n        t2\r\n)\r\nselect\r\n    t3.&#x5B;bucket],\r\n    t3.&#x5B;revenue_min] + t3.&#x5B;bucket_range] * (t3.&#x5B;bucket] - 1) as lower_limit,\r\n    t3.&#x5B;revenue_min] + t3.&#x5B;bucket_range] * t3.&#x5B;bucket] as upper_limit,\r\n    count(t3.&#x5B;revenue]) as order_cnt,\r\n    sum(t3.&#x5B;revenue]) as total_revenue\r\nfrom\r\n    t3\r\ngroup by\r\n    t3.&#x5B;bucket],\r\n    t3.&#x5B;revenue_min],\r\n    t3.&#x5B;bucket_range]\r\norder by\r\n    t3.&#x5B;bucket];\r\n<\/pre>\n<p>&nbsp;<br \/>\n\u7d50\u679c\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1720\" style=\"width: 401px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1720\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-12.png\" alt=\"\" width=\"391\" height=\"148\" class=\"size-full wp-image-1720\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-12.png 391w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-12-300x114.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><p id=\"caption-attachment-1720\" class=\"wp-caption-text\">T-SQL \u3067\u30c7\u30fc\u30bf\u5206\u5e03\u3092\u62bd\u51fa<\/p><\/div>\n<p>&nbsp;<br \/>\nPowerBI \u3067\u8868\u793a\u3059\u308b\u3068\u3053\u3046\u306a\u308b\u3002<br \/>\nX \u8ef8\u306e\u300c\u5185\u5074\u306e\u30d1\u30c7\u30a3\u30f3\u30b0\u300d\u306f 0 px \u306b\u3057\u3066\u307e\u3059\u3002<\/p>\n<div id=\"attachment_1721\" style=\"width: 494px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1721\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-12.png\" alt=\"\" width=\"484\" height=\"327\" class=\"size-full wp-image-1721\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-12.png 484w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-12-300x203.png 300w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><p id=\"caption-attachment-1721\" class=\"wp-caption-text\">PowerBI \u3067\u30c7\u30fc\u30bf\u5206\u5e03(\u30d2\u30b9\u30c8\u30b0\u30e9\u30e0)\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 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-1718","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\/1718","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=1718"}],"version-history":[{"count":5,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1718\/revisions"}],"predecessor-version":[{"id":1736,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1718\/revisions\/1736"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}