common-table-expression

Postgres hierarchical (jsonb) CTE unnecessarily slow

て烟熏妆下的殇ゞ 提交于 2020-01-07 03:07:28
问题 I have a JsonB column in my table which holds hierarchical information. MyTable (id uuid, indexes jsonb, content bytea) Now if I create a CTE say WITH RECURSIVE hierarchy(pid, id, content) AS ( --load first parents SELECT t.indexes ->> 'parentId' as pId, t.id, t.content FROM MyTable c JOIN MyTable t ON t.indexes ->> 'Id' = c.indexes ->> 'parentId' WHERE c.Id = ANY('{..Some UUIDS}') UNION SELECT t.indexes ->> 'parentId' as pId, t.id, t.content FROM hierarchy h, MyTable t WHERE t.indexes ->>

How to write involved recursive subquery in sqlplus w/ multiple tables to trace nodes?

瘦欲@ 提交于 2020-01-06 06:35:24
问题 I am trying to create a report that maps loads on a circuit to their respective power source (Transformer). All of the devices are in separate tables and interconnected in the database: Loads, Switches, Lines, Wires, Transformers. For simplicity's sake, I will show an example where a load is connected to a transformer directly through switches only: Load table: LoadNumber, SectionNumber, BusNumber 100 54 3000 Switch table: SwitchNumber, FromSectionNumber, ToSectionNumber, State, BusNumber 1

jOOQ: “error: relation CTE does not exist”

岁酱吖の 提交于 2020-01-06 05:32:07
问题 Using jOOQ 3.11.2 and Postgres 9.4, I am trying to re-use one jOOQ CTE in the definition of a second CTE. The following is incorrect per StackOverflow question How to re-use one CTE in another CTE in jOOQ CommonTableExpression<...> cteTwo = name().fields().as(select( with(cteOne ... ).from(cteOne) ); Based on the above answer I omitted the with(cteOne) clause in cteTwo completely. I tried: import static org.jooq.impl.DSL.*; CommonTableExpression<...> cteOne = name().fields().as(select(...);

Better way than multiple SELECT statements?

自作多情 提交于 2020-01-06 01:30:08
问题 I'm creating a web app that displays a pie chart. In order to get all the data for the chart from a PostgreSQL 9.3 database in a single HTTP request, I'm combining multiple SELECT statements with UNION ALL — here's a portion: SELECT 'spf' as type, COUNT(*) FROM (SELECT cai.id FROM common_activityinstance cai JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id JOIN common_activitysetting cas ON cas.id = cais.id JOIN quizzes_quiz q ON q.id = cai.activity_id WHERE cai

Loop in function does not work as expected

丶灬走出姿态 提交于 2020-01-05 21:10:23
问题 Using PostgreSQL 9.0.4 Below is a very similar structure of my table: CREATE TABLE departamento ( id bigserial NOT NULL, master_fk bigint, nome character varying(100) NOT NULL CONSTRAINT departamento_pkey PRIMARY KEY (id), CONSTRAINT departamento_master_fk_fkey FOREIGN KEY (master_fk) REFERENCES departamento (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) And the function I created: CREATE OR REPLACE FUNCTION fn_retornar_dptos_ate_raiz(bigint[]) RETURNS bigint[] AS $BODY$ DECLARE

Loop in function does not work as expected

六月ゝ 毕业季﹏ 提交于 2020-01-05 21:05:14
问题 Using PostgreSQL 9.0.4 Below is a very similar structure of my table: CREATE TABLE departamento ( id bigserial NOT NULL, master_fk bigint, nome character varying(100) NOT NULL CONSTRAINT departamento_pkey PRIMARY KEY (id), CONSTRAINT departamento_master_fk_fkey FOREIGN KEY (master_fk) REFERENCES departamento (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) And the function I created: CREATE OR REPLACE FUNCTION fn_retornar_dptos_ate_raiz(bigint[]) RETURNS bigint[] AS $BODY$ DECLARE

Oracle Table Function from CTE

大兔子大兔子 提交于 2020-01-05 09:14:10
问题 I'd like to create a table function in oracle from the following query: WITH A AS ( SELECT * FROM PART_TABLE WHERE PART_NO LIKE part_num ) SELECT * FROM A; 'part_num' being a parameter passed into the function. I am having trouble with the syntax. This is what I have tried: CREATE OR replace FUNCTION part_test_f(search_part IN varchar2) RETURN part_test_t PIPELINED AS rec PART_TEST; CURSOR cur(part_num) IS WITH A AS ( SELECT * FROM PART_TABLE WHERE PART_NO LIKE part_num ) SELECT * FROM A;

Oracle Table Function from CTE

元气小坏坏 提交于 2020-01-05 09:13:36
问题 I'd like to create a table function in oracle from the following query: WITH A AS ( SELECT * FROM PART_TABLE WHERE PART_NO LIKE part_num ) SELECT * FROM A; 'part_num' being a parameter passed into the function. I am having trouble with the syntax. This is what I have tried: CREATE OR replace FUNCTION part_test_f(search_part IN varchar2) RETURN part_test_t PIPELINED AS rec PART_TEST; CURSOR cur(part_num) IS WITH A AS ( SELECT * FROM PART_TABLE WHERE PART_NO LIKE part_num ) SELECT * FROM A;

How to calculate RowTotal of CTE that run in less time

我的未来我决定 提交于 2020-01-05 05:50:13
问题 I have store procedure which have following structure WITH ItemsContact ( IsCostVariantItem ,ItemID ,AttributeSetID ,ItemTypeID ,HidePrice ,HideInRSSFeed ,HideToAnonymous ,IsOutOfStock ,AddedOn ,BaseImage ,AlternateText ,SKU ,[Name] ,DownloadableID ,[Description] ,ShortDescription ,[Weight] ,Quantity ,Price ,ListPrice ,IsFeatured ,IsSpecial ,ViewCount ,SoldItem ,TotalDiscount ,RatedValue ,RowNumber ) AS ( SELECT ------, ROW_NUMBER() OVER ( ORDER BY i.[ItemID] DESC ) AS RowNumber FROM -------

How to re-use one CTE in another CTE in jOOQ

半城伤御伤魂 提交于 2020-01-05 04:09:13
问题 In jOOQ am re-using a CTE in a later CTE. I am trying to summarise student completion records by year and school. I am using jOOQ 3.11.2 and postgres 9.4. I have working SQL code. However in jOOQ, I am getting null values returned. This appears to be a problem with how I am re-using one CTE in a later CTE. At first, I thought it might be a problem with the use of count(). From the manual, it looks like count() is being used correctly. As a test, I removed all reference to count() in the query