common-table-expression

Bucket Filling SQL query CTE

帅比萌擦擦* 提交于 2019-12-25 08:09:45
问题 I want to achieve the below output from the given input tables. Input Table (Bucket to be filled) ID | FullCapacity | CurrentAmount ---+--------------+-------------- B1 | 100 | 0 B2 | 50 | 0 B3 | 70 | 0 Input Table (Filler Table) ID | Filler ---+------- F1 | 90 F2 | 70 F3 | 40 F4 | 20 Output table should have below showing filling process. ID | FullCapacity | CurrentAmount ---+--------------+-------------- B1 | 100 | 90 B2 | 50 | 0 B3 | 70 | 0 ---+--------------+-------------- B1 | 100 | 100

Returning all children with a recursive select

↘锁芯ラ 提交于 2019-12-25 07:46:52
问题 Good day everyone! I've got a graph. First, I know how to build simple recursive selections. I read some info on msdn . In this image you can see that (for example) the top node of the graph, which is numbered 0, influences node number 1 (etc (2->4), (3->4), (4->5), (5->6), (1->5)) TASK: for every node show nodes which it influences. For example, number 1 influences 5 and 6. The result SQL must return something like this: who_acts| on_whom_influence 0 | 1 0 | 5 0 | 6 1 | 5 1 | 6 2 | 4 2 | 5 2

postgres returning id, insert or select if id is present or not

偶尔善良 提交于 2019-12-25 07:27:03
问题 WITH x AS ( INSERT INTO users(name, age) SELECT 'john', 30 WHERE NOT EXISTS ( SELECT id FROM users WHERE name = 'john' ) u RETURNING (CASE WHEN u.id > 0 THEN u.id ELSE id END) ) SELECT * FROM x How can I always return an id and if the id is not present, then insert it. Also, I need this to be in a CTE. 来源: https://stackoverflow.com/questions/24050779/postgres-returning-id-insert-or-select-if-id-is-present-or-not

sqlite CTE with UPDATE

此生再无相见时 提交于 2019-12-25 04:48:13
问题 I hope this is not a duplicate, I red some posts, but could not figure out how to fix this. I have a table like this CREATE TABLE yo (ad INTEGER PRIMARY KEY, pa INTEGER, pd INTEGER); INSERT INTO yo VALUES (1,1,1),(2,1,3),(3,1,4),(4,3,5),(5,4,2),(6,3,8),(7,1,9),(8,6,7),(9,3,6); .header on .mode column yo select * from yo; ad pa pd ---------- ---------- ---------- 1 1 1 2 1 3 3 1 4 4 3 5 5 4 2 6 3 8 7 1 9 8 6 7 9 3 6 I can create a temp table using CTE to obtain the depth level of col 'pd' like

Get records containing all the child records in sql server

烂漫一生 提交于 2019-12-25 03:39:41
问题 I have heirarchical data in sql server. Follwing are my data heirarcy tree. Id Name ParentID 1124 ABC 2 1125 BCD 1124 1126 EFG 1124 1127 HIJ 1126 1128 KLM 1126 1129 OPQ 1124 1130 RST 1124 1131 UVW 1130 1132 XYZ 1131 1133 ZYA1 1124 Now i want following result from above table. In that i want path from root (parent) with all child till last child. Id Name ParentID PATH 1124 ABC 2 ABC 1125 BCD 1124 ABC/BCD 1126 EFG 1124 ABC/EFG 1127 HIJ 1126 ABC/EFG/HIJ 1128 KLM 1126 ABC/EFG/KLM 1129 OPQ 1124

How to determine in sql if a user can read/write/execute a file a la unix permissions system?

穿精又带淫゛_ 提交于 2019-12-25 03:18:09
问题 I am trying to write an application where there are files and users. The permissions for the files should be as in unix: read/write/execute. Each file belongs to a owner and a group, and for each file there are "other" users (based on who is owner, and who belongs to a group). The schema looks like this: create table s_file( file_id INTEGER PRIMARY KEY, parent_id INTEGER default NULL, name, content BLOB DEFAULT NULL, owner_id INTEGER NOT NULL, group_id INTEGER NOT NULL, read_owner BOOLEAN not

Converting 'WITH CTE' MSSQL to MySQL

筅森魡賤 提交于 2019-12-25 03:12:43
问题 I realize that this doesn't directly translate from MSSQL to MySQL but I'm not not sure how to make it work. Any help that you have is appreciated. ;WITH cte As ( SELECT post_id, status, dealer, distributor, SUM( 3959 * acos( cos( radians(%f) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(%f) ) + sin( radians(%f) ) * sin( radians( lat ) ) ) ) AS DISTANCE FROM wp_geodatastore GROUP BY post_id, status, dealer, distributor ) SELECT post_id, status, dealer, distributor, DISTANCE FROM

Error when inserting CTE table values into physical table

為{幸葍}努か 提交于 2019-12-24 22:25:19
问题 I have a complex query that creates a master CTE_Table form other CTE_Tables. I want to insert the results of the master CTE_Table into a physical table. I'm using Teradata version 15.10.04.03 SELECT Failed. [3707] Syntax error, expected something like a 'SELECT' keyword or '(' or a 'TRANSACTIONTIME' keyword or a 'VALIDTIME' keyword between ')' and the 'INSERT' keyword. DROP TABLE dbname.physicalTablename ; CREATE MULTISET TABLE dbname.physicalTablename , NO FALLBACK , NO BEFORE JOURNAL, NO

Using Common Table Expression (CTE) in JPA Criteria API

允我心安 提交于 2019-12-24 18:36:51
问题 WITH cte AS ( select A.A_ID, B.Lib, A.Lib, C.Lib, (SELECT count(*) FROM X WHERE A.A_ID = X.A_ID) AS countX, (SELECT count(*) FROM Y WHERE A.A_ID = Y.A_ID) AS countY, (SELECT count(*) FROM Z WHERE A.A_ID = Z.A_ID) AS countZ from A left outer join C on A.C_ID=C.C_ID left outer join B on A.B_ID=B.B_ID ) select * from cte where countX = 2 AND countY = 3 Order BY countZ DESC; I know how to create the sql query inside the CTE but I don't have any idea how to create the CTE in JPA Criteria API (

One INSERT with multiple SELECT

喜你入骨 提交于 2019-12-24 14:02:42
问题 I've already read this, this and this, but I cant make this SQL work: INSERT INTO main_phrase (description) VALUES ('Mot commun féminin pluriel animaux'); /* ERROR: */ WITH t1 AS ( SELECT id FROM main_phrase WHERE description='Mot commun féminin pluriel animaux' ), t2 AS ( SELECT id FROM main_groupecategories WHERE description='Mot commun féminin pluriel animaux' ) INSERT INTO main_phrasegroupecategories (phrase_id, groupe_categories_id) VALUES (t1.id, t2.id); I get: ERROR: missing entry for