ORDER records based on parameters in IN clause
От | Riya Verghese |
---|---|
Тема | ORDER records based on parameters in IN clause |
Дата | |
Msg-id | BBA4F47BB0CEDD4D8ADD2FE21E2823673116C5@dhost002-37.dex002.intermedia.net обсуждение исходный текст |
Список | pgsql-sql |
<div class="Section1"><p class="MsoNormal" style="background:white"><b><font face="Times New Roman" size="3"><span lang="EN"style="font-size:12.0pt;font-weight:bold">Thanks!</span></font></b><p class="MsoNormal" style="background:white"><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt">This worked beautifully.</span></font><pclass="MsoNormal" style="background:white"><font face="Times New Roman" size="3"><span lang="EN"style="font-size:12.0pt"> </span></font><p class="MsoNormal" style="background:white"><font face="Times New Roman"size="3"><span lang="EN" style="font-size:12.0pt">Here’s what I have:</span></font><p class="MsoNormal" style="background:white"><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt">SELECT a.listing_id FROM cls.listings a</span></font><p class="MsoNormal" style="background:white"><font face="Times New Roman"size="3"><span lang="EN" style="font-size:12.0pt">JOIN (SELECT listing_id,count(listing_id) AS count FROM cls.tagslistings </span></font><p class="MsoNormal" style="background:white"><font face="Times New Roman" size="3"><spanlang="EN" style="font-size:12.0pt">WHERE cust_id =27 </span></font><p class="MsoNormal" style="background:white"><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt">AND tag_id IN (SELECTtag_id FROM cls.tags WHERE tag_name IN ('toys','263'))</span></font><p class="MsoNormal" style="background:white"><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt">GROUP BY listing_seq_id</span></font><p class="MsoNormal" style="background:white"><font face="Times New Roman" size="3"><span lang="EN"style="font-size:12.0pt">) AS X ON X.listing_id= a.listing_id </span></font><p class="MsoNormal" style="background:white"><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt">ORDER by X.countDESC </span></font><p class="MsoNormal" style="background:white"><font face="Times New Roman" size="3"><span lang="EN"style="font-size:12.0pt"> </span></font><p class="MsoNormal" style="background:white"><font face="Times New Roman"size="3"><span lang="EN" style="font-size:12.0pt">R. Verghese</span></font><p class="MsoNormal" style="background:white"><b><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt;font-weight:bold"> </span></font></b><pclass="MsoNormal" style="background:white"><font face="TimesNew Roman" size="3"><span lang="EN" style="font-size:12.0pt"> </span></font><p class="MsoNormal" style="background:white"><b><fontface="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt;font-weight:bold"> </span></font></b><pclass="MsoNormal" style="background:white"><b><font face="TimesNew Roman" size="3"><span lang="EN" style="font-size:12.0pt;font-weight:bold">Author: </span></font></b><spanlang="EN">Zac<br /><b><span style="font-weight:bold">Date: </span></b>2005-06-29 05:12<noscript>2005-06-29 12:12</noscript> -700<noscript>UTC<br /></noscript><b><spanstyle="font-weight:bold">To: </span></b>pgsql-sql<br /><b><span style="font-weight:bold">Subject: </span></b>Re:[SQL] ORDER records based on parameters in IN clause</span><p class="MsoNormal"style="background:white"><font face="Times New Roman" size="3"><span lang="EN" style="font-size:12.0pt">>SELECT<i><span style="font-style:italic"><br /> > table.*<br /> > FROM<br /> > table<br/> > JOIN (SELECT id, count(id) AS count FROM... your subquery) AS x<br /> > ORDER BY<br /> > x.count<br/> > <br /> > Bye.</span></i><br /> Sorry: I forgot join condition:<br /> SELECT</span></font><pre style="background:white"><fontface="Courier New" size="2"><span lang="EN" style="font-size:10.0pt"> table.*</span></font></pre><pclass="MsoNormal" style="background:white"><font face="Times New Roman" size="3"><span lang="EN"style="font-size:12.0pt">FROM</span></font><pre style="background:white"><font face="Courier New" size="2"><spanlang="EN" style="font-size:10.0pt"> table</span></font></pre><pre style="background:white"><font face="CourierNew" size="2"><span lang="EN" style="font-size:10.0pt"> JOIN (SELECT id, count(id) AS count FROM... yoursubquery) AS x ON </span></font></pre><p class="MsoNormal" style="background:white"><font face="Times New Roman" size="3"><spanlang="EN" style="font-size:12.0pt">(table.id = x.id)<br /> ORDER BY</span></font><pre style="background:white"><fontface="Courier New" size="2"><span lang="EN" style="font-size:10.0pt"> x.count</span></font></pre><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font></div>