Re: Special ORDER BY

From: Tulio <tulio(at)informidia(dot)com(dot)br>
To: Martín Marqués <martin(dot)marques(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Special ORDER BY
Date: 2012-06-19 19:49:37
Message-ID: 4FE0D7D1.8090209@informidia.com.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#333333">
You can use something like "CASE WHEN" .. and use a number to order
your data...<br>
like this<br>
<pre wrap=""><font color="#000099">SELECT *, CASE WHEN c.etiqueta = </font><font color="#000099">245</font> THEN <font color="#000099">1
</font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">130</font> THEN <font color="#000099">2 </font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = 240</font><font color="#000099"></font> THEN <font color="#000099">3 </font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">243</font> THEN <font color="#000099">4 </font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">246</font> THEN <font color="#000099">5 </font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">490</font> THEN <font color="#000099">6 </font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">630</font> THEN <font color="#000099">7</font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">730</font> THEN <font color="#000099">8</font>
<font color="#000099"> </font><font color="#000099">WHEN c.etiqueta = </font><font color="#000099">740</font> THEN <font color="#000099">9</font><font color="#000099"></font>
<font color="#000099"> end as newcolum FROM subcampo sc JOIN campo c ON (c.codigo = sc.campo)
WHERE c.etiqueta IN (245, 130, 240, 243, 246, 490, 630, 730, 740)
ORDER BY </font><font color="#000099">newcolum</font>
</pre>
<br>
Em 19/06/2012 16:31, Martín Marqués escreveu:
<blockquote
cite="mid:CABeG9LtRiaAaEULLwxBoud+xcv+HknpFYioUaQYkC2ynq_4t+Q(at)mail(dot)gmail(dot)com"
type="cite">
<pre wrap="">How can I order a result set by a special order of a column. For
example, let's say I have this query

SELECT * FROM subcampo sc JOIN campo c ON (c.codigo = sc.campo)
WHERE c.etiqueta IN (245, 130, 240, 243, 246, 490, 630, 730, 740)

Now I want to order the result set using c.etiqueta but with the order
of the values as you see in the IN clause.

How can I do that?

</pre>
</blockquote>
<br>
<div class="moz-signature"><br>
<span style="font-size:8.0pt;font-family:Tahoma;
color:green"></span>
</div>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 2.5 KB

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Hellmuth Vargas 2012-06-19 19:53:05 Re: Special ORDER BY
Previous Message Martín Marqués 2012-06-19 19:31:44 Special ORDER BY