{"id":509,"date":"2019-12-02T17:07:15","date_gmt":"2019-12-02T08:07:15","guid":{"rendered":"http:\/\/www.fatware.jp\/?page_id=509"},"modified":"2019-12-02T17:07:33","modified_gmt":"2019-12-02T08:07:33","slug":"sql-crosstab-query-for-jp-fiscal-year","status":"publish","type":"page","link":"https:\/\/www.fatware.jp\/?page_id=509","title":{"rendered":"[SQL] Crosstab query for JP Fiscal Year"},"content":{"rendered":"\n<p>In this example, cross-tabulation, or crosstab, on financial accounting of a company is considered.On financial accounting of a company, journal voucher is a unit that manages financial statement of a company.<\/p>\n\n\n\n<p>In SQL, journal voucher can be considered as a table entry. That is , journal voucher table is composed of following columns; &#8220;<em>ID&#8221;, &#8220;name of an account item&#8221;, &#8220;price of the item&#8221; and &#8220;transaction date&#8221;<\/em>. Journal voucher table may contain a flag or type column that shows an item is recorded as credit, deposit, payment, and so on. The structure of journal voucher table can be figured as follow;<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>ID<\/th><th>Date<\/th><th>Price<\/th><th>Item<\/th><th>Type<\/th><\/tr><\/thead><\/table>\n\n\n\n<p>For example,\u00a0 an entry of the above journal voucher table is recorded as <strong>&#8220;10,\u00a02019-04-01, 1,000 JPY, traveling fee, expense payment&#8221;<\/strong> , that means <strong>&#8220;Voucher ID 10: traveling fee\u00a0 1,000 JPY was paid as expense payment in April 1st in 2019&#8221;<\/strong>.<\/p>\n\n\n\n<p>Cross-tabulation, or crosstab query can calculate monthly total from such journal voucher table. In other words, crosstab query of monthly total converts a table such as<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>ID<\/th><th>Date<\/th><th>Price<\/th><th>Item<\/th><th>Type<\/th><\/tr><\/thead><\/table>\n\n\n\n<p>to another table like<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>Item<\/th><th>April<\/th><th>May<\/th><th>June<\/th><th>&#8230;<\/th><th>March<\/th><\/tr><\/thead><\/table>\n\n\n\n<h1 class=\"wp-block-heading\">Crosstab query<\/h1>\n\n\n\n<p>Crosstab query of monthly total in an JP fiscal year can be described as follow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select item, <br>    sum(case when m='04' then price else 0 end) as April, <br>    sum(case when m='05' then price else 0 end) as May, <br>    sum(case when m='06' then price else 0 end) as June, <br>    sum(case when m='07' then price else 0 end) as July, <br>    sum(case when m='08' then price else 0 end) as August, <br>    sum(case when m='09' then price else 0 end) as September, <br>    sum(case when m='10' then price else 0 end) as October, <br>    sum(case when m='11' then price else 0 end) as November, <br>    sum(case when m='12' then price else 0 end) as December, <br>    sum(case when m='01' then price else 0 end) as January, <br>    sum(case when m='02' then price else 0 end) as February, <br>    sum(case when m='03' then price else 0 end) as June, <br>    sum(price) as price<br>from<br>(<br>  SELECT<br>   (<br>     case when<br>       month(t.date) &lt; 4<br>     then<br>       year(t.date)-1<br>     else<br>       year(t.date) end<br>   ) as jpfiscal,<br>   month(t.date) as m,<br>   t.price,<br>   t.item<br>  FROM<br>   vhrtransfer as t<br>  where<br>   t.isCredit=0<br>   having jpfiscal=2014<br>   order by year(t.date),m<br> ) as s<br><br>group by item;<\/pre>\n\n\n\n<p>The procedure of this crosstab query is<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The sub query labeled &#8220;s&#8221; generates a table of JP fiscal year 2014 from &#8220;vhrtransfer&#8221; table, where each account item is not labeled as credit.<\/li><li>&#8220;Group&#8221; by item, then, all columns of same account item are grouped in sub query &#8220;s&#8221;.<\/li><li>At last, the main query divides grouped items into each month column by case sentences.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"head2\">Detail of Main \/ Sub queries<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Sub query 1:&nbsp; pulling data of the target JP Fiscal year from a sorted list<\/h3>\n\n\n\n<p>In Japan, fiscal year is composed of 2Q, 3Q, 4Q and 1Q of next year. The SELECT query labeled &#8220;jpfiscal&#8221; can be written by &#8220;case&#8221; and &#8220;having&#8221;, that <br>pulls data of a target JP fiscal year from a sorted list ;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select<br>  (<br>   case when<br>     month(datecol) &lt; 4<br>   then<br>     year(datecol)-1<br>   else<br>     year(datecol) end<br> ) as jpfiscal<br>from<br> sortedlist<br>having jpfiscal=2014<\/pre>\n\n\n\n<p>The format of &#8220;case&#8221; sentence is as follow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">case when <strong>condition<\/strong> then <strong>action1<\/strong> else <strong>action2<\/strong><\/pre>\n\n\n\n<p>If condition is True, action1 is extecuted, else, action2 is executed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sub query 2:&nbsp; pulling data of the target JP fiscal year from an original accounting table.<\/h3>\n\n\n\n<p>Suppose, &#8220;vhrtransfer&#8221; table contains columns as follow:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>ID<\/td><td>isCredit&nbsp;<br>(flag of credit)<\/td><td>price<br>(price of item)<\/td><td>date<br>(transaction date)<\/td><td>item<br>(account item)<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>&#8220;vhrtransfer&#8221; table contains &#8220;isCredit&#8221; column. If the isCredit flag is set, the account item is recorded as credit. <\/p>\n\n\n\n<p>A sorted list for jpfiscal sub query should be created from &#8220;vhrtransfer&#8221; table by pulling such entries whose isCredit column is set as 0. The SQL query for creating &#8220;sortedlist&#8221; is as follow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  FROM<br>   vhrtransfer as t<br>  where<br>   t.isCredit=0<br>   having jpfiscal=2014<br>   order by year(t.date),m<br><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Main query: crosstab output of monthly total<\/h3>\n\n\n\n<p>Here, the table created by jpfiscal sub query would be calculated and sorted as a crosstab table. The image of crosstab table is as follow:&nbsp;<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>item<\/th><th>April<br>2014<\/th><th>May<br>2014<\/th><td><strong>June<\/strong><br><strong>2014<\/strong><\/td><td>&#8230;<\/td><td><strong>February<\/strong><br><strong>2015<\/strong><\/td><th>March<br>2015<\/th><\/tr><\/thead><\/table>\n\n\n\n<p>Pseudo SQL query for crosstab output of monthly total can be written as follow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select  item,<br>\u3000sum in April,<br>\u3000sum in May,<br>\u3000\u3000\u3000\uff1a<br>\u3000sum in March<br>from<br>  table<\/pre>\n\n\n\n<p>Thus, the sum of each month should be calculated from each entry of the table generated by sub queries.<\/p>\n\n\n\n<p>As an example of the case sentence of each month, the query for sum in April would be described as follow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp; sum(case when m='04' then price else 0 end) as April,<\/pre>\n\n\n\n<p>this sum calculation query means:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>When the timestamp of an entry is in April, calculate sum of prices on the entry and use the sum of prices as a column, that is labeled as April. If the month of the timestamp is not April, add 0.&nbsp; &nbsp;<\/p><\/blockquote>\n\n\n\n<p>Crosstab output query of monthly total would be made by writing such sum calculation query for each month.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, cross-tabulation, or cr <a href=\"https:\/\/www.fatware.jp\/?page_id=509\" class=\"read-more\">\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":363,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-509","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/pages\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fatware.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=509"}],"version-history":[{"count":10,"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/pages\/509\/revisions"}],"predecessor-version":[{"id":523,"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/pages\/509\/revisions\/523"}],"up":[{"embeddable":true,"href":"https:\/\/www.fatware.jp\/index.php?rest_route=\/wp\/v2\/pages\/363"}],"wp:attachment":[{"href":"https:\/\/www.fatware.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}