Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a9MJk-0003Ya-MM for pgsql-sql@arkaria.postgresql.org; Thu, 17 Dec 2015 00:21:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a9MJk-00021Q-93 for pgsql-sql@arkaria.postgresql.org; Thu, 17 Dec 2015 00:21:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9MIk-0000sG-Rs for pgsql-sql@postgresql.org; Thu, 17 Dec 2015 00:20:11 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a9MIh-0002Zt-V2 for pgsql-sql@postgresql.org; Thu, 17 Dec 2015 00:20:09 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 9CDFC20A2B for ; Wed, 16 Dec 2015 19:20:06 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Wed, 16 Dec 2015 19:20:06 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=2A3Vt0qrp2GAvFO/MCKBhPwi190=; b=Ot3V91 6uT+xFEJ3rsZflgfuWlYQ786O87+EEBu5bLCEuc7bHoHltklurBSN+E95BxAfoRx Ap9Vwbikmj0GItgAgIIq5r/zIIBWdp4FCxjwtvY/XX+0EdFkB5QoX680uI5NUS6J bH9Pb9x+FEJ7ShlwtpHwyzN2/PypfOQFlQriA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=2A3Vt0qrp2GAvFO /MCKBhPwi190=; b=VR2F96NL22aocy2PXDt5tRcJNYj7GRJxitabZ/s5psgFBBa UHMhzJvXwadYHKD1cHMEjegrKW7JOIuYhfH6g5nZL0uaca6rfynCjP9ZacvCr6kK BqmlD2BJieoqSxQ3m+zJH8cxnCgHPxLr5eOgK/+3xD6tea9Efj+dXdA9j5r0= X-Sasl-enc: fzlfiTXu2fyyxBqQ0qi1D7fLKpEcS/a5sw+PCsv4aWm9 1450311606 Received: from [192.168.1.2] (65-102-182-223.tukw.qwest.net [65.102.182.223]) by mail.messagingengine.com (Postfix) with ESMTPA id 016FA680084; Wed, 16 Dec 2015 19:20:05 -0500 (EST) Subject: Re: Help with complicated query (total SQL newb!) To: britt_mcclafferty , pgsql-sql@postgresql.org References: <1450303614561-5877942.post@n5.nabble.com> From: Adrian Klaver Message-ID: <5671FF93.809@aklaver.com> Date: Wed, 16 Dec 2015 16:19:31 -0800 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <1450303614561-5877942.post@n5.nabble.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) 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 On 12/16/2015 02:06 PM, britt_mcclafferty wrote: > 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? What error are you getting? When I do this: test=> select max(TRIM(TRAILING ' total bookmarks' FROM '34 total bookmarks')); max ----- 34 it works. Do you have empty strings in your Data1 and Data2 columns? > > 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 FYI, it is generally better to just cut and paste your results directly into the post. > > *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? Have you looked at DISTINCT ON: http://www.postgresql.org/docs/9.4/interactive/sql-select.html#SQL-DISTINCT > > 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. That depends on what you are looking at, the overall aggregated totals for a user or the totals by event or some other parameter. Probably need to show the actual 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. > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql