1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>10.5.UNION, CASE, and Related Constructs</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="typeconv.html" title="Chapter10.Type Conversion">
<link rel="prev" href="typeconv-query.html" title="10.4.Value Storage">
<link rel="next" href="indexes.html" title="Chapter11.Indexes">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="typeconv-union-case"></a>10.5.<code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs</h2></div></div></div>
<a name="id627311"></a><a name="id627324"></a><a name="id627338"></a><a name="id627351"></a><a name="id627365"></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="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">
<a name="id627439"></a><p class="title"><b>Type Resolution for <code class="literal">UNION</code>, <code class="literal">CASE</code>,
and Related Constructs</b></p>
<ol type="1">
<li><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, ignore the <code class="type">unknown</code> inputs while choosing the result type.</p></li>
<li><p>If the non-unknown inputs are not all of the same type category, fail.</p></li>
<li><p>Choose the first non-unknown input type which is a preferred type in
that category or allows all the non-unknown inputs to be implicitly
converted to it.</p></li>
<li><p>Convert all inputs to the selected type.</p></li>
</ol>
</div>
<p>Some examples follow.</p>
<div class="example">
<a name="id627503"></a><p class="title"><b>Example10.7.Type Resolution with Underspecified Types in a Union</b></p>
<div class="example-contents">
<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 as type <code class="type">text</code>.</p>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id627528"></a><p class="title"><b>Example10.8.Type Resolution in a Simple Union</b></p>
<div class="example-contents">
<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">
<a name="id627568"></a><p class="title"><b>Example10.9.Type Resolution in a Transposed Union</b></p>
<div class="example-contents">
<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></body>
</html>
|