1

I'm having a huge problem with my query, for some reason I just can't get one of the WHERE clauses to work.

This is my SQL:

SELECT COUNT(*) FROM "diets" JOIN "meals" on "idDiet" = "dietId" 
WHERE kcal != 0 AND "diets.createdAt" > '2016-10-2' 
GROUP BY "userIdUser" HAVING count(*) >= 5;

And my error:

ERROR:  column "diets.createdAt" does not exist

My scheme for both tables:

enter image description here enter image description here

Any idea on what I must do for this query to work? Thank you very much, if more information is needed please let me know.

1
  • You should avoid quoted identifiers in general. They are much more trouble then they are worth it. If you never use double quotes you will be fine Commented Oct 10, 2016 at 5:48

2 Answers 2

2

Your quotes are wrong:

SELECT COUNT(*)
FROM "diets" JOIN
     "meals" 
     ON "idDiet" = "dietId" 
WHERE kcal <> 0 AND "diets"."createdAt" > '2016-10-2' 
GROUP BY "userIdUser"
HAVING count(*) >= 5;

The double quotes go around an identifier. A qualified column reference such as diets.createdAt consists of two identifiers, so each needs to have the quotes (if you have them at all).

Otherwise, you are referring to a column whose name is "diets.createdAt". That is, the column name would have a period in it.

Sign up to request clarification or add additional context in comments.

1 Comment

That is amazing, thank you so much!!! I know this was a stupid mistake, but not even my databases professor spotted it when I questioned him. You own!
0
 SELECT COUNT(*) FROM diets a  JOIN  meals b on a.idDiet = b.dietId 
 WHERE a.kcal <> 0 AND a.createdAt > '2016-10-2' 
 GROUP BY a.userIdUser HAVING count(*) >= 5;

1 Comment

The columns were created with double quotes which makes them case sensitive. createdAt is a different column name then "createdAt" - leofontes will have to use double quotes all the time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.