Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3ewt-0002BE-EN for pgsql-general@arkaria.postgresql.org; Wed, 04 Apr 2018 09:43:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f3ews-0006rY-FI for pgsql-general@arkaria.postgresql.org; Wed, 04 Apr 2018 09:43:22 +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.89) (envelope-from ) id 1f3ews-0006qZ-0Z for pgsql-general@lists.postgresql.org; Wed, 04 Apr 2018 09:43:22 +0000 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f3ewo-0001MA-AA for pgsql-general@postgresql.org; Wed, 04 Apr 2018 09:43:20 +0000 Received: by mail-wm0-x241.google.com with SMTP id x82so40664853wmg.1 for ; Wed, 04 Apr 2018 02:43:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=sDbGEOAI7xivqJeKuW/Jrh2oqqT8WAS4emo04Xcfiqo=; b=eZ0ltD6e+J9pTZ+5Ft/IWZd45FAUt0G3zTXo8XxbTRJ1nAZW5g1vIZ6GTKF+WKX+NO zX3VrYKEh06shqCXm+IwhXlybTZq3xXSANvfG6K3II/LKK/hZFrcNrDN7O8TnK78ZRcT LszbZzgUZ2op7oDYZZ7TCd9NK0xhDi/PQuOTxj5MFMXhy0v4FiSkIr8Q6S3Dm+5yA+Fq gW17wjpYKJlJ+Lj7bYT0mspV0L8xeJa7aIZjc/tJi+o5JdRTg5+ftaxB2gwrx9luVL0u P5jB3/s+brSywjDmrgA3NQQk+sETT4qGR0M38kHvkcuiLFf2Dqvw/3whexqXrmQdw1gF iRHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=sDbGEOAI7xivqJeKuW/Jrh2oqqT8WAS4emo04Xcfiqo=; b=kHedmfizw51Gk6vCC8K5Utw5QQ5Oj6UdYFTUVbAkH6w+RK0UOenkW+23uMLgjKbvwJ ES3OtqsiDpwqZc5sJE0VCuepBT22yWPsWa/AB3wzzhOTzgFTcdoI3aMCmVUPPa0QQW1F 6K3G/91mvxvysVi0o+efDVNrWxZXrSHcdGNTUpFDSV3aseqg0ucMNORoK0jZpryv4lbn IAgHDGmGkpCikGH3baCDDS0vRjrsReedrf0mFy3pusMZQDkz4AD/4tvZvO5vEAXQ9zmT mQjPj2yjTpSZGaPUsYs3astXtKePs2FQrz+lybLWw49yS+KApyxZ2G21ryA8gcOxDFFg elNg== X-Gm-Message-State: AElRT7G+RIuEa+MbK3nfgBteN/0/Fwt44XTA6xTTC7NJhhLN6vXyTm7O FAie7lM8WvXgC+3y3RB4dr8= X-Google-Smtp-Source: AIpwx4/JpabpFuZB1uI1DETPE37gI+gwgLDK5shb2/mAU5Oj5s605KuWdtp3ZyelFnYjLDLy01Y8Wg== X-Received: by 10.80.202.72 with SMTP id e8mr19097458edi.204.1522834995318; Wed, 04 Apr 2018 02:43:15 -0700 (PDT) Received: from hollewijn.internal (84-245-13-9.dsl.cambrium.nl. [84.245.13.9]) by smtp.gmail.com with ESMTPSA id c56sm3301723eda.41.2018.04.04.02.43.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Apr 2018 02:43:14 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.2 \(3445.5.20\)) Subject: Re: Concatenate of values in hierarchical data From: Alban Hertroys In-Reply-To: Date: Wed, 4 Apr 2018 11:43:11 +0200 Cc: pgsql-general@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <4945D518-FC1C-49AF-B773-7F4F7FC18E3E@gmail.com> References: To: "Mr. Baseball 34" X-Mailer: Apple Mail (2.3445.5.20) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 2 Apr 2018, at 19:23, Mr. Baseball 34 = wrote: >=20 > I have the data below, returned from a PostgreSQL table using this = SQL: >=20 > SELECT ila.treelevel,=20 > ila.app,=20 > ila.lrflag,=20 > ila.ic,=20 > ila.price,=20 > ila.treelevel-1 as parent,=20 > ila.seq=20 > FROM indexlistapp ila=20 > WHERE ila.indexlistid IN (SELECT il.indexlistid=20 > FROM indexlist il > WHERE il.model =3D '$model' > AND ('$year' BETWEEN il.beginyear AND = il.endyear)=20 > AND il.type =3D '$part')=20 > ORDER BY ila.seq; >=20 > Data Returned >=20 > level app lrflag ic price parent = seq indexlistid > = --------------------------------------------------------------------------= ------- > 1, 'Front', null, null, null, 0, = 27, 439755 > 2, 'V-Series' null, null, null, 1, = 28, 439755 > 3, 'opt J56', null, null, null, 2, = 29, 439755 > 4, 'R.', 'R', '536-01132AR','693.00', 3, = 30, 439755 > 4, 'L.', 'L', '536-01133AL','693.00', 3, = 31, 439755 > 3, 'opt J63', null, null, null, 2, = 32, 439755 > 4, 'R.', 'R', '536-01130R', null, 3, = 33, 439755 > 4, 'L.', 'L', '536-01131L', null, 3, = 34, 439755 > 2, 'exc. V-Series', null, null, null, 1, = 35, 439755 > 3, 'opt JE5', null, null, null, 2, = 36, 439755 > 4, 'AWD', null, null, null, 3, = 37, 439755 > 5, 'R.', null, '536-01142', null, 4, = 38, 439755 > 5, 'L.', null, '536-01143', null, 4, = 39, 439755 > 4, 'RWD', null, null, null, 3, = 40, 439755 > 5, 'R.', null, '536-01143', null, 4, = 41, 439755 > 5, 'L.', null, '536-01142', null, 4, = 42, 439755 > 3, 'opt J55', null, null, null, 2, = 43, 439755 > 4, 'AWD', null, null, null, 3, = 44, 439755 > 5, 'R.', null, '536-01036', null, 4, = 45, 439755 > 5, 'L.', null, '536-01037', null, 4, = 46, 439755 > 4, 'RWD', null, null, null, 3, = 47, 439755 > 5, 'R.', null, '536-01037', null, 4, = 48, 439755 > 5, 'L.', null, '536-01036', null, 4, = 49, 439755 > 1, 'Rear', null, null, null, 0, = 260, 439765 > 2, 'Base', null, null, null, 1, = 261, 439765 > 3, 'opt JE5', null, null, null, 2, = 262, 439765 > 4, 'R.', 'R', '536-01038R', null, 3, = 263, 439765 > 4, 'L.', 'L', '536-01039L', null, 3, = 264, 439765 > 3, 'opt J55', null, null, null, 2, = 265, 439765 > 4, 'R.', 'R', '536-01042R', null, 3, = 266, 439765 > 4, 'L.', 'L', '536-01043L', null, 3, = 267, 439765 > 2, 'V-Series', null, null, null, 1, = 268, 439765 > 3, 'R.', 'R', '536-01134AR', '403.00', 2, = 269, 439765 > 3, 'L.', 'L', '536-01135AL', '466.00', 2, = 270, 439765 >=20 > matching data from indexlist >=20 > model type beginyear endyear indexlistid > --------------------------------------------- > 'CTS', '536', 2009, 2010, 439755 > 'CTS', '536', 2009, 2010, 439765 >=20 > There are primary keys on indexlist (on indexlistid) and indexlistapp = (on indexlistid) but there is no foreign key pointing to the other = table. The indexlistid in indexlist=20 > points directly to the indexlistid in indexlistapp. The parent column = is simply calculated from the treelevel. The tree is built entirely from = the seq and treelevel. >=20 > I need the data to be returned in this format: >=20 > app price ic > --------------------------------------------------------------- > 'Front-V-Series-opt J56-R.', '$693', '536-01132AR' > 'Front-V-Series-opt J56-L.', '$693', '536-01132AL' > 'Front-V-Series-opt J63-R.', null, '536-01130R' > 'Front-V-Series-opt J63-L.', null, '536-01131L' > 'Front-exc. V-Series-opt JE5-AWD-R.', null, '536-01142' > 'Front-exc. V-Series-opt JE5-AWD-L.', null, '536-01143' > 'Front-exc. V-Series-opt JE5-RWD-R.', null, '536-01143' > 'Front-exc. V-Series-opt JE5-RWD-L.', null, '536-01142' > 'Front-exc. V-Series-opt J55-AWD-R.', null, '536-01136' > 'Front-exc. V-Series-opt J55-AWD-L.', null, '536-01137' > 'Front-exc. V-Series-opt J55-RWD-R.', null, '536-01137' > 'Front-exc. V-Series-opt J55-RWD-L.', null, '536-01136' > 'Rear-Base-opt JE5-R.', null, '536-01038R' > 'Rear-Base-opt JE5-L.', null, '536-01039L' > 'Rear-Base-opt J55-R.', null, '536-01042R' > 'Rear-Base-opt J55-L.', null, '536-01043L' > 'Rear-V-Series-R.', '$403.00', '536-01134AR' > 'Rear-V-Series-L.', '$466.00', '536-01135AL' >=20 > I am unsure how to do this in SQL. (=E2=80=A6) > Also, using this recursive sql = (REF:https://stackoverflow.com/questions/26280379/how-to-concatenate-field= -values-with-recursive-query-in-postgresql), I'm able to get it fairly = close.=20 > Just not sure why it is returning 476 rows vs. 34. = http://sqlfiddle.com/#!15/ca1ee/3 >=20 > WITH RECURSIVE the_tree AS ( > =20 > SELECT g.seq, g.app::TEXT, g.price, g.ic, g.treelevel::INTEGER > FROM indexlistapp g > WHERE g.indexlistid in (SELECT il.indexlistid=20 > FROM indexlist il > WHERE il.model =3D 'CTS' > AND ('2010' BETWEEN il.beginyear AND = il.endyear)=20 > AND il.type =3D '536') > AND g.treelevel =3D 1 > =20 > =20 > UNION > =20 > SELECT t.seq, t.app || ', ' || g.app AS app, t.price, t.ic, = t.treelevel::INTEGER + 1 > FROM the_tree AS t > INNER JOIN indexlistapp g ON g.treelevel =3D t.treelevel + 1 > WHERE g.indexlistid in (SELECT il.indexlistid=20 > FROM indexlist il > WHERE il.model =3D 'CTS' > AND ('2010' BETWEEN il.beginyear AND = il.endyear)=20 > AND il.type =3D '536') > ) > SELECT * from the_tree; >=20 >=20 > Can anyone help me out here? Your JOIN in the recursive part of the CTE should probably be ON = g.parent =3D t.treelevel AND g.indexlistid =3D t.indexlistid. Since both = the initial and recursive part use the same value for indexlistid, the = subquery in the recursive part is superfluous. You should also take the order in seq into account for items on the same = node, if that is possible in your data, otherwise there is no guarantee = that you'll get your string components in the correct order. I think you need something closer to this: WITH RECURSIVE the_tree AS ( SELECT g.seq, g.app::TEXT, g.price, g.ic, g.treelevel::INTEGER, = g.indexlistid FROM indexlistapp g WHERE g.indexlistid IN ( SELECT il.indexlistid FROM indexlist il WHERE il.model =3D 'CTS' AND '2010' BETWEEN il.beginyear AND il.endyear AND il.type =3D '536' ) AND g.parent =3D 0 UNION ALL SELECT t.seq, t.app || ', ' || g.app AS app, t.price, t.ic, = g.treelevel, g.indexlistid FROM the_tree t INNER JOIN indexlistapp g ON g.parent =3D t.treelevel AND = g.indexlistid =3D t.indexlistid ORDER BY g.seq ) SELECT * FROM the_tree Another observation is that you only seem interested in the leaf nodes = of your tree. Recognising leaf nodes in your hierarchical query can be = achieved by adding a column that tests the existence of a node with the = current node as its parent - if it does not exist, this node is a leaf = node. The result of SELECT * FROM the_tree can then be filtered to return leaf = nodes only. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.