{"id":1725,"date":"2021-11-15T12:15:14","date_gmt":"2021-11-15T03:15:14","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1725"},"modified":"2021-11-16T11:26:05","modified_gmt":"2021-11-16T02:26:05","slug":"tsql-to-query-user-age-group-data","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2021\/11\/15\/tsql-to-query-user-age-group-data\/","title":{"rendered":"T-SQL \u3067\u30e6\u30fc\u30b6\u30fc\u5e74\u9f62\u533a\u5206\u5225\u306e\u30c7\u30fc\u30bf\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\/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\u30e6\u30fc\u30b6\u30fc\u306e\u5e74\u9f62\u533a\u5206\u5225\u306b\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3059\u308b\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\ndeclare @startdate date = '2013-01-01';\r\ndeclare @enddate date = '2013-12-31';\r\nwith t1 as (\r\n    SELECT\r\n        s.&#x5B;ProductKey],\r\n        s.&#x5B;CustomerKey],\r\n\t\ts.&#x5B;OrderDate],\r\n\t\ts.&#x5B;OrderDateKey],\r\n\t\tc.&#x5B;Gender],\r\n\t\tc.&#x5B;BirthDate],\r\n\t\tconvert(int, format(c.&#x5B;BirthDate], 'yyyyMMdd')) as i_birthdate,\r\n\t\tfloor((s.&#x5B;OrderDateKey] - convert(int, format(c.&#x5B;BirthDate], 'yyyyMMdd'))) \/ 10000) as age,\r\n\t\tc.&#x5B;DateFirstPurchase],\r\n\t\tc.&#x5B;GeographyKey],\r\n\t\tg.&#x5B;CountryRegionCode],\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\t\tinner join &#x5B;dbo].&#x5B;DimCustomer] as c on s.&#x5B;CustomerKey] = c.&#x5B;CustomerKey]\r\n\t\tinner join &#x5B;dbo].&#x5B;DimGeography] as g on c.&#x5B;GeographyKey] = g.&#x5B;GeographyKey]\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 @startdate and @enddate\r\n),\r\nt2 as (\r\n    select\r\n        t1.&#x5B;SalesOrderNumber],\r\n        t1.&#x5B;CustomerKey],\r\n        t1.&#x5B;Gender],\r\n        t1.&#x5B;age],\r\n        concat(\r\n            case\r\n                when t1.&#x5B;age] &gt;= 20 then t1.&#x5B;gender]\r\n                else ''\r\n            end,\r\n            case\r\n                when t1.&#x5B;age] between 4 and 12 then 'C'\r\n                when t1.&#x5B;age] between 13 and 19 then 'T'\r\n                when t1.&#x5B;age] between 20 and 34 then '1'\r\n                when t1.&#x5B;age] between 35 and 49 then '2'\r\n                when t1.&#x5B;age] &gt;= 50 then '3'\r\n            end\r\n        ) as agegroup,\r\n        t1.&#x5B;subcategory]\r\n    from\r\n        t1\r\n)\r\nselect\r\n    t2.&#x5B;subcategory],\r\n    t2.&#x5B;agegroup],\r\n    count(distinct SalesOrderNumber) as order_cnt\r\nfrom\r\n    t2\r\ngroup by\r\n    t2.&#x5B;subcategory],\r\n    t2.&#x5B;agegroup]\r\norder by\r\n    t2.&#x5B;subcategory],\r\n    t2.&#x5B;agegroup];\r\n<\/pre>\n<p>&nbsp;<br \/>\n\u7d50\u679c\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1728\" style=\"width: 285px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1728\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-15.png\" alt=\"\" width=\"275\" height=\"455\" class=\"size-full wp-image-1728\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-15.png 275w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-15-181x300.png 181w\" sizes=\"auto, (max-width: 275px) 100vw, 275px\" \/><p id=\"caption-attachment-1728\" class=\"wp-caption-text\">\u5546\u54c1\u3068\u5e74\u9f62\u533a\u5206\u306e\u96c6\u8a08<\/p><\/div>\n<p>&nbsp;<br \/>\nPowerBI \u3067\u8868\u793a\u3059\u308b\u3068\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1729\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1729\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-15-1024x409.png\" alt=\"\" width=\"625\" height=\"250\" class=\"size-large wp-image-1729\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-15-1024x409.png 1024w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-15-300x120.png 300w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-15-768x306.png 768w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-15-624x249.png 624w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-15.png 1258w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-1729\" class=\"wp-caption-text\">PowerBI \u3067\u30e6\u30fc\u30b6\u30fc\u5e74\u9f62\u533a\u5206\u6bce\u306b\u5546\u54c1\u58f2\u4e0a\u3092\u53ef\u8996\u5316<\/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-1725","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\/1725","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=1725"}],"version-history":[{"count":5,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1725\/revisions"}],"predecessor-version":[{"id":1738,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1725\/revisions\/1738"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}