Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a9LJf-00083Z-0p for pgsql-sql@arkaria.postgresql.org; Wed, 16 Dec 2015 23:17:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a9LJe-0008Li-Jg for pgsql-sql@arkaria.postgresql.org; Wed, 16 Dec 2015 23:17:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9KDx-0004wt-Q8 for pgsql-sql@postgresql.org; Wed, 16 Dec 2015 22:07:05 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1a9KDp-0000nh-JF for pgsql-sql@postgresql.org; Wed, 16 Dec 2015 22:07:05 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id 5936951DABB7 for ; Wed, 16 Dec 2015 14:05:46 -0800 (PST) Date: Wed, 16 Dec 2015 15:06:54 -0700 (MST) From: britt_mcclafferty To: pgsql-sql@postgresql.org Message-ID: <1450303614561-5877942.post@n5.nabble.com> Subject: Help with complicated query (total SQL newb!) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.0 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Hi there, I am only just learning SQL and have three specific questions on correct syntax for my queries... *1.* The 'Data1' and 'Data2' columns have stings which contain both numbers and letters. In the query below I have removed the letters and re-named the trimmed versions. I want to cast the remaining numbers to numerics and run aggregate functions on them (ie AVG, MAX etc). However, whenever I wrap the TRIM in a function I am getting errors. How do I fix this? Here is my query SELECT data1, TRIM(TRAILING ' total bookmarks' FROM data1) as Bookmarks_trim, data2, TRIM(TRAILING ' folders' FROM data2) as Folders_trim, event_code, user_id FROM events WHERE event_code =8 And this is what it returns: http://screencast.com/t/DCvey2sAxZ *2. *I want to add an additional parameter to the query above to show only DISTINCT instances of the user_id. The separate query I have for that is below. How do I combine the two? SELECT DISTINCT * FROM (SELECT DISTINCT user_id, event_code, data1, data2 FROM events) AS temp WHERE event_code = 8 This returns: http://screencast.com/t/IXhpix0vLNSp *3. *Lastly, I want to be able to sort by DESC on both the trimmed data1 column and data2 (to see the users with the highest number of folders and bookmarks) I know you do this with ORDER BY but I am not sure where it would go in such a large query. ANY help would be hugely appreciated. -- View this message in context: http://postgresql.nabble.com/Help-with-complicated-query-total-SQL-newb-tp5877942.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql