Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLs8j-0006la-CT for pgsql-general@arkaria.postgresql.org; Thu, 24 May 2018 15:26:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fLs8i-0001Zc-23 for pgsql-general@arkaria.postgresql.org; Thu, 24 May 2018 15:26:52 +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.89) (envelope-from ) id 1fLs8h-0001ZR-Ns for pgsql-general@lists.postgresql.org; Thu, 24 May 2018 15:26:51 +0000 Received: from mail-ot0-x230.google.com ([2607:f8b0:4003:c0f::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fLs8e-00011w-PB for pgsql-general@lists.postgresql.org; Thu, 24 May 2018 15:26:51 +0000 Received: by mail-ot0-x230.google.com with SMTP id 15-v6so2397498otn.12 for ; Thu, 24 May 2018 08:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Pjv4f8OdeSpcousgtWf7GXVIaUoXCKANFQERXFOL5ks=; b=Itgi+EryKY915G9+5Gtd+A+OgbYwy/RkdJPZ5PWyuxHLgUt4JbjSKkSmXhuY7UyC4h y303GId9igIBhZLat4tV8VwJ7nuG4u4IBjbnm61bgCirSM0r+QtLuQr8h/wqioObGwzP sK1pzvxN+1SBrosgWvWWmZ1KEPahoVckXgw0XhPxQNg528KJnVS1tLmftJPzoS8L2JEI rLuGxzVeqJqpPhYHvi4FBcOWOiQC+hcZidm4+bfQ11ZBLpqXZSmnQmipMPf3Trjo7sAr 4xaxnTrvxItmqS7wvwTWnuX9xDBDkXON6xOuzqkKXxdjiVmxZAQZ0KBdIpZVYDrrFNZj J2iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=Pjv4f8OdeSpcousgtWf7GXVIaUoXCKANFQERXFOL5ks=; b=P363xTnckCP2eu6Vjoq4sK0SjDsoUL/74eHTDfdCmQBoAtvxnMlbBK6xIj4/YRiUDv gtKvs4dJ6cok52NP4Wfl88T7FPLPnG7X1CowzaOdt/0ZPyO0E30J7fVPXGYL+G0GIJ+h ZwmBmBSB+qpnk7+qgXgVHtWBZC/GCYYR/Uv8dFoIZCqHyC6F2AAn7DxdD1gkxPh3U8iv g/HQ83jCJCCIDxmYzD3nN9RnWgtgVkfEJ3eKBilxLsxNL1zUqxUeNJ9/o/M1CWDlFLJ5 DkF9AUg3CgwIFd7nRrjUkQgjEC+na4mcQV/z1WTIZbH8AEcXH/JIpRZmGqZh+fH7V4ht 7B5g== X-Gm-Message-State: ALKqPwejQ5tWDvBUgNTyDpYvaIYSB615/AVPNPEmOeHckc3v4BOHW2DT QNaLOMgORnD4EgaYCZ4FGRMf/g== X-Google-Smtp-Source: AB8JxZqZ4glNYRXvKGIU987/csIswD7cq68PgcAhxUjr8rH1MGZ1KbWCbu0CrrakrpiHw8tIfALjxQ== X-Received: by 2002:a9d:b61:: with SMTP id p30-v6mr5118093otd.32.1527175606251; Thu, 24 May 2018 08:26:46 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id l2-v6sm14657960otd.38.2018.05.24.08.26.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 May 2018 08:26:45 -0700 (PDT) Subject: Re: computing z-scores To: pgsql-general@lists.postgresql.org References: <2CC1AFF5-BB3A-499D-8969-C5B3388ED534@northwestern.edu> From: Ron Message-ID: Date: Thu, 24 May 2018 10:26:44 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <2CC1AFF5-BB3A-499D-8969-C5B3388ED534@northwestern.edu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/24/2018 10:15 AM, Martin Mueller wrote: > You construct a z-score for a set of values by subtracting the average from the value and dividing the result by the standard deviation. I know how to do this in a two-step procedure. First, I compute the average and standard deviation. In a second run I use the formula and apply it to each value. > > Is there a way of doing this in a single-step procedure or can you chain the two parts together in one query? This goes beyond my SQL competence. What about this? SELECT value, (value - AVG(value))/stddev(value) as zvalue FROM sometable WHERE some conditions GROUP by value -- Angular momentum makes the world go 'round.