<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="calculating-days">
      </a>
      5.6.8 Calculating Visits Per Day
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045324604464">
  </a>
  <a class="indexterm" name="idm46045324603424">
  </a>
  <a class="indexterm" name="idm46045324602352">
  </a>
  <a class="indexterm" name="idm46045324601264">
  </a>
  <p>
   The following example shows how you can use the bit group
        functions to calculate the number of days per month a user has
        visited a Web page.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa19074488"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1 <span class="token punctuation">(</span><span class="token datatype">year</span> <span class="token datatype">YEAR</span><span class="token punctuation">,</span> <span class="token keyword">month</span> <span class="token datatype">INT</span> <span class="token keyword">UNSIGNED</span><span class="token punctuation">,</span>
             <span class="token keyword">day</span> <span class="token datatype">INT</span> <span class="token keyword">UNSIGNED</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t1 <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   The example table contains year-month-day values representing
        visits by users to the page. To determine how many different
        days in each month these visits occur, use this query:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa12472084"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token datatype">year</span><span class="token punctuation">,</span><span class="token keyword">month</span><span class="token punctuation">,</span><span class="token function">BIT_COUNT</span><span class="token punctuation">(</span><span class="token function">BIT_OR</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span><span class="token keyword">day</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> days <span class="token keyword">FROM</span> t1
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> <span class="token datatype">year</span><span class="token punctuation">,</span><span class="token keyword">month</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Which returns:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-none"><div class="docs-select-all right" id="sa3768721"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-none">+------+-------+------+
| year | month | days |
+------+-------+------+
| 2000 |     1 |    3 |
| 2000 |     2 |    2 |
+------+-------+------+</code></pre>
  </div>
  <p>
   The query calculates how many different days appear in the table
        for each year/month combination, with automatic removal of
        duplicate entries.
  </p>
 </div>
 <br/>
</div>
