I have a table containing account information:
And another table, containing transactions information:
I'd like to retrieve both the titles from transactions.from_acc_id and transactions.to_acc_id
So far, I'm only able to retrieve either one or the other with the following JOIN:
SELECT transactions.transaction_type,
transactions.from_acc_id,
transactions.to_acc_id,
transactions.amount,
account.title AS "ACCOUNT DESTINATION"
FROM transactions
JOIN account
ON transactions.to_acc_id = account.acc_id
This gives me the title of transactions.to_acc_id.
How can I add another field containing the title of transactions.from_acc_id with the same SELECT statement, please ?
Thanks
EDIT: I'd like to keep all field from the Select statement, adding the title of transactions.from_acc_id where is pertinent
You join your account table in twice and give each instance its own alias. Furthermore, to ensure that every record from transactions table shows up and only those records from your accounts table (both source and destination), use a LEFT OUTER JOIN instead of your implicit INNER JOIN that you are currently using.
SELECT transactions.transaction_type,
transactions.from_acc_id,
transactions.to_acc_id,
transactions.amount,
dest.title AS "ACCOUNT DESTINATION",
src.title AS "ACCOUNT SOURCE"
FROM transactions
LEFT OUTER JOIN account as dest
ON transactions.to_acc_id = dext.acc_id
LEFT OUTER JOIN account as src
ON transactions.from_acc_id = src.acc_id
For more information on types of joins that are available in most databases, check out W3Schools SQL Joins page
Simply join the table twice. Use table aliases to tell the source account from the target account.
SELECT
t.transaction_type,
t.from_acc_id,
t.to_acc_id,
t.amount,
from_acc.title AS from_account,
to_acc.title AS to_account
FROM transactions t
LEFT JOIN account from_acc ON from_acc.acc_id = t.from_acc_id
LEFT JOIN account to_acc ON to_acc.acc_id = t.to_acc_id
Related
Hi I am working with Postgres, I have one role "my_role", and I want to update records from one table only where my corporate_id is related to other table.
I want to create a Policy to person table, and I have a corporate_id from my corporate table to drive to get this information would be something like these:
SELECT * FROM person p
INNER JOIN person_brand a ON p.person_id=a.person_id
INNER JOIN brand b ON a.brand_id=b.brand_id
INNER JOIN corporate c on b.corporate_id=c.corporate_id
WHERE c.corporate_id=corporate_id
I my policy will be something like these:
ALTER TABLE core.person ENABLE ROW LEVEL SECURITY;
CREATE POLICY person_corporation_all
ON person
AS PERMISSIVE
FOR UPDATE
TO "my_role"
USING (EXISTS(SELECT 1 FROM person p
INNER JOIN person_brand a ON p.person_id=a.person_id
INNER JOIN brand b ON a.brand_id=b.brand_id
INNER JOIN corporate c on b.corporate_id=c.corporate_id
WHERE c.corporate_id=corporate_id));
But show me this error:
ERROR: column reference "corporate_id" is ambiguous
SQL state: 42702
What I need to send as variable into my query?
Regards
You will have a nested policy because inside the verification you have the person table again, you will need to remove it, and refers to the columns using the name of the table person, for example:
CREATE POLICY person_corporation_all
ON person
AS PERMISSIVE
FOR UPDATE
TO "my_role"
USING (EXISTS(SELECT 1 FROM person_brand a
INNER JOIN brand b ON a.brand_id=b.brand_id
INNER JOIN corporate c on b.corporate_id=c.corporate_id
WHERE a.person_id=person.person_id and c.corporate_id=person.corporate_id));
I'm working with Hibernate thus HQL, linked to a PostgreSQL database.
I have a table users and a table teams that are linked with a ManyToMany condition throught the table teams_users.
I'd like to update or select the table team so the property usersCount takes the amount of users belonging to a team.
I do not want to add a #Formula to my Entity Class, because I don't want it to be executed all the time, that's too wastful on big JOIN FETCH query where I do not need the count.
I other words, I'd like to find the HQL equivalent of the following PSQL query
UPDATE teams t
SET users_count = (SELECT COUNT(ut.*)
FROM teams t1
LEFT JOIN teams_users tu
ON t1.id = tu.team_id
WHERE t1.id = t.id
GROUP BY t1.id);
OR
An equivalent of the following
SELECT t.*, count(tu.*) AS users_count
FROM teams t
LEFT JOIN teams_users tu
ON t.id = tu.team_id
GROUP BY t.id;
Unsuccessful tries (to get an idea)
UPDATE Team t SET
t.usersCount = COUNT(t.users)
UPDATE Team t SET
t.usersCount = (SELECT COUNT(t1.users) FROM Team t1 WHERE t1.id = t.id)
SELECT t, count(t.users) AS t.usersCount
FROM Team t
I've found the solution for the UPDATE query.
It simply is
UPDATE Team t
SET t.usersCount = (SELECT COUNT(u) from t.users u)
It makes an extra join on the table users whilst the table teams_users would be enought but well... It works.
If anyone has ths solution for the SELECT one, I'm still curious !
I'm completely new to SQL and have a question. I am using is PostgreSQL.
I have two tables called "employees" and "offices"
The table "employees" have a list of unique employees with each having an OfficeID (The office where they work).
What I want to do is to "count" the number of appearances of the Office_ID and take that count into the table "offices" where the "office_ID" have a column called "number_of_employees".
Being completely new to SQL the only thing I have managed to even come close to this is fore example.
SELECT COUNT(*)
FROM employees
WHERE office_id = 203
But this only selects and gives the sum of rows with the id "203" that has to be manually entered into "number of empolyees"
What I want is a trigger function that updates the field "number_of_empolyees" when a new record is inserted into the table "empolyees"
A view is the way to go here.
I am assuming since you're completely new to SQL, you're unsure how to make it work (Edit: just seen your comment after posting :^D) .
The correct way to count employees for each office is:
SELECT office_id, COUNT(*) as employeeCount
FROM employees
GROUP BY office_id
Note how your WHERE office_id = XXX has been replaced by a GROUP BY office_id in order to count employees for all offices in a single query.
That being done, we can use it inside the view.
Be careful about the JOIN: I believe in your schema, an office may have no employee (for instance, right after you created it or right before you delete it). We will handle that part with a LEFT JOIN.
CREATE VIEW OfficeWithEmployeeCount AS
SELECT Offices.*, EmployeeCount
FROM Offices
LEFT OUTER JOIN (SELECT office_id, COUNT(*) as EmployeeCount FROM Employees GROUP BY office_id) T
ON Offices.office_id = T.office_id
Note: to avoid having NULL returned in EmployeeCount for empty offices, you may want to write:
CREATE VIEW OfficeWithEmployeeCount AS
SELECT Offices.*, COALESCE(EmployeeCount,0)
FROM ...
Okay. So the question that I got asked by the teacher was this:
(5 marks) Construct a SQL query on the dvdrental database that uses a natural join of two or more tables and an additional where condition. (E.g. find the titles of films rented by a particular customer.) Note the hints on the course news page if your query returns nothing.
Here is the layout of the database im working with:
http://www.postgresqltutorial.com/wp-content/uploads/2013/05/PostgreSQL-Sample-Database.png
The hint to us was this:
PostgreSQL hint:
If a natural join doesn't produce any results in the dvdrental DB, it is because many tables have the last update: timestamp field, and thus the natural join tries to join on that field as well as the intended field.
e.g.
select *
from film natural join inventory;
does not work because of this - it produces an empty table (no results).
Instead, use
select *
from film, inventory
where film.film_id = inventory.film_id;
This is what I did:
select *
from film, customer
where film.film_id = customer.customer_id;
The problem is I cannot get a particular customer.
I tried doing customer_id = 2; but it returns a error.
Really need help!
Well, it seems that you would like to join two tables that have no direct relation with each other, there's your issue:
where film.film_id = customer.customer_id
To find which films are rented by which customer you would have to join customer table with rental, then with inventory and finally with film.
The task description states
Construct a SQL query on the dvdrental database that uses a natural join of two or more tables and an additional where condition.quote
UPDATE user
SET balance = balance + p.amount
FROM payments p WHERE user.id = p.user_id AND p.id IN (36,38,40)
But it adds to the balance, only the value amount of the first payment 1936.
Please help me how to fix it, i do not want to make cycle in the code to run a lot of requests.
In a multiple-table UPDATE, each row in the target table is updated only once, even it's returned more than once by the join.
From the docs:
When a FROM clause is present, what essentially happens is that the target table is joined to the tables mentioned in the fromlist, and each output row of the join represents an update operation for the target table. When using FROM you should ensure that the join produces at most one output row for each row to be modified. In other words, a target row shouldn't join to more than one row from the other table(s). If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable.
Use this instead:
UPDATE user u
SET balance = balance + p.amount
FROM (
SELECT user_id, SUM(amount) AS amount
FROM payment
WHERE id IN (36, 38, 40)
GROUP BY
user_id
) p
WHERE u.id = p.user_id