common-table-expression

Accruing over time (non-overlapping) - technique?

被刻印的时光 ゝ 提交于 2020-01-04 05:44:08
问题 I'm trying to find a better way of doing a Crystal Report (Someone Else's)... Add up non-overlapping time in groups. This is evidently an age-old problem... Is there a technique of getting Adjusted (start/end) times, per record, to remove common/over-lap time, within subgroups --using straight SQL (although I find I can do CTEs) Assume initial order-by for Start Time (and/or Group, SubGroup) and Start and End are separate fields. A kind-a graphic example: Group 1 SubGroup A Tkt 1 |--start&end

Reverse aggregation inside of common table expression

旧巷老猫 提交于 2020-01-03 21:07:00
问题 I would have expected that the following query returns all people with their respective children. WITH RECURSIVE nested_people (id, name, children) AS ( SELECT id, name, NULL::JSON AS children FROM people WHERE parent_id IS NULL UNION ALL SELECT people.id, people.name, ROW_TO_JSON(nested_people.*) AS children FROM people JOIN nested_people ON people.parent_id = nested_people.id ) SELECT * FROM nested_people; But actually it does the exact reverse. I can't think of a way to do correct nesting

Reverse aggregation inside of common table expression

自作多情 提交于 2020-01-03 21:02:14
问题 I would have expected that the following query returns all people with their respective children. WITH RECURSIVE nested_people (id, name, children) AS ( SELECT id, name, NULL::JSON AS children FROM people WHERE parent_id IS NULL UNION ALL SELECT people.id, people.name, ROW_TO_JSON(nested_people.*) AS children FROM people JOIN nested_people ON people.parent_id = nested_people.id ) SELECT * FROM nested_people; But actually it does the exact reverse. I can't think of a way to do correct nesting

get first record in group by result base on condition

 ̄綄美尐妖づ 提交于 2020-01-03 05:45:09
问题 this is my database structure create database testGroupfirst; go use testGroupfirst; go create table testTbl ( id int primary key identity,name nvarchar(50) ,year int ,degree int , place nvarchar(50) ) insert into testTbl values ('jack',2015,50,'giza') insert into testTbl values ('jack',2016,500,'cai') insert into testTbl values ('jack',2017,660,'alex') insert into testTbl values ('jack',2018,666,'giza') insert into testTbl values ('jack',2011,50,'alex') insert into testTbl values ('rami'

Recursive CTE in H2: No data returned

三世轮回 提交于 2020-01-03 05:34:13
问题 I am trying to convert a proprietary Oracle CONNECT BY query into a standard SQL query that will run on H2, and generate the same data in the same order . This is the Oracle query, which works: SELECT id, name, parent FROM myschema.mytable START WITH id = 1 CONNECT BY PRIOR id = parent This is what I've come up - however, it returns no rows in the ResultSet . WITH RECURSIVE T(id, name, parent, path) AS ( SELECT id, name, '' AS parent, id AS path FROM myschema.mytable WHERE id = 1 UNION ALL

Does SQLite support common table expressions?

我们两清 提交于 2020-01-02 04:41:28
问题 Does SQLite support common table expressions? I'd like to run query like that: with temp (ID, Path) as ( select ID, Path from Messages ) select * from temp 回答1: As of Sqlite version 3.8.3 SQLite supports common table expressions. Change log Instructions 回答2: Another solution is to integrate a "CTE to SQLite" translation layer in your application : "with w as (y) z" => "create temp view w as y;z" "with w(x) as (y) z" => "create temp table w(x);insert into w y;z" As an (ugly, desesperate, but

Is a SQL Server recursive CTE considered a loop?

风格不统一 提交于 2020-01-02 04:40:46
问题 I was under the impression that recursive CTEs were set based, but in a recent SO post someone mentioned that they are loops. Are recursive CTEs set based? Am I wrong to assume that a set based operation cannot be a loop? 回答1: If it is recursive it is still considered a loop. Although one statement is set based, calling it over and over can be considered a loop. This is an argument about the definition or wording based on the context being used. They are set based statements but the

Postgres CTE : type character varying(255)[] in non-recursive term but type character varying[] overall

折月煮酒 提交于 2020-01-01 08:43:17
问题 I am new to SO and postgres so please excuse my ignorance. Attempting to get the cluster for a graph in postgres using a solution similar to the one in this post Find cluster given node in PostgreSQL the only difference is my id is a UUID and I am using varchar(255) to store this id when i try to run the query I get the following error (but not sure how to cast): ERROR: recursive query "search_graph" column 1 has type character varying(255)[] in non-recursive term but type character varying[]

Best Way to Store/Access a Directed Graph

佐手、 提交于 2019-12-31 09:05:55
问题 I have around 3500 flood control facilities that I would like to represent as a network to determine flow paths (essentially a directed graph). I'm currently using SqlServer and a CTE to recursively examine all the nodes and their upstream components and this works as long as the upstream path doesn't fork alot. However, some queries take exponentially longer than others even when they are not much farther physically down the path (i.e. two or three segments "downstream") because of the added

Best Way to Store/Access a Directed Graph

試著忘記壹切 提交于 2019-12-31 09:04:09
问题 I have around 3500 flood control facilities that I would like to represent as a network to determine flow paths (essentially a directed graph). I'm currently using SqlServer and a CTE to recursively examine all the nodes and their upstream components and this works as long as the upstream path doesn't fork alot. However, some queries take exponentially longer than others even when they are not much farther physically down the path (i.e. two or three segments "downstream") because of the added