<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Trevor Fox</title>
    <description>The latest articles on DEV Community by Trevor Fox (@realtrevorfaux).</description>
    <link>https://dev.to/realtrevorfaux</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F264063%2F5e46559d-37a0-4983-8db0-d09c43e80cb4.jpg</url>
      <title>DEV Community: Trevor Fox</title>
      <link>https://dev.to/realtrevorfaux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/realtrevorfaux"/>
    <language>en</language>
    <item>
      <title>Beginner's Guide to Content Management Systems and Templating Engines</title>
      <dc:creator>Trevor Fox</dc:creator>
      <pubDate>Tue, 25 Feb 2020 17:04:02 +0000</pubDate>
      <link>https://dev.to/realtrevorfaux/beginner-s-guide-to-content-management-systems-and-templating-engines-4c18</link>
      <guid>https://dev.to/realtrevorfaux/beginner-s-guide-to-content-management-systems-and-templating-engines-4c18</guid>
      <description>&lt;p&gt;If you're new to web development—especially if you are coming from adjacent territory like marketing or product management, you've probably begun to understand the basics of variables and for loops but there's a big gap between where you are and how things get done in the wild. The goal of this post is to introduce you to the wild world of content management and tame it at the same time. Hope you enjoy!&lt;/p&gt;

&lt;h2&gt;
  
  
  Redefining the Content Management System Category
&lt;/h2&gt;

&lt;p&gt;The term “Content Management System” describes an actively expanding category of software that can be used to manage the creation and modification of digital content. You may think that definition seems broad but that’s because the category is really broad! In fact, **&lt;a href="https://en.wikipedia.org/wiki/Content_management_system"&gt;Wikipedia&lt;/a&gt; doesn't even include serving content as a requirement for this definition!&lt;/p&gt;

&lt;p&gt;On top of the content creation-and-modification functionality, most CMS’s provide a much wider range of offerings. This starts, of course, with actually serving the content as HTML pages (rather than just text or image files) and includes common services like handling ecommerce transactions, managing user credentials and web analytics. &lt;/p&gt;

&lt;p&gt;Here are a few members of the Venn diagram that is the CMS category:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://wordpress.com/"&gt;Wordpress&lt;/a&gt;&lt;/strong&gt;: The &lt;strong&gt;most popular CMS&lt;/strong&gt; started as a blog platform but now is capable of supporting any type of app from ecommerce shops to user-review websites.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://www.shopify.com/"&gt;Shopify&lt;/a&gt;&lt;/strong&gt;: The &lt;strong&gt;popular&lt;/strong&gt; &lt;strong&gt;ecommerce platform&lt;/strong&gt; is essentially a CMS that focuses on managing product content and handling monetary transactions.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://www.dropbox.com/"&gt;Dropbox&lt;/a&gt;:&lt;/strong&gt; You might not consider the **digital asset management software **a CMS, the app allows you to upload images and serve them publicly on the web.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://www.hubspot.com/"&gt;Hubspot&lt;/a&gt;:&lt;/strong&gt; The &lt;strong&gt;customer relationship management (CRM)&lt;/strong&gt; system, also offers a blog and landing page CMS with the personalization benefits that only a CRM could. &lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://ghost.org/"&gt;Ghost&lt;/a&gt;&lt;/strong&gt;: One of the most popular in a category of “&lt;strong&gt;headless CMS&lt;/strong&gt;,” Ghost serves content via an API which is perfect Javascript-based single-page apps (SPAs).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://webflow.com/"&gt;Webflow&lt;/a&gt;&lt;/strong&gt;: A &lt;strong&gt;codeless CMS&lt;/strong&gt;, it affords 80% of the content and design capabilities of a code-based CMS like Wordpress, without needing to write custom code to use them.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://blog.getpelican.com/"&gt;Pelican&lt;/a&gt;:&lt;/strong&gt; On the far corner of the CMS world is this &lt;strong&gt;static site generator&lt;/strong&gt; written in Python. Pelican simply translates a collection of text files (&lt;a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet"&gt;Markdown&lt;/a&gt; is commonly used) into a website that can be hosted on services like.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From headless to full-stack, codeless to code-only, feature-rich to barely-a-CMS, you get the idea: the space is huge. &lt;/p&gt;

&lt;p&gt;There is a CMS for almost any use case: If you don’t want to use a database, use a static site generator like Pelican or Jekyll. If you want to build a front end but don’t want to worry about the back end, use a headless CMS. If you want to use a CMS with a ton of community support and familiar developers, use Wordpress. The list goes on.&lt;/p&gt;

&lt;p&gt;No matter your use case, there are some general principles that apply to most CMS that are good to understand. That’s what we’ll get into next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beyond Content: Themes and Templates
&lt;/h2&gt;

&lt;p&gt;If you are working on SEO but aren’t really familiar with web development, you might have heard some of these terms like “theme,” “templating,” and “rendering,” and wonder what they’re all about. Let’s fix that.&lt;/p&gt;

&lt;h3&gt;
  
  
  CMS Themes
&lt;/h3&gt;

&lt;p&gt;Most CMS, including Wordpress, Drupal, and Shopify employ a concept of themes (even if they call them something else). Themes are the CSS, Javascript, and HTML template files that package CMS content into a complete website experience.&lt;/p&gt;

&lt;p&gt;The combination of these files in a browser creates the &lt;em&gt;“look-and-feel”&lt;/em&gt; of a website: CSS files define visual characteristics like background colors, typography, and iconography while JS files create animations and interactivity. HTML templates determine the layout of the content on a page whether that’s in a phone, tablet, or computer screen.&lt;/p&gt;

&lt;p&gt;**Themes offer one massive benefit: &lt;a href="https://en.wikipedia.org/wiki/Separation_of_concerns"&gt;separation of concerns&lt;/a&gt;. **This means that the &lt;em&gt;content&lt;/em&gt; is separated from the &lt;em&gt;presentation&lt;/em&gt; of the content. And while you might take that for granted, this is what makes it so easy to modify the layout of all the pages on a site without having to change the HTML of each page. (This was a game-changer in the early Web 2.0 days!) &lt;/p&gt;

&lt;p&gt;For example, if you want to change a Wordpress theme or Pelican theme, all you have to do is add the theme files to your project and change some site settings and voilà, your site has a hot new look!&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML Templating
&lt;/h3&gt;

&lt;p&gt;At the core of every theme is the way that content is transformed and merged into HTML to create a webpage. This is called templating. Typically template files look like regular HTML files except, instead of containing the page’s content within the HTML tags, there are variable placeholders and other “templating logic”  that will be replaced with the page’s content when the page is “rendered” by the CMS code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wu6c1CJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zz0jsrhxczztark0myov.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wu6c1CJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zz0jsrhxczztark0myov.png" alt="How templating engines work"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It really helps to see an example. Below is a very minimal HTML page with variables (denoted by the &lt;code&gt;{{variable}}&lt;/code&gt; syntax) embedded in the HTML.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After the variables are replaced with the page’s content, the output HTML file would look something like this: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let’s step way back for a moment and discuss what this all means. Consider how much easier it is to scale up a website with a lot of content. Without templating, the best way to create new pages would be to copy a similar existing HTML file and replace the copied page’s with new content. It sounds like a nightmare! And what if you wanted to change something about a page you copied from?? Yeah… you get the idea.&lt;/p&gt;

&lt;p&gt;This generic approach to generating HTML files was a game-changer. Now nearly every single site on the web employs the concept of templating and it’s a foundational piece of SEO.&lt;/p&gt;

&lt;p&gt;Let’s consider the popular marketplace website, craigslist.com. The site has millions (billions?) of live pages at any moment but it likely has fewer than 100 different templates. There are pages for every geographic locale that list all the categories for that locale, there are several types of category pages that list all the event, service, and for-sale item listing in that locale, and there are pages with all the details for each event, service, or item in that category. The pages add up quickly, but thanks to templates, the site’s content scales beautifully. &lt;/p&gt;

&lt;p&gt;This is a great example for SEO too. Consider for a moment that all of Craigslist’s category pages are just templates with content from other pages combined dynamically to generate content for these new pages. It’s like every time they have content for a few listing pages they get a category page for free—and all the organic traffic that comes with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Templating Engines
&lt;/h3&gt;

&lt;p&gt;Most CMSs employ a templating engine (or you might hear them called  “templating languages”) on top of whatever programming language the CMS was built on. templating engines make it easy to render HTML files from template files. Here are a few examples and the CMS’ and web frameworks that use them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://shopify.github.io/liquid/"&gt;Liquid&lt;/a&gt;&lt;/strong&gt;: Ruby’s most popular templating engine is used by Shopify, &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;, &lt;a href="https://support.zendesk.com/hc/en-us/articles/203662116-Using-placeholders"&gt;Zendesk&lt;/a&gt;, and many &lt;a href="https://github.com/Shopify/liquid/wiki#who-uses-liquid"&gt;others&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="http://jinja.pocoo.org/"&gt;Jinja2&lt;/a&gt;&lt;/strong&gt;: Python’s most popular templating engine is used by Pelican and can be used with the web frameworks &lt;a href="https://palletsprojects.com/p/flask/"&gt;Flask&lt;/a&gt; and &lt;a href="https://www.djangoproject.com/"&gt;Django&lt;/a&gt; and &lt;a href="https://stackshare.io/jinja2"&gt;others&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://designers.hubspot.com/docs/hubl/syntax"&gt;HubL&lt;/a&gt;&lt;/strong&gt;: Hubspot’s proprietary templating engine for web and email is an extension of on Jinja offering more Hubspot-specific functionality&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://handlebarsjs.com/"&gt;Handlebars&lt;/a&gt;&lt;/strong&gt;: The Javascript templating engine is used by Ghost, &lt;a href="https://stackshare.io/jinja2"&gt;Enduro.js&lt;/a&gt; CMS, and web frameworks such as Ember.js and Metor.js.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;a href="https://css-tricks.com/php-templating-in-just-php/"&gt;PHP&lt;/a&gt;?&lt;/strong&gt;: Ok, PHP is a scripting language but it’s still worth mentioning here because the language was built for constructing web pages. As part of the language’s design, you &lt;a href="https://www.sitepoint.com/create-your-own-wordpress-theme-from-an-html-template/"&gt;write logic into .php files&lt;/a&gt; that output content into the HTML.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is only a small sample of templating engines. Most popular programming languages have several popular options but all of these templating engines have a few things in common: they assemble and render HTML files from templates that look like HTML but can contain variables, conditional logic, and other inserted or inherited templates. &lt;/p&gt;

&lt;p&gt;In the future, I &lt;em&gt;hope&lt;/em&gt; to break down how website templating works, especially within the &lt;a href="https://trevorfox.com/2018/10/seo-measurement-framework/"&gt;context of SEO&lt;/a&gt;. &lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--YACU9y7v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/791411688226512897/muhNBTCK_normal.jpg" alt="Trevor Fox profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Trevor Fox
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/realtrevorfaux"&gt;@realtrevorfaux&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Would you be interested in an online course: "Intro to Technical SEO and Web Analytics" featuring Python and Javascript? &lt;br&gt;&lt;br&gt;Stay tuned: &lt;a href="https://t.co/mmuJ68YmDp"&gt;mailchi.mp/17a84f9ff204/t…&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;a href="https://twitter.com/hashtag/technicalseo"&gt;#technicalseo&lt;/a&gt; &lt;a href="https://twitter.com/hashtag/webanalytics"&gt;#webanalytics&lt;/a&gt; &lt;a href="https://twitter.com/hashtag/webmarketing"&gt;#webmarketing&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      06:56 AM - 25 Feb 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1232197587630989313" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1232197587630989313" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      0
      &lt;a href="https://twitter.com/intent/like?tweet_id=1232197587630989313" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      0
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;But for now, this is the end of this primer on content management systems, themes, and templates. Hopefully, this sets the foundation for future posts about hands-on technical SEO with the static-site generator, Pelican as code examples.&lt;/p&gt;

</description>
      <category>cms</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>seo</category>
    </item>
    <item>
      <title>Intro to SQL User Defined Functions (UDFs) in Redshift</title>
      <dc:creator>Trevor Fox</dc:creator>
      <pubDate>Sat, 01 Feb 2020 22:50:13 +0000</pubDate>
      <link>https://dev.to/realtrevorfaux/intro-to-sql-user-defined-functions-udfs-in-redshift-bp7</link>
      <guid>https://dev.to/realtrevorfaux/intro-to-sql-user-defined-functions-udfs-in-redshift-bp7</guid>
      <description>&lt;p&gt;As a data analyst, your credibility is as valuable as your analytical skills. And to maintain your credibility, it’s important to be able to answer questions correctly and consistently. That’s why you must be careful to integrate reproducibility into your SQL analyses. This post is going to show you how you can use Redshift User Defined Functions to do just that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reproducibility in SQL Analysis
&lt;/h3&gt;

&lt;p&gt;There are two broad factors to reproducibility. The first is the data—different data for the same analysis is going to produce different results. A good example would be a court case: if you ask two witnesses the same question, each one will probably tell you something similar but likely slightly different. &lt;/p&gt;

&lt;p&gt;The second factor is the analytical methods. If we use the court case example again, this would be like the prosecution and the defense asking a witness the same question in two different ways. The lawyers would do this with the intent_ _to get two different answers.&lt;/p&gt;

&lt;p&gt;This post is more concerned with the second factor of reproducibility, the analytical method. Whenever you have to write complex SQL queries to get an answer, your analytical method (the SQL query) becomes a big variable. SQL is iterative by nature! Think about it, just be adding and removing “WHEN” conditions, you’re liable to drastically change your results. &lt;/p&gt;

&lt;p&gt;As you iterate on a numerical calculation or classification in a &lt;a href="https://blog.panoply.io/postgres-case-statement-basics-by-example"&gt;CASE expression&lt;/a&gt; you are likely to change your query results. And what happens when you have to perform the same analysis weeks later? You better hope you use the same iteration of your SQL query the second time as the first! &lt;/p&gt;

&lt;p&gt;And that is exactly where User-Defined Functions become so valuable! &lt;/p&gt;

&lt;p&gt;User-Defined Functions (UDFs) are simply a way of saving one or more calculations or expressions with a name so that you can refer to it as a SQL function for further use.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are User Defined Functions?
&lt;/h2&gt;

&lt;p&gt;User-Defined Functions can be used just like any other function in SQL like SUBSTRING or ROUND except you get to define what the output of the function is, given the input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;User-Defined Functions (UDFs) are simply a way of saving one or more calculations or expressions with a name so that you can refer to it as a SQL function for further use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They are a great way to simplify your SQL queries and make them more reproducible at the same time. You can basically take several lines of code that produce one value from your SELECT statement, give it a name, and keep it for future use. Using UDFs, you can ensure that, given the same data, your calculations will always produce the same result. &lt;/p&gt;

&lt;h2&gt;
  
  
  UDF Functions are Scalar Functions. What does scalar mean?
&lt;/h2&gt;

&lt;p&gt;As you learn about UDFs, you’ll see references to the word “scalar.” Scalar just means that the function is defined with one or more parameters and &lt;strong&gt;returns a single result&lt;/strong&gt;. Just like the ROUND function has one parameter (the number) and an optional second parameter (the number of decimal places for rounding) and returns the rounded number. The function is applied to every value in a column, &lt;em&gt;but it only returns one value&lt;/em&gt; for each row in that column.  &lt;/p&gt;

&lt;h2&gt;
  
  
  A Hello World! SQL UDF Example
&lt;/h2&gt;

&lt;p&gt;If you are familiar with any kind of programming language, this should be pretty simple. The CREATE FUNCTION syntax only requires a function name and a return data type. That’s it. &lt;/p&gt;

&lt;p&gt;A function called hello_world that returns ‘HELLO WORLD!’ every time would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create function hello_world ( )
  returns varchar
stable
as $$
  select 'HELLO WORLD!'
$$ language sql; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In that case, the input data type and the output data type are both varchar because “HELLO WORLD!” is a text output. You could use your function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select hello_world() as my_first_function;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And you’d get an output that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_first_function
HELLO WORLD!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But that wouldn’t be very interesting. You’ll generally want to modify the input(s) of your functions. Let’s take apart a more interesting UDF example.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Write SQL UDF Functions
&lt;/h2&gt;

&lt;p&gt;This example function, called url_category takes a varchar as an input (a URL) and returns a varchar output (the category of the URL). To do this, the function compares the input (shown as $1 because it is the first parameter) to the conditions of a case expression.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ygogSVij--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/trevorfox.com/wp-content/uploads/2020/02/redshift-udf-example.png%3Fw%3D720%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ygogSVij--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/trevorfox.com/wp-content/uploads/2020/02/redshift-udf-example.png%3Fw%3D720%26ssl%3D1" alt="Redshift UDF Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You could also write this function with two parameters. Here’s an example if you were using &lt;a href="https://panoply.io/integrations/google-analytics/"&gt;Google Analytics data&lt;/a&gt;. You could take in the parameters, hostname and a page_path to get more granular with your URL categorization.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL UDF Functions with Multiple Arguments
&lt;/h2&gt;

&lt;p&gt;This is Redshift’s example from their docs. It takes two parameters (both specified as float) and returns the value that is greater of the two.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create function f_sql_greater (float, float)
  returns float
stable
as $$
  select case when $1 &amp;gt; $2 then $1
    else $2
  end
$$ language sql;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To refer to the different parameters in the function, you just use the dollar sign ($) and the order of the parameter in the function definition. As long as you follow that convention, you could go wild with your input parameters!&lt;/p&gt;

&lt;h2&gt;
  
  
  Redshift UDF Limitations
&lt;/h2&gt;

&lt;p&gt;UDFs are basically restricted to anything that you can normally do inside a SELECT clause. The only exception would be subqueries—you &lt;em&gt;cannot&lt;/em&gt; use subqueries in a UDF. This means you’re limited to constant or literal values, &lt;a href="https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html"&gt;compound expressions&lt;/a&gt;, comparison conditions, CASE expressions, and any other scalar function. But that’s quite a lot! &lt;/p&gt;

&lt;h2&gt;
  
  
  Common UDF Errors and their Causes
&lt;/h2&gt;

&lt;p&gt;Once you start writing UDFs, you’ll find that it’s pretty easy going but there are two especially common “gotchas” &lt;/p&gt;

&lt;h3&gt;
  
  
  ERROR:  return type mismatch in function declared to return {data type}
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;DETAIL:  Actual return type is {data type}.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This just means that you’ve created a function where the output value has a different data type than you said it would. Check that the return data type that you specified is the same as the function is actually returning. This can be tricky if your function is using a CASE expression because a CASE could accidentally return two different data types.&lt;/p&gt;

&lt;h3&gt;
  
  
  ERROR:  The select expression can not have subqueries.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;CONTEXT:  Create SQL function "try_this" body&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This means you tried to write a SELECT statement in your function that includes a subquery. You can’t do that.&lt;/p&gt;

&lt;h3&gt;
  
  
  ERROR:  function function_name({data type}) does not exist
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;HINT:  No function matches the given name and argument types. You may need to add explicit type casts.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is one especially odd thing about Redshift UDFs. You can have several functions with the same name as long as they take different arguments or argument types. This can get confusing. The error here means that you’ve called a function with the wrong type of argument. Check the input data type of your function and make sure it’s the same as you input data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scaling your SQL Analysis with Confidence!
&lt;/h2&gt;

&lt;p&gt;User-Defined Functions make it really easy to repeat your analytical method across team members and across time. All you have to do is define a function once and let everyone know that they can use it. On top of that, if you want to change the logic of your function you only have to do it in one place and then that logic will be changed for each user in every workbench, notebook, or dashboard!&lt;/p&gt;

&lt;p&gt;Take advantage of this clever tool. Your team will thank you, and &lt;em&gt;you will thank you later!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>redshift</category>
      <category>aws</category>
    </item>
    <item>
      <title>8 New SQL Tools That Will Change How You Work in 2020</title>
      <dc:creator>Trevor Fox</dc:creator>
      <pubDate>Sun, 05 Jan 2020 22:45:45 +0000</pubDate>
      <link>https://dev.to/realtrevorfaux/8-new-sql-tools-that-will-change-how-you-work-in-2020-n63</link>
      <guid>https://dev.to/realtrevorfaux/8-new-sql-tools-that-will-change-how-you-work-in-2020-n63</guid>
      <description>&lt;p&gt;When it comes to working with SQL, we all want the same things: speed and scale. And on top of that we, want to be easy—maybe even fun. From data collection to processing, to analysis, to putting it into action; we work with data because we want to reach a goal, not because we love parsing JSON, type checking, inspecting logs, and writing documentation. That’s why we need good tools—especially ones that evolve with our changing pace and work styles. That’s why I  wanted to share these tools with you today. &lt;/p&gt;

&lt;p&gt;I’ve been working in SQL in a few roles, for a few years. It’s been long enough to understand the work, but not long enough to be tied to antiquated methodologies or systems. To me, these tools reflect a shift towards a modern SQL workflow to fit the pace and flexibility of modern work. I appreciate both the innovation of these tools and ingenuity used to solve laborious SQL tasks.&lt;/p&gt;

&lt;p&gt;These tools cover everything from getting data into your database, to transforming your data, to analyzing your data, and even sharing your findings. Ok, enough with the intro, let’s get to the SQL tools!&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Collection
&lt;/h2&gt;

&lt;p&gt;Without data in your database, SQL is entirely useless. These tools make it easy to cross the bridge from messy data to a SQL database. &lt;/p&gt;

&lt;h3&gt;
  
  
  Numidian Convert
&lt;/h3&gt;

&lt;p&gt;Everybody has run into the problem of having a database full of interesting data and some other file that would be a lot more interesting if it were in the database. &lt;a href="https://numidian.io/convert"&gt;Numidian Convert&lt;/a&gt; makes it &lt;em&gt;incredibly easy&lt;/em&gt; to get data from JSON and CSV files into databases. All you have to do is input a file and define your transformations then it gives you the CREATE TABLE and INSERT statements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kwPvxo7Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3w0eelzzejpi44ubxklt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kwPvxo7Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3w0eelzzejpi44ubxklt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Numidian convert supports Postgres, MySQL, and SQLite. They have a free offering that allows you to convert files up to 10MB, and they also offer a paid API when you need to convert a lot of data for $20/mo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Panoply
&lt;/h3&gt;

&lt;p&gt;You know the problem. All your data comes from a bunch of APIs, spreadsheets, and file systems and you just want it in one place where you can connect your SQL tools. &lt;a href="https://panoply.io/"&gt;Panoply&lt;/a&gt; is a cloud data warehouse with a ton of native data pipelines from including MySQL, MongoDB, Salesforce, Stripe, Google Analytics, Google Sheets and a ton of other &lt;a href="https://panoply.io/integrations/"&gt;integrations&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jk0FNXGT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0pxxbyv67w8f08clt1nz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jk0FNXGT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0pxxbyv67w8f08clt1nz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Panoply is built on AWS infrastructure and can be used with Azure too. It is basically the power of Redshift except &lt;a href="https://blog.panoply.io/aws-redshift-tutorial"&gt;10x easier to setup&lt;/a&gt;, manage and scale. They offer a full-featured free trial that allows you to start querying all your data in less time than it takes to finish a cup of coffee.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Exploration
&lt;/h2&gt;

&lt;p&gt;If you’ve ever had to wrap your head around a new DB, you’ve probably had to spend a lot of time querying system tables just to figure you where the good data lives and how much data there is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Schema Explorer
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://schemaexplorer.io/"&gt;Schema Explorer&lt;/a&gt;, recently open-sourced, is a friendly UI wrapper around all the queries that you would make to get familiar with a new database. What is especially nice about the tool is, if you have foreign key constraints in place, Schema Explorer will draw out a diagram of your database for you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NwfMZ4vZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/syxw63l6ueum9s0avw00.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NwfMZ4vZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/syxw63l6ueum9s0avw00.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The software is pretty lean (it appears to be a one-man show) but it supports both Mac and Windows and is 100% free and &lt;a href="https://github.com/timabell/schema-explorer"&gt;open source on Github&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Transformations
&lt;/h2&gt;

&lt;p&gt;When it comes to scaling, especially scaling out the complexity and application of data across an organization, you have to have a good process. These two tools are really impressive in how they’ve made complex data transformation workflows simple.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dataform
&lt;/h3&gt;

&lt;p&gt;Consistent with the, no/low maintenance infrastructure trend, &lt;a href="https://dataform.co/"&gt;Dataform&lt;/a&gt; wraps up the entire Transform segment of the &lt;a href="https://panoply.io/data-warehouse-guide/3-ways-to-build-an-etl-process/"&gt;ELT process&lt;/a&gt; into a SaaS application. This makes it easier than ever to democratize trustworthy tables for end-user analysis. The best part: everything is managed in SQL or in a well-designed UI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--16LDtx7F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ojukor2u3adt00tmg0rg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--16LDtx7F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ojukor2u3adt00tmg0rg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dataform works with Redshift, BigQuery, Panoply, Azure SQL DW, and Snowflake data warehouses. It also seamlessly handles version control via Github within the UI. And again, all this power comes without maintaining any infrastructure!&lt;/p&gt;

&lt;h3&gt;
  
  
  DBT (Data Build Tool)
&lt;/h3&gt;

&lt;p&gt;For those of you who like total control of your data pipelines (and don’t mind maintaining the infrastructure that comes along with it) &lt;a href="https://www.getdbt.com/"&gt;DBT&lt;/a&gt; is the transformation tool for you. DBT is among my favorite Github repos because they have really thought about all the challenges of data preparation and transformation down to generating documentation on the fly! DBT predates Dataform so the &lt;a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph"&gt;DAG&lt;/a&gt;-ish workflows and templating feel similar except you can control every detail of your deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3SKATBTy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/nnkp4bimd49qy26eot8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3SKATBTy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/nnkp4bimd49qy26eot8b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DBT is a command-line tool that supports Postgres, Redshift, Bigquery, Panoply, and Snowflake, community/partial support for Microsoft SQL Server, Presto, and Spark. Also, their community is &lt;em&gt;really&lt;/em&gt; impressive. With “customers” like Hubspot and Seatgeek, you know you're amongst some solid data company.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Access and Sharing
&lt;/h2&gt;

&lt;p&gt;This is probably the most solved problem in the SQL space (if not over-solved). But as our workflows evolve, so should our tools. These two tools stand out to me because they are natural extensions from modern collaboration tools, Slack and Google Sheets.&lt;/p&gt;

&lt;h3&gt;
  
  
  SQLBot
&lt;/h3&gt;

&lt;p&gt;If you’re like me, you’ve integrated every app you use with Slack. I’m not kidding, I have a &lt;a href="https://trevorfox.com/2017/01/custom-slack-slash-command-and-10-free-apis/"&gt;slash command for Chuck Norris jokes&lt;/a&gt;. It should come as no surprise that I was really excited when I learned about SQLBot. I had always thought it would be cool, security aside, to write SQL into Slack and get results back. &lt;a href="https://www.sqlbot.co/"&gt;SQLBot&lt;/a&gt; is the next best thing. It allows you to set up slash commands that return query results. You can get your reports without leaving your conversation!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lTDio6mf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i4n98713e6vz2163h38p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lTDio6mf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i4n98713e6vz2163h38p.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SQLBot is a newer app that is fresh from &lt;a href="https://www.indiehackers.com/product/sqlbot-co"&gt;IndieHackers&lt;/a&gt; so while the functionality is amazing the Bootstrap interface keeps things pretty simple. It supports Postgres, MySQL, SQLServer, Amazon Redshift, and Panoply and allows you to drop variables in to your queries which is pretty slick.&lt;/p&gt;

&lt;h3&gt;
  
  
  SeekWell
&lt;/h3&gt;

&lt;p&gt;As much as I believe that &lt;a href="https://trevorfox.com/2020/01/getting-started-with-sql-for-marketing-with-facebook-ads-example/"&gt;everybody should learn SQL&lt;/a&gt;, there are, and probably always will be those who love their spreadsheets too much to let them go. Lucky for us, &lt;a href="https://www.seekwell.io/"&gt;SeekWell&lt;/a&gt; has built a bridge between your database and your co-workers spreadsheets. The tool allows you to write SQL, or Python via a Jupyter notebook and import the data directly into Google Spreadsheets and Slack. You can even schedule queries from both your database and Python notebooks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vTUudg88--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/visu69xkkrm2uu3lijvd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vTUudg88--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/visu69xkkrm2uu3lijvd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SeekWell supports MySQL, Postgres, Redshift, Panoply, Microsoft SQL Server, and Snowflake and I have been pretty impressed by their development speed. It seems like every time I check back with their product, the tool’s experience is becoming more seamless.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Documentation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At the intersection between &lt;em&gt;nobody’s-got-time-for-that&lt;/em&gt; and &lt;em&gt;this-is-so-useful&lt;/em&gt; lives our old friend documentation. Here is a new tool that will do the job nicely and is pretty painless.&lt;/p&gt;

&lt;h3&gt;
  
  
  dbdiagram.io
&lt;/h3&gt;

&lt;p&gt;ER diagrams and data dictionaries are a data analyst’s best friends as they get acquainted with a new database or data warehouse but building these from scratch with Google Sheets or Google Drawings is lame and boring and doesn’t take advantage of code to take care of the repetitive tasks that we love to automate. &lt;a href="https://dbdiagram.io/home"&gt;dbiagram.io&lt;/a&gt; took a novel approach to this problem and translates a markup language that describes a database and it’s relationships into interactive diagrams that &lt;em&gt;show&lt;/em&gt; the database tables. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5xMIbtZ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5r4pj1zyl9zdk7lpmz3n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5xMIbtZ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5r4pj1zyl9zdk7lpmz3n.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The free tool is open source, as is the &lt;a href="https://www.dbml.org/home/#intro"&gt;DBML&lt;/a&gt;, their markup language that the tool uses, and is maintained by the good people at Holistics. If you have a big DB that you need to diagram, I’d recommend looking into dbdiagram.io to see how you can programmatically generate your ER diagram for free!&lt;/p&gt;

&lt;p&gt;I hope some of these tools will bring joy to your SQL work in 2020!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>productivity</category>
      <category>postgres</category>
      <category>database</category>
    </item>
  </channel>
</rss>
