public inbox for pgsql-novice@postgresql.org  
help / color / mirror / Atom feed
From: Ibrahim Shaame <ishaame@gmail.com>
To: pgsql-novice@lists.postgresql.org
Subject: Merging two queries and I get syntax errors
Date: Sat, 21 Mar 2026 17:31:42 +0300
Message-ID: <CAJOWwD7r5e_s=X1ZJEc0jVYdcGiMnkGik0m=Q=1t8hmBTT0vFQ@mail.gmail.com> (raw)

I am trying to insert a new column in a working query. When I query I get
the column correctly, but when adding that query to the main column I get
syntax errors Here are the queries:

1 - This one works perfectly:
SELECT (((b.jina::text || ' '::text) || b.baba::text) || ' '::text) ||
b.babu::text AS jina_kamili,
    b.namba,
    a.mlipaji,
    a.kiasi,
    a.imelipwaje,
    to_char(a.kwa_mwezi_wa::timestamp with time zone, 'yyyy'::text) AS
mwaka,
    to_char(a.kwa_mwezi_wa::timestamp with time zone, 'mm'::text) AS mwezi
   FROM michango2 a,
    majina2 b
  WHERE a.mlipaji = b.namba
  ORDER BY ((((b.jina::text || ' '::text) || b.baba::text) || ' '::text) ||
b.babu::text), (to_char(a.kwa_mwezi_wa::timestamp with time zone,
'yyyy'::text)), (to_char(a.kwa_mwezi_wa::timestamp with time zone,
'mm'::text));
******************************************
2 - and this one works perfectly:
SELECT (EXTRACT(YEAR FROM AGE(anza_mchango, CASE WHEN mwisho_mchango ISNULL
THEN NOW() ELSE mwisho_mchango END))*12 + EXTRACT(MONTH FROM
AGE(anza_mchango, CASE WHEN mwisho_mchango ISNULL THEN NOW() ELSE
mwisho_mchango END)))*-1 AS miezi_deni FROM majina2;
**********************************************
3 - But when the second is merged in the first like this:

SELECT (((b.jina::text || ' '::text) || b.baba::text) || ' '::text) ||
b.babu::text AS
jina_kamili, b.namba, a.mlipaji, a.kiasi, a.imelipwaje,
to_char(a.kwa_mwezi_wa::timestamp
with time zone, 'yyyy'::text) AS mwaka, to_char(a.kwa_mwezi_wa::timestamp
with time zone, 'mm'::text) AS mwezi,  (EXTRACT(YEAR FROM AGE(anza_mchango,
CASE WHEN mwisho_mchango ISNULL THEN NOW() ELSE mwisho_mchango END))*12 +
EXTRACT(MONTH FROM AGE(anza_mchango, CASE WHEN mwisho_mchango ISNULL THEN
NOW() ELSE mwisho_mchango END)))*-1 AS miezi_deni

FROM michango2 a, majina2 b

WHERE a.mlipaji = b.namba

ORDER BY ((((b.jina::text || ' '::text) || b.baba::text) || ' '::text) ||
b.babu::text), (to_char(a.kwa_mwezi_wa::timestamp with time zone,
'yyyy'::text)), (to_char(a.kwa_mwezi_wa::timestamp with time zone,
'mm'::text));


Here it does not work and I get the message:

ERROR:  syntax error at or near "a"
LINE 1: ...1 AS miezi_deni FROM michango2 a, majina2 b WHERE a.mlipaji

and it point at a.mlipaji

I would appreciate any hint.

Thanks in advance


reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-novice@postgresql.org
  Cc: ishaame@gmail.com, pgsql-novice@lists.postgresql.org
  Subject: Re: Merging two queries and I get syntax errors
  In-Reply-To: <CAJOWwD7r5e_s=X1ZJEc0jVYdcGiMnkGik0m=Q=1t8hmBTT0vFQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox