public inbox for pgsql-novice@postgresql.org
help / color / mirror / Atom feedMerging two queries and I get syntax errors
3+ messages / 2 participants
[nested] [flat]
* Merging two queries and I get syntax errors
@ 2026-03-21 14:31 Ibrahim Shaame <ishaame@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Ibrahim Shaame @ 2026-03-21 14:31 UTC (permalink / raw)
To: pgsql-novice@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Merging two queries and I get syntax errors
@ 2026-03-21 14:42 David G. Johnston <david.g.johnston@gmail.com>
parent: Ibrahim Shaame <ishaame@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: David G. Johnston @ 2026-03-21 14:42 UTC (permalink / raw)
To: Ibrahim Shaame <ishaame@gmail.com>; +Cc: pgsql-novice@lists.postgresql.org <pgsql-novice@lists.postgresql.org>
On Saturday, March 21, 2026, Ibrahim Shaame <ishaame@gmail.com> wrote:
>
> FROM michango2 a, majina2 b
>
> WHERE a.mlipaji = b.namba
>
>
> 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
>
Nothing seems obviously wrong which leads me to suspect some nonprinting
character issues. Try writing/rebuilding the query in small increments
until you get the error again then manually retype the last added chunk.
Or just take the opportunity to use a more conventional explicit ANSI join
syntax and move the join condition to the ON clause, getting rid of the
where clause with the apparent issue.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Merging two queries and I get syntax errors
@ 2026-03-21 14:52 Ibrahim Shaame <ishaame@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 3+ messages in thread
From: Ibrahim Shaame @ 2026-03-21 14:52 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-novice@lists.postgresql.org <pgsql-novice@lists.postgresql.org>
Thank you David for the quick response. I placed the new column two columns
before and it works. I just can't understand why it does not when added to
the end. But anyway it works where it is now.
Thank you again.
Ibrahim Shaame
On Sat, Mar 21, 2026 at 5:42 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:
> On Saturday, March 21, 2026, Ibrahim Shaame <ishaame@gmail.com> wrote:
>
>>
>> FROM michango2 a, majina2 b
>>
>> WHERE a.mlipaji = b.namba
>>
>>
>> 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
>>
>
> Nothing seems obviously wrong which leads me to suspect some nonprinting
> character issues. Try writing/rebuilding the query in small increments
> until you get the error again then manually retype the last added chunk.
> Or just take the opportunity to use a more conventional explicit ANSI join
> syntax and move the join condition to the ON clause, getting rid of the
> where clause with the apparent issue.
>
> David J.
>
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2026-03-21 14:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-21 14:31 Merging two queries and I get syntax errors Ibrahim Shaame <ishaame@gmail.com>
2026-03-21 14:42 ` David G. Johnston <david.g.johnston@gmail.com>
2026-03-21 14:52 ` Ibrahim Shaame <ishaame@gmail.com>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox