{"id":1731,"date":"2021-11-16T13:54:07","date_gmt":"2021-11-16T04:54:07","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1731"},"modified":"2021-11-16T13:54:07","modified_gmt":"2021-11-16T04:54:07","slug":"tsql-to-query-prod-category-sold-for-venn-diagram","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2021\/11\/16\/tsql-to-query-prod-category-sold-for-venn-diagram\/","title":{"rendered":"T-SQL \u3067\u30e6\u30fc\u30b6\u30fc\u306e\u8cfc\u5165\u30ab\u30c6\u30b4\u30ea\u3092\u96c6\u8a08\u3057\u3066\u30d9\u30f3\u56f3\u306b\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\u30e6\u30fc\u30b6\u30fc\u306e\u8cfc\u5165\u30ab\u30c6\u30b4\u30ea\u3092\u96c6\u8a08\u3057\u3066\u30d9\u30f3\u56f3\u306b\u3059\u308b\u305f\u3081\u306e\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\tc.&#x5B;CustomerAlternateKey],\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\tinner join &#x5B;dbo].&#x5B;DimCustomer] as c on s.&#x5B;CustomerKey] = c.&#x5B;CustomerKey]\r\n\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;CustomerAlternateKey],\r\n        sign(sum(case when t1.&#x5B;category] = 'Bikes' then 1 else 0 end)) as bikes,\r\n        sign(sum(case when t1.&#x5B;category] = 'Clothing' then 1 else 0 end)) as clothing,\r\n        sign(sum(case when t1.&#x5B;category] = 'Accessories' then 1 else 0 end)) as accessories\r\n    from\r\n        t1\r\n    group by\r\n        t1.&#x5B;CustomerAlternateKey]\r\n),\r\nt3 as (\r\n    select\r\n        t2.&#x5B;bikes],\r\n        t2.&#x5B;clothing],\r\n        t2.&#x5B;accessories],\r\n        count(1) as cnt\r\n    from\r\n        t2\r\n    group by\r\n        rollup(t2.&#x5B;bikes], t2.&#x5B;clothing], t2.&#x5B;accessories])\r\n)\r\nselect\r\n    t3.&#x5B;bikes],\r\n    t3.&#x5B;clothing],\r\n    t3.&#x5B;accessories],\r\n    t3.cnt\r\nfrom\r\n    t3\r\nwhere\r\n    t3.&#x5B;bikes] is not null\r\n    and t3.&#x5B;clothing] is not null\r\n    and t3.&#x5B;accessories] is not null;\r\n<\/pre>\n<p>&nbsp;<br \/>\n\u7d50\u679c\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<div id=\"attachment_1740\" style=\"width: 265px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1740\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/tsql-2021-11-16.png\" alt=\"\" width=\"255\" height=\"189\" class=\"size-full wp-image-1740\" \/><p id=\"caption-attachment-1740\" class=\"wp-caption-text\">T-SQL \u3067\u30d9\u30f3\u56f3\u7528\u30c7\u30fc\u30bf\u3092\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_1741\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1741\" src=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-16-1024x581.png\" alt=\"\" width=\"625\" height=\"355\" class=\"size-large wp-image-1741\" srcset=\"https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-16-1024x581.png 1024w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-16-300x170.png 300w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-16-768x435.png 768w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-16-624x354.png 624w, https:\/\/www.kwonline.org\/memo2\/wp-content\/uploads\/2021\/11\/pbi-2021-11-16.png 1134w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-1741\" class=\"wp-caption-text\">PowerBI \u3067\u30d9\u30f3\u56f3\u3092\u8868\u793a<\/p><\/div>\n<p>\u30d9\u30f3\u56f3\u30d3\u30b8\u30e5\u30a2\u30eb\u306f Venn Diagram by MAQ Software \u3092\u8ffd\u52a0\u3057\u3066\u5229\u7528\u3057\u305f\u3002<\/p>\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-1731","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\/1731","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=1731"}],"version-history":[{"count":3,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1731\/revisions"}],"predecessor-version":[{"id":1743,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1731\/revisions\/1743"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}