user11624254
user11624254

Reputation:

PostgreSQL - why is this statement causing a syntax error?

SELECT
        (SELECT COUNT(*) FROM table1) AS count1 WHERE date='2019-06-12',
        (SELECT COUNT(*) FROM table2) AS count2 WHERE date='2019-06-12'

why is this statement causing a syntax error at or near ","?

Upvotes: 2

Views: 61

Answers (3)

fphilipe
fphilipe

Reputation: 10056

Let's rewrite the statement replacing the SELECT in parenthesis with a simple value:

SELECT
        1 AS count1 WHERE date='2019-06-12',
        2 AS count2 WHERE date='2019-06-12'

Now it's easy to see that up to the comma you have a valid SQL query, but you're appending further values that you want to select, which is invalid.

I assume what you want is to have the WHEREs inside the subqueries:

SELECT
        (SELECT COUNT(*) FROM table1 WHERE date='2019-06-12') AS count1,
        (SELECT COUNT(*) FROM table2 WHERE date='2019-06-12') AS count2

Upvotes: 0

Fahmi
Fahmi

Reputation: 37473

need to add where clause inside subquery

SELECT
       (SELECT COUNT(*) FROM table1 WHERE date='2019-06-12') AS count1 ,
       (SELECT COUNT(*) FROM table2 WHERE date='2019-06-12') AS count2 

Upvotes: 3

mkRabbani
mkRabbani

Reputation: 16908

Try this-

SELECT
(SELECT COUNT(*) FROM table1 WHERE date='2019-06-12')  AS count1 ,
(SELECT COUNT(*) FROM table2 WHERE date='2019-06-12') AS count2 

Upvotes: 1

Related Questions