<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>10.5. UNION, CASE, and Related Constructs</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="typeconv-query.html" title="10.4. Value Storage" /><link rel="next" href="typeconv-select.html" title="10.6. SELECT Output Columns" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">10.5. <code xmlns="http://www.w3.org/1999/xhtml" class="literal">UNION</code>, <code xmlns="http://www.w3.org/1999/xhtml" class="literal">CASE</code>, and Related Constructs</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="typeconv-query.html" title="10.4. Value Storage">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><th width="60%" align="center">Chapter 10. Type Conversion</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="typeconv-select.html" title="10.6. SELECT Output Columns">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TYPECONV-UNION-CASE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">10.5. <code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs</h2></div></div></div><a id="id-1.5.9.10.2" class="indexterm"></a><a id="id-1.5.9.10.3" class="indexterm"></a><a id="id-1.5.9.10.4" class="indexterm"></a><a id="id-1.5.9.10.5" class="indexterm"></a><a id="id-1.5.9.10.6" class="indexterm"></a><a id="id-1.5.9.10.7" class="indexterm"></a><p> SQL <code class="literal">UNION</code> constructs must match up possibly dissimilar types to become a single result set. The resolution algorithm is applied separately to each output column of a union query. The <code class="literal">INTERSECT</code> and <code class="literal">EXCEPT</code> constructs resolve dissimilar types in the same way as <code class="literal">UNION</code>. The <code class="literal">CASE</code>, <code class="literal">ARRAY</code>, <code class="literal">VALUES</code>, <code class="function">GREATEST</code> and <code class="function">LEAST</code> constructs use the identical algorithm to match up their component expressions and select a result data type. </p><div class="procedure" id="id-1.5.9.10.9"><p class="title"><strong>Type Resolution for <code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs</strong></p><ol class="procedure" type="1"><li class="step"><p> If all inputs are of the same type, and it is not <code class="type">unknown</code>, resolve as that type. </p></li><li class="step"><p> If any input is of a domain type, treat it as being of the domain's base type for all subsequent steps. <a href="#ftn.id-1.5.9.10.9.3.1.1" class="footnote"><sup class="footnote" id="id-1.5.9.10.9.3.1.1">[11]</sup></a> </p></li><li class="step"><p> If all inputs are of type <code class="type">unknown</code>, resolve as type <code class="type">text</code> (the preferred type of the string category). Otherwise, <code class="type">unknown</code> inputs are ignored for the purposes of the remaining rules. </p></li><li class="step"><p> If the non-unknown inputs are not all of the same type category, fail. </p></li><li class="step"><p> Choose the first non-unknown input type which is a preferred type in that category, if there is one. </p></li><li class="step"><p> Otherwise, choose the last non-unknown input type that allows all the preceding non-unknown inputs to be implicitly converted to it. (There always is such a type, since at least the first type in the list must satisfy this condition.) </p></li><li class="step"><p> Convert all inputs to the selected type. Fail if there is not a conversion from a given input to the selected type. </p></li></ol></div><p> Some examples follow. </p><div class="example" id="id-1.5.9.10.11"><p class="title"><strong>Example 10.10. Type Resolution with Underspecified Types in a Union</strong></p><div class="example-contents"><p> </p><pre class="screen"> SELECT text 'a' AS "text" UNION SELECT 'b'; text ------ a b (2 rows) </pre><p> Here, the unknown-type literal <code class="literal">'b'</code> will be resolved to type <code class="type">text</code>. </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.10.12"><p class="title"><strong>Example 10.11. Type Resolution in a Simple Union</strong></p><div class="example-contents"><p> </p><pre class="screen"> SELECT 1.2 AS "numeric" UNION SELECT 1; numeric --------- 1 1.2 (2 rows) </pre><p> The literal <code class="literal">1.2</code> is of type <code class="type">numeric</code>, and the <code class="type">integer</code> value <code class="literal">1</code> can be cast implicitly to <code class="type">numeric</code>, so that type is used. </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.10.13"><p class="title"><strong>Example 10.12. Type Resolution in a Transposed Union</strong></p><div class="example-contents"><p> </p><pre class="screen"> SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL); real ------ 1 2.2 (2 rows) </pre><p> Here, since type <code class="type">real</code> cannot be implicitly cast to <code class="type">integer</code>, but <code class="type">integer</code> can be implicitly cast to <code class="type">real</code>, the union result type is resolved as <code class="type">real</code>. </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.10.14"><p class="title"><strong>Example 10.13. Type Resolution in a Nested Union</strong></p><div class="example-contents"><p> </p><pre class="screen"> SELECT NULL UNION SELECT NULL UNION SELECT 1; ERROR: UNION types text and integer cannot be matched </pre><p> This failure occurs because <span class="productname">PostgreSQL</span> treats multiple <code class="literal">UNION</code>s as a nest of pairwise operations; that is, this input is the same as </p><pre class="screen"> (SELECT NULL UNION SELECT NULL) UNION SELECT 1; </pre><p> The inner <code class="literal">UNION</code> is resolved as emitting type <code class="type">text</code>, according to the rules given above. Then the outer <code class="literal">UNION</code> has inputs of types <code class="type">text</code> and <code class="type">integer</code>, leading to the observed error. The problem can be fixed by ensuring that the leftmost <code class="literal">UNION</code> has at least one input of the desired result type. </p><p> <code class="literal">INTERSECT</code> and <code class="literal">EXCEPT</code> operations are likewise resolved pairwise. However, the other constructs described in this section consider all of their inputs in one resolution step. </p></div></div><br class="example-break" /><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.5.9.10.9.3.1.1" class="footnote"><p><a href="#id-1.5.9.10.9.3.1.1" class="para"><sup class="para">[11] </sup></a> Somewhat like the treatment of domain inputs for operators and functions, this behavior allows a domain type to be preserved through a <code class="literal">UNION</code> or similar construct, so long as the user is careful to ensure that all inputs are implicitly or explicitly of that exact type. Otherwise the domain's base type will be preferred. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="typeconv-query.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="typeconv.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="typeconv-select.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">10.4. Value Storage </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 10.6. <code class="literal">SELECT</code> Output Columns</td></tr></table></div></body></html>