{"id":1866,"date":"2022-11-29T16:13:58","date_gmt":"2022-11-29T07:13:58","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1866"},"modified":"2022-12-02T13:52:13","modified_gmt":"2022-12-02T04:52:13","slug":"query-monthly-headcount-from-employee-db-snowflake","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2022\/11\/29\/query-monthly-headcount-from-employee-db-snowflake\/","title":{"rendered":"\u5f93\u696d\u54e1 DB \u304b\u3089\u6bce\u6708\u306e\u5728\u7c4d\u4eba\u6570\u3092\u62bd\u51fa\u3059\u308b SQL"},"content":{"rendered":"<p>&nbsp;<br \/>\n\u5f93\u696d\u54e1\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u5165\u793e\u65e5\u3068\u9000\u8077\u65e5\u304c\u5165\u3063\u3066\u308b\u3093\u3060\u3051\u3069\u3001\u305d\u3053\u304b\u3089\u6708\u5225\u306e\u5728\u7c4d\u4eba\u6570\u306e\u30ec\u30dd\u30fc\u30c8\u3092\u4f5c\u3063\u305f\u306e\u3067\u30e1\u30e2\u3002<br \/>\n\u306a\u304a\u3001 DB \u306f Snowflake \u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nset from_date = date('2020-01-01'); \r\nset to_date = current_date();\r\n\r\nwith recursive month as (\r\n    select $from_date as count_date\r\n    union all\r\n    select dateadd(month, 1, count_date)\r\n    from month\r\n    where count_date &lt; $to_date\r\n) \r\n,base as (\r\n    select e.user_id\r\n    ,date_trunc(month, e.join_date) as count_date\r\n    ,e.join_date \r\n    ,e.last_date_worked\r\n    from dim_employee as e\r\n    where e.join_date &lt; $to_date\r\n    order by e.join_date \r\n)\r\nselect \r\n    b.user_id\r\n    ,m.count_date \r\n    ,b.join_date \r\n    ,b.last_date_worked \r\n    ,case\r\n        when m.count_date &lt;= coalesce(b.last_date_worked, current_date()) \r\n        then 1\r\n        else 0\r\n    end as count_employees\r\nfrom month as m\r\ninner join base as b on (m.count_date &gt;= b.count_date)\r\norder by m.count_date\r\n<\/pre>\n<p>set \u3067\u5909\u6570\u5ba3\u8a00\u51fa\u6765\u308b\u306e\u3067\u4fbf\u5229\u306d\u3002<br \/>\n\u3042\u3068 recursive \u3067\u65e5\u4ed8\u3092\u751f\u6210\u51fa\u6765\u308b\u306e\u3082\u4fbf\u5229\u3002<br \/>\n\u203b\u3042\u3068\u3067 dim_date (\u65e5\u4ed8\u30c7\u30a3\u30e1\u30f3\u30b7\u30e7\u30f3) \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u308b\u306e\u3067\u4e00\u6642\u7684\u306a\u5bfe\u7b56\u3002<\/p>\n<p>\u3053\u306e recursive CTE \u3067\u751f\u6210\u3057\u305f\u65e5\u4ed8\u306b\u5bfe\u3057\u3066\u3001\u3042\u3068\u306f dim_employee (\u5f93\u696d\u54e1\u30c7\u30a3\u30e1\u30f3\u30b7\u30e7\u30f3) \u30c6\u30fc\u30d6\u30eb\u306b\u793e\u54e1\u306e JOIN_DATE (\u5165\u793e\u65e5) \u3068 LASTE_DATE_WORKED (\u9000\u8077\u65e5) \u306e\u65e5\u4ed8\u3092\u6bd4\u8f03\u3059\u308b\u3002<br \/>\n\u3053\u308c\u3067\u6bce\u67081\u65e5\u6642\u70b9\u3067\u306e fct_headcount_snapshot (\u5728\u7c4d\u793e\u54e1\u30b9\u30ca\u30c3\u30d7\u30b7\u30e7\u30c3\u30c8) \u3068\u3044\u3046\u30d5\u30a1\u30af\u30c8\u30c6\u30fc\u30d6\u30eb \u3092\u4f5c\u3063\u305f\u3002<br \/>\n\u3055\u3089\u306b\u3053\u308c\u306b\u5bfe\u3057\u3066\u3001\u4e0b\u8a18\u306e\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3057\u3066\u30c7\u30fc\u30bf\u30de\u30fc\u30c8\u306b\u3057\u3066\u3042\u3052\u308c\u3070\u5b8c\u6210\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nselect \r\n    count_date\r\n    ,count(distinct user_id) as num_employee\r\nfrom fct_headcount_snapshot\r\ngroup by count_date\r\norder by count_date\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u5f93\u696d\u54e1\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u5165\u793e\u65e5\u3068\u9000\u8077\u65e5\u304c\u5165\u3063\u3066\u308b\u3093\u3060\u3051\u3069\u3001\u305d\u3053\u304b\u3089\u6708\u5225\u306e\u5728\u7c4d\u4eba\u6570\u306e\u30ec\u30dd\u30fc\u30c8\u3092\u4f5c\u3063\u305f\u306e\u3067\u30e1\u30e2\u3002 \u306a\u304a\u3001 DB \u306f Snowflake \u3067\u3042\u308b\u3002 set from_date = date(&#8216;2020 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,20],"tags":[],"class_list":["post-1866","post","type-post","status-publish","format-standard","hentry","category-snowflakedb","category-sql"],"_links":{"self":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1866","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=1866"}],"version-history":[{"count":3,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1866\/revisions"}],"predecessor-version":[{"id":1872,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1866\/revisions\/1872"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}