Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U010K-0006YF-JO for pgsql-sql@arkaria.postgresql.org; Tue, 29 Jan 2013 02:32:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U010K-0003kd-2u for pgsql-sql@arkaria.postgresql.org; Tue, 29 Jan 2013 02:32:56 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U010I-0003jd-OG for pgsql-sql@postgresql.org; Tue, 29 Jan 2013 02:32:54 +0000 Received: from dub0-omc2-s11.dub0.hotmail.com ([157.55.1.150]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U010H-0007dQ-7Q for pgsql-sql@postgresql.org; Tue, 29 Jan 2013 02:32:54 +0000 Received: from DUB116-W6 ([157.55.1.137]) by dub0-omc2-s11.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Mon, 28 Jan 2013 18:32:52 -0800 X-EIP: [8v2TNuswp8dCtfBxm4Dek1PlZrT6fg59] X-Originating-Email: [kong_mansatiansin@hotmail.com] Message-ID: Content-Type: multipart/alternative; boundary="_8023ae8b-619c-4252-aea9-718d1d03c10b_" From: Kong Man To: Subject: Writeable CTE Not Working? Date: Mon, 28 Jan 2013 18:32:51 -0800 Importance: Normal MIME-Version: 1.0 X-OriginalArrivalTime: 29 Jan 2013 02:32:52.0018 (UTC) FILETIME=[EFAFE120:01CDFDC8] X-Pg-Spam-Score: 0.3 (/) 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 --_8023ae8b-619c-4252-aea9-718d1d03c10b_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Can someone explain how this writable CTE works? Or does it not? What I tried to do was to make those non-null/non-empty values of suppliers= .suppliercode unique by (1) nullifying any blank=2C but non-null=2C supplie= rcode=2C then (2) appending the supplierid values to the suppliercode value= s for those duplicates. The writeable CTE=2C upd_code=2C did not appear to= work=2C allowing the final UPDATE statement to=2C unexpectedly=2C fill wha= t used to be empty values with '-'||suppliercode. WITH upd_code AS ( UPDATE suppliers SET suppliercode =3D NULL=20 WHERE suppliercode IS NOT NULL=20 AND length(trim(suppliercode)) =3D 0 ) =2C ranked_on_code AS ( SELECT supplierid =2C trim(suppliercode)||'-'||supplierid AS new_code =2C rank() OVER (PARTITION BY upper(trim(suppliercode)) ORDER BY supplier= id) FROM suppliers WHERE suppliercode IS NOT NULL AND NOT inactive AND type !=3D 'car' ) UPDATE suppliers SET suppliercode =3D new_code FROM ranked_on_code WHERE suppliers.supplierid =3D ranked_on_code.supplierid AND rank > 1=3B I have seen similar behavior in the past and could not explain it. Any exp= lanation is much appreciated. Thanks=2C -Kong = --_8023ae8b-619c-4252-aea9-718d1d03c10b_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Can someone explain how this writable CTE works? =3B Or does it not?
What I tried to do was to make those non-null/non-empty values of supp= liers.suppliercode unique by (1) nullifying any blank=2C but non-null=2C su= ppliercode=2C then (2) appending the supplierid values to the suppliercode = values for those duplicates. =3B The writeable CTE=2C upd_code=2C did n= ot appear to work=2C allowing the final UPDATE statement to=2C unexpectedly= =2C fill what used to be empty values with '-'||suppliercode.

WITH u= pd_code AS (
 =3B UPDATE suppliers SET suppliercode =3D NULL
&nb= sp=3B WHERE suppliercode IS NOT NULL
 =3B AND length(trim(supplierc= ode)) =3D 0
)
=2C ranked_on_code AS (
 =3B SELECT supplierid =3B =2C trim(suppliercode)||'-'||supplierid AS new_code
 =3B = =2C rank() OVER (PARTITION BY upper(trim(suppliercode)) ORDER BY supplierid= )
 =3B FROM suppliers
 =3B WHERE suppliercode IS NOT NULL
=  =3B AND NOT inactive AND type !=3D 'car'
)
UPDATE suppliers
S= ET suppliercode =3D new_code
FROM ranked_on_code
WHERE suppliers.supp= lierid =3D ranked_on_code.supplierid
AND rank >=3B 1=3B

I have = seen similar behavior in the past and could not explain it. =3B Any exp= lanation is much appreciated.
Thanks=2C
-Kong
= --_8023ae8b-619c-4252-aea9-718d1d03c10b_--