Weird error when trying to query from database [duplicate] - postgresql

This question already has answers here:
PostgreSQL "Column does not exist" but it actually does
(6 answers)
SQL query column does not exist error
(1 answer)
Closed last month.
As the title suggests, I have no clue why this doesn't work. If someone can point out what I am doing wrong it would be sweet.
Here's the current table rows and cols:
Makes table:
id | make
----+---------------
1 | Acura
Models Table:
id | model | makesId
-----+---------------------------------+---------
1 | CL | 1
2 | ILX | 1
3 | Integra | 1
4 | Legend | 1
5 | MDX | 1
6 | NSX | 1
7 | RDX | 1
8 | RL | 1
9 | RLX | 1
I am trying to query from both tables using a simple line with the WHERE clause with the following query:
SELECT models.model, makes.make
FROM models, makes
WHERE models.makesId = makes.id;
funprojectdb=# SELECT models.model, makes.make FROM models, makes WHERE models.makesId = makes.id;
ERROR: column models.makesid does not exist
LINE 1: ...models.model, makes.make FROM models, makes WHERE models.mak...
^
HINT: Perhaps you meant to reference the column "models.makesId".
The goal is to basically show me all of the models associated to the makes id.

Thanks to the people that answered my question.
It was an issue with postgres case sensitivity which I completely forgot about.
I will be re-doing the database columns with proper field names.

Related

PostgreSQL - Setting null values to missing rows in a join statement

SQL newbie here. I'm trying to write a query that generates a scoring table, setting null to a student's grades in a module for which they haven't yet taken their exams (on PostgreSQL).
So I start with tables that look something like this:
student_evaluation:
|student_id| module_id | course_id |grade |
|----------|-----------|-----------|-------|
| 1 | 1 | 1 |3 |
| 1 | 1 | 1 |7 |
| 1 | 2 | 1 |8 |
| 2 | 4 | 2 |9 |
course_module:
| module_id | course_id |
| ---------- | --------- |
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
In our use case, a course is made up of several modules. Each module has a single exam, but a student who failed his exam may have a couple of retries. The same module may also be present in different courses, but an exam attempt only counts for one instance of the module (ie. student A passed module 1's exam on course 1. If course 2 also has module 1, student A has to retake the same exam for course 2 if he also has access to that course).
So the output should look like this:
student_id
module_id
course_id
grade
1
1
1
3
1
1
1
7
1
2
1
8
1
3
1
null
2
4
2
9
I feel like this should have been a simple task, but I think I have a very flawed understanding of how outer and cross joins work. I have tried stuff like:
SELECT se.student_id, se.module_id, se.course_id, se.grade FROM student_evaluation se
RIGHT OUTER JOIN course_module ON course_module.course_id = se.course_id
AND course_module.module_id = se.module_id
or
SELECT se.student_id, se.module_id, se.course_id, se.grade FROM student_evaluation se
CROSS JOIN course_module WHERE course_module.course_id = se.course_id
Neither worked. These all feel wrong, but I'm lost as to what would be the proper way to go about this.
Thank you in advance.
I think you need both join types: first use a cross join to build a list of all combinations of students and courses, then use an outer join to add the grades.
SELECT sc.student_id,
sc.module_id,
sc.course_id,
se.grade
FROM student_evaluation se
RIGHT JOIN (SELECT s.student_id,
c.module_id,
c.course_id
FROM (SELECT DISTINCT student_id
FROM student_evaluation) AS s
CROSS JOIN course_module AS c) AS sc
USING (course_id));

Stored procedure (or better way) to add a new row to existing table every day at 22:00

I will be very grateful for your advice regarding the following issue.
Given:
PostgreSQL database
Initial (basic) query
select day, Value_1, Value_2, Value_3
from table
where day=current_date
which returns a row with following columns
Day | Value_1(int) | Value_2(int) | Value 3 (int)
2019-11-14 | 10 | 10 | 14
It is needed to create a view with this starting information and add a new row every day based on the outcome of initial query executed at 22:00.
The expected outcome tomorrow at 22:01 will be
Day | Value_1 | Value_2 | Value_3
2019-11-14 | 10 | 10 | 14
2019-11-15 | N | M | P
Many thanks in advance for your time and support.

How to update a column in one table from a column in another table in postgres [duplicate]

This question already has answers here:
Postgresql Update with join
(3 answers)
update a column field using inner join two tables in postgres and I am getting an ERROR: table name "TblFacultyMaster" specified more than once
(2 answers)
SQL Updating column in new table with arithmetic function from column in another table
(1 answer)
How does the FROM side of an UPDATE relate to the table targeted for UPDATE?
(2 answers)
How to update table from linking table?
(1 answer)
Closed 4 years ago.
I have 2 tables A and B.
In table A I have a char column and integer column for many2one(ID) of column B.
In table B I have same char column.
Now i have to compare char columns of both tables and update the respecting ID of table B to table A.
Table A
| Name | ID of B |
|------|---------|
| ABC | |
| BCD | |
| CDF | |
Table B
| ID | Name |
|----|------|
| 1 | CDF |
| 2 | ABC |
| 3 | BCD |
How to do this in UPDATE query, if not possible how to do that in INSERT query for whole table. Im having 100s of record in table.

Cognos force 0 on group by

I've got a requirement to built a list report to show volume by 3 grouped by columns. The issue i'm having is if nothing happened on specific days for the specific grouped columns, i cant force it to show 0.
what i'm currently getting is something like:
ABC | AA | 01/11/2017 | 1
ABC | AA | 03/11/2017 | 2
ABC | AA | 05/11/2017 | 1
what i need is:
ABC | AA | 01/11/2017 | 1
ABC | AA | 02/11/2017 | 0
ABC | AA | 03/11/2017 | 2
ABC | AA | 04/11/2107 | 0
ABC | AA | 05/11/2017 | 1
ive tried going down the route of unioning a "dummy" query with no query filters, however there are days where nothing has happened, at all, for those first 2 columns so it doesn't always populate.
Hope that makes sense, any help would be greatly appreciated!
to anyone who wanted an answer i figured it out. Query 1 for just the dates, as there will always be some form of event happening daily so will always give a unique date range.
query 2 for the other 2 "grouped by" columns.
Create a data item in each with "1" as the result (but would work with anything as long as they are the same).
Query 1, left join to Query 2 on this new data item.
This then gives a full combination of all 3 columns needed. The resulting "Query 3" can then be left joined again to get the measures. Final query (depending on aggregation) may need to have the measure data item wrapped with a COALESCE/ISNULL to create a 0 on those days nothing happened.

Indirectly generate sequence numbers for composite primary keys with JPA

I have a JPA entity class with a composite primary key (uid,lid) that in the database should look like this;
UID | LID | ...
---------------
1 | 1 | ...
1 | 2 | ...
1 | 3 | ...
2 | 1 | ...
2 | 2 | ...
2 | 3 | ...
How can I make EclipseLink/JPA generate sequence numbers on the fly, or how can I find out the highest number in the UID-column?
Or if I have a UID but want to add a new LID?
Apologies if this is a too easy question. :)
Composite keys a quite complex thing to me, but I think I start to understand them a bit.
No existing key generator can do that for you but you can write your own. See this answer for some pointers about getting started.