I’m curious about ctes in postgres db , I tried to use join inside but i got an error!
My questions ?
Could i use where of multiple tables instead of inner join with a cte ?
Is there another way to save table results if not cte suitable? I revised variables and they only suitable for one row ?
I used views, it is a better alternative. if somebody curious.
Related
I have very little experience with PostgreSQL and looking for a way using pgadmin to list out the rows across tables with foreign keys so that I can quickly update some records without doing this via a webapp.
All my tables are prefixed with app
I can select * from one table, but don't know what I'm doing beyond that to get the related table data.
SELECT app_choice.choice_text, app_choice.question_id, app_choice_choice_value
FROM app_choice;
INNER JOIN app_projectquestionnairequestion
ON app.questionnaire.id = app_projectquestionnairequestion.id;
Some my choice table is linked to my projectquestionnairequestion table. So basically every question in projectquestionnairequestion has a related choice in the choice table.
But not all questions have a choice available so i need a way to list all the questions to let me add a choice to it?
Sorry for the bad explanation. It's hard to explain when I dont know the terms
Thanks
This is not an answer but a comment that doesn't fit in the comments section. Don't upvote it since I'll remove it.
Your syntax is a bit wrong, but you are not too far from it. Also, you should use "table aliases" as well (c and q below). Your query could look like:
SELECT
c.choice_text, c.question_id, c.choice_value,
q.name -- this would be the "name" column of the second table
FROM app_choice c
INNER JOIN app_projectquestionnairequestion q
ON q.id = c.question_id;
While I am doing monitoring with my PostgreSql server, I found out that this query frequently shown up.
select t.oid, t.typname from pg_type t where t.typtype='b'
and because I have a lot of table, that query will take long time to process.
Is it possible for me to custom that query into another query like
select t.oid, t.typname
from pg_type t
left join pg_type base_type on t.typelem=base_type.oid
where t.typtype='b'
and (base_type.oid is null or base_type.typtype='b')
or maybe another suggestion will be good
thank you
Try to find out what issues this query, then you can fix the problem. It may well be some ORM, framework or other tool.
Having extremely many tables, users, schemas or other objects is a bad idea, because the catalogs are not built and indexed for that.
I am new to using CTEs but I work with a humongous database and think they would cause less stress to the system that subqueries. I'm not sure if what I want to do is possible.
I have 2 CTEs with different columns from different tables but each CTE has the same sample_num (same data type of int) in them that could be used to join them if possible. I use the first CTE to limit the data for samples. I want the second CTE to look into the first and if the sample numbers match, include that sample number data in the second CTE. The reason I have the second CTE is because I use it's data to create a pivot table.
Ultimately what I want to do in my outer query is to use the fields from the first CTE and add the pivot table columns from the second CTE to the left. Basically marry the two CTEs side by side in the final outer query.
Is this possible or am I making this a lot harder than it needs to be. Remember, I work on a huge database with thousands of users.
I have an SQL script I want to replicate inside tableau using tableau's joins.
SQL script (script simplified just for explanation):
Select *
From transactions
Inner join data1
UNION
Select *
From transactions
Inner join data2
How do replicate this in tableau using joins n unions? (No tableau custom SQL please)
You can do manual unions or wildcard unions: Tableau Help
Depending on your data connection, some features of it may or may not be supported.
Same with joins, lots of ways to do it: Tableau Help
Make sure you're looking up the docs and searching for an answer before posting. These types of this are easy to find.
The task at hand is to select musicians (pid) and the amount of instruments each play, including instruments only played at a concert - these instruments might not be in the [plays] table.
I've solved it, but I read that sub queries in a from clause should be avoided if possible. Just out of curiosity, can anyone show me a more effective way? Or is this a good solution? I'm using psql.
select a.pid, sum(a.instr)
from
(
select pid, count(instr) as instr from plays group by pid
union all
select pid, count(instr) as instr from concert group by pid
) as a
group by a.pid;
Such queries are not a issue. The query optimizer of the database will take care of getting the best out of this query. In some cases a INNER JOIN will be converted to exactly the same execution plan as a sub-SELECT.
If you think the query has a problem you can always fire up the EXPLAIN ANALYZE function of psql. This will give you a overview what your query is actually doing. This way you can also compare different ways to write the query.
The example you gave... I do not think you can solve this without sub-queries very easily. I think the way you chose is good. Anything involving some LEFT JOINs will be more difficult to read.
Advantages
Subqueries are advantageous because they structure the query to isolate each part of the statement, perform the same operation that would ordinarily require complex joins and unions and are easier to read.
Disadvantages
When using subqueries the query optimizer may need to perform additional steps so they take longer to execute than a join.
Uncorrelated subqueries are executed one time for each row of the parent query. If this kind of subqueries process a great amount of data, you should expect it to take a very long time to process the data.
Possible solution:
You can create temporary tables for storing the data of subqueries, then use a JOIN for completing the query. Remember that using a JOIN is better than using a subquery. How to Create a Table
Use a with clause. WITH provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for one query. It allows you to execute a subquery just once instead of executing it for each row. How to Use With Clause
NOTICE: You should avoid using UNION or UNION ALL.