common-table-expression

Hierarchical roll-up in SQL accounting system

▼魔方 西西 提交于 2020-05-17 04:50:05
问题 I'm trying to make annual reports (Balance Sheet and Profit & Loss) from general journal entries in an accounting system. The general journal table (simplified) includes: CREATE TABLE `sa_general_journal` ( `ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Date` timestamp NOT NULL DEFAULT current_timestamp(), `Item` varchar(1024) NOT NULL DEFAULT '', `Amount` decimal(9,2) NOT NULL DEFAULT 0.00, `Source` int(10) unsigned NOT NULL, `Destination` int(10) unsigned NOT NULL, PRIMARY KEY (`ID`), KEY

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

我只是一个虾纸丫 提交于 2020-05-07 05:24:07
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

你离开我真会死。 提交于 2020-05-07 05:23:35
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

雨燕双飞 提交于 2020-05-07 05:23:17
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

▼魔方 西西 提交于 2020-05-07 05:23:06
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Setting multiple variables from a CTE

自闭症网瘾萝莉.ら 提交于 2020-04-30 07:14:46
问题 SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON GO CREATE TRIGGER dbo.transferVehicle ON dbo.Vehicles AFTER INSERT AS BEGIN SET NOCOUNT ON; DECLARE @Level0Key INT, @Level1Key INT, @Level2Key INT, @Level3Key INT, @Level4Key INT, @Level5Key INT,@Level6Key INT,@Level7Key INT, @LocKey INT; SELECT @LocKey = [LocKey] FROM Inserted ; with tbParent as ( select * from Canepro.dbo.locations where LocKey= @LocKey union all select locations.* from Canepro.dbo.locations join tbParent on locations.LocKey =

Is branch pruning possible for recursive cte query

 ̄綄美尐妖づ 提交于 2020-04-30 06:38:26
问题 This is inspired by question Retrieve a list of lists in one SQL statement - I have come up with a solution, but I have doubts on its efficiency. To restate the problem: we have 2 Tables: Person and Parent Person contains basic data about each person Parent is a join table relating person with its parents each Person can have multiple parents we want to receive each person data with list of all their ancestors - each ancestor in its own row if there are no ancestors, we have only one row for

Postgres Recursive Query, CTE goes in infinite loop inside postgres function using sql?

你离开我真会死。 提交于 2020-04-18 05:46:27
问题 Trying to make a postgres function, inside of which I made a CTE, to recursively iterate through hierarchical data (Parent child relationship) MY Table structure In this I need to recursively iterate if text_id != new_text_id and stop (terminating condition) when text_id == new_text_id Table Schema for reference create table demoTextTable ( text_id serial primary key, text_details character varying, new_text_id integer ) insert into demoTextTable(text_details, new_text_id) values ('Comment 1'

Does SQLScript for SAP HANA support the use of INSERT with CTEs (Common Table Expressions)?

旧城冷巷雨未停 提交于 2020-03-25 21:23:05
问题 I know this isn't a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT INTO and WITH/CTE ...and I can't seem to find it out there on any SAP HANA help forums etc. so thought there may be an expert on here who can just give me a simple yes or no answer. The SQL statement I am using contains multiple CTEs, but when I try to insert it tells me there is a Syntax error around the word

How to use WITH clause in Laravel Query Builder

爱⌒轻易说出口 提交于 2020-02-25 04:18:43
问题 I have SQL query (see example). But I can't find a way how I can write it in Query Builder. Do you have any ideas how is it possible? WITH main AS ( SELECT id FROM table1 ) SELECT * FROM table2 WHERE table2.id IN (SELECT * FROM main) I want to get format like: $latestPosts = DB::table('posts') ->select('user_id', DB::raw('MAX(created_at) as last_post_created_at')) ->where('is_published', true) ->groupBy('user_id'); $users = DB::table('users') ->joinSub($latestPosts, 'latest_posts', function (