<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>SQL on Deep Blue</title>
    <link>https://blog.dustbreak.com/tags/sql/</link>
    <description>Recent content in SQL on Deep Blue</description>
    <generator>Hugo -- gohugo.io</generator>
    <lastBuildDate>Wed, 14 Apr 2021 16:17:51 +0800</lastBuildDate><atom:link href="https://blog.dustbreak.com/tags/sql/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>SQL in BigQuery 1</title>
      <link>https://blog.dustbreak.com/posts/sql_in_bigquery_1/</link>
      <pubDate>Wed, 14 Apr 2021 16:17:51 +0800</pubDate>
      
      <guid>https://blog.dustbreak.com/posts/sql_in_bigquery_1/</guid>
      <description>1. How to handle duplicate column names on left join A SELECT * EXCEPT statement specifies the names of one or more columns to exclude from the result set. All matching column names are omitted from the output.
SELECT t1.* EXCEPT (name), t2.* EXCEPT (name,account_id), t1.name AS deployment_name, t2.name AS account_name FROM table1 t1 LEFT JOIN table2 t2 ON t1.name = t2.name 2. Extract date from timestamp Returns a value that corresponds to the specified part from a supplied timestamp_expression.</description>
      <content:encoded><![CDATA[<h2 id="1-how-to-handle-duplicate-column-names-on-left-join">1. How to handle duplicate column names on left join</h2>
<p>A SELECT * EXCEPT statement specifies the names of one or more columns to exclude from the result set. All matching column names are omitted from the output.</p>
<pre tabindex="0"><code>SELECT 
	t1.* EXCEPT (name),
	t2.* EXCEPT (name,account_id),
	t1.name AS deployment_name,
	t2.name AS account_name
FROM
	table1 t1
LEFT JOIN
	table2 t2
ON t1.name = t2.name
</code></pre><h2 id="2-extract-date-from-timestamp">2. Extract date from timestamp</h2>
<p>Returns a value that corresponds to the specified <code>part</code> from a supplied <code>timestamp_expression</code>. This function supports an optional <code>timezone</code> parameter. See <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#timezone_definitions">Time zone definitions</a> for information on how to specify a time zone.</p>
<pre tabindex="0"><code>EXTRACT(part FROM timestamp_expression [AT TIME ZONE timezone])
</code></pre><pre tabindex="0"><code>SELECT
	EXTRACT(DATE FROM started) AS deployment_start_date
FROM
	table
</code></pre><h2 id="3-how-to-calculate-duration-between-two-timestamp">3. How to calculate duration between two timestamp</h2>
<p>Returns the number of whole specified <code>date_part</code> intervals between two <code>TIMESTAMP</code> objects (<code>timestamp_expression_a</code> - <code>timestamp_expression_b</code>). If the first <code>TIMESTAMP</code> is earlier than the second one, the output is negative. Throws an error if the computation overflows the result type, such as if the difference in microseconds between the two <code>TIMESTAMP</code> objects would overflow an <code>INT64</code> value.</p>
<pre tabindex="0"><code>TIMESTAMP_DIFF(timestamp_expression_a, timestamp_expression_b, date_part)
</code></pre><pre tabindex="0"><code>SELECT
	TIMESTAMP_DIFF( t1.stopped, t1.started, SECOND ) AS deployment_time
FROM
	table t1
</code></pre><h2 id="4-regular-expression-and-multi-condition-in-bq">4. Regular expression and multi-condition in BQ</h2>
<p>Short-circuiting in conditional expressions can be exploited for error handling or performance tuning.</p>
<p><strong>Note:</strong> BigQuery provides regular expression support using the <a href="https://github.com/google/re2/wiki/Syntax">re2</a> library; see that documentation for its regular expression syntax.</p>
<pre tabindex="0"><code>SELECT
	CASE
  	WHEN REGEXP_CONTAINS(t1.app_image,r&#34;dkr.ecr.&#34;) THEN &#34;central ECR&#34;
    WHEN REGEXP_CONTAINS(t1.app_image,r&#34;registry.cowbell.realestate.com.au&#34;) THEN &#34;cowbell&#34;
    ELSE &#34;others&#34;
	END
	AS registry
FROM
	table t1
</code></pre><h2 id="5-how-to-get-data-from-last-one-year-dynamically">5. How to get data from last one year dynamically</h2>
<p>Truncates a timestamp to the granularity of <code>date_part</code>.</p>
<pre tabindex="0"><code>TIMESTAMP_TRUNC(timestamp_expression, date_part[, timezone])
</code></pre><pre tabindex="0"><code>SELECT
	t1.*
FROM
	table t1
WHERE
	s1.started
  	BETWEEN
			TIMESTAMP_TRUNC(TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 365 * 24 HOUR),DAY)
		AND
			TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), DAY)
</code></pre><h2 id="reference">Reference</h2>
<p><a href="https://stackoverflow.com/questions/58599629/bigquery-how-to-automatically-handle-duplicate-column-names-on-left-join">bigquery-how-to-automatically-handle-duplicate-column-names-on-left-join</a></p>
<p><a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#extract">timestamp_function_extract</a></p>
<p><a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#timestamp_trunc">timestamp_trunc</a></p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
