common-table-expression

SQL CTE scope in query

▼魔方 西西 提交于 2021-01-29 12:34:51
问题 I have tables ChatMessages , ChatGroups and ChatGroupMemberships . A user can be in 0..N groups and in group can be 1..N chat messages. That first message is created once group is initied and it is sort of "alive" ping. I'm optimizing the way I'm reconstructing the list of a user's conversations. That list is pretty standard and you may know it from any social site: | Chat with User X -> [last message in that chat group] | Group chat named ABC -> [same] What I did so far was that I simply

Graph Database or Relational Database Common Table Extensions: Comparing acyclic graph query performance

♀尐吖头ヾ 提交于 2021-01-29 10:09:07
问题 Are graph databases more performant than relational databases for highly connected acyclic graph data? I need to significantly speed up my query results and hope that graph databases will be the answer. I had seen significant improvement in my relational database queries when I used Common Table Extensions bringing a recursive search of my sample data from 16 hours to 30 minutes. Still, 30 minutes is way too long for a web application and trying to work around that kind of response gets

Identifier could not be bound in multiple common table expressions

…衆ロ難τιáo~ 提交于 2021-01-29 09:44:42
问题 I'm using the SQLBook database from the Data Analysis Using SQL and Excel book to display the average days to ship for states that have higher than average overall shipping days. I'm using 2 common table expressions: WITH orderDetails (days, state) AS( SELECT DATEDIFF(day, o.OrderDate, ol.ShipDate), o.State FROM SQLBook.dbo.Orders o JOIN [SQLBook].dbo.OrderLines ol ON ol.OrderId = o.OrderId ) , /* This finds the overall average shipping days */ AvgShipping (avgShip) AS( SELECT AVG(DATEDIFF

Sql Server query for tree path of one item

南笙酒味 提交于 2021-01-28 00:41:21
问题 I need a SQL query giving me the complete tree path of one item. The tables Looks like this and there is a 1:n relation between MyItem_MyItemId and MyItemMapping_MyItemId. Table MyItem: MyItem_MyItemId | MyItem_Title 1 | Desktop 2 | Workspace 3 | Folder1 4 | Folder2 5 | Folder3 6 | Folder4 ... Table MyItemMapping: MyItemMapping_MyItemId | MyItemMapping_MyItemParentId 4 | 3 3 | 2 2 | 1 1 | NULL 5 | 2 6 | 2 ... Now I Need a query the brings the path for the Folder2 like "Desktop\Workspace

Ordering parent rows by date descending with child rows ordered independently beneath each

偶尔善良 提交于 2021-01-27 12:44:58
问题 This is a contrived version of my table schema to illustrate my problem: QuoteID, Details, DateCreated, ModelQuoteID Where QuoteID is the primary key and ModelQuoteID is a nullable foreign key back onto this table to represent a quote which has been modelled off another quote (and may have subsequently had its Details column etc changed). I need to return a list of quotes ordered by DateCreated descending with the exception of modelled quotes, which should sit beneath their parent quote,

Finding the spanning forest (WITH RECURSIVE, PostgreSQL 9.5)

半腔热情 提交于 2021-01-27 10:46:51
问题 I have a table of identities (i.e. aliases) for an arbitrary number of people. Each row has a previous name and a new name. In production, there are about 1 M rows. For example: id, old, new --- 1, 'Albert', 'Bob' 2, 'Bob', 'Charles' 3, 'Mary', 'Nancy' 4, 'Charles', 'Albert' 5, 'Lydia', 'Nancy' 6, 'Zoe', 'Zoe' What I want is to generate the list of users and reference all of their respective identities. This is analogous to finding all of the nodes in each graph of connected identities, or

Finding the spanning forest (WITH RECURSIVE, PostgreSQL 9.5)

大兔子大兔子 提交于 2021-01-27 10:43:23
问题 I have a table of identities (i.e. aliases) for an arbitrary number of people. Each row has a previous name and a new name. In production, there are about 1 M rows. For example: id, old, new --- 1, 'Albert', 'Bob' 2, 'Bob', 'Charles' 3, 'Mary', 'Nancy' 4, 'Charles', 'Albert' 5, 'Lydia', 'Nancy' 6, 'Zoe', 'Zoe' What I want is to generate the list of users and reference all of their respective identities. This is analogous to finding all of the nodes in each graph of connected identities, or

Multiple INSERTS into one table and many to many table

半世苍凉 提交于 2021-01-22 10:34:06
问题 I'm trying to develop a Q&A website in PHP using a PostgreSQL database. I have an action to create a page which has a title, body, category and tags. I managed to insert all those fields however I'm having some issues inserting multiple tag values. I used this function to get the comma separated values into an array and now I want something that inserts each array element into the database (avoiding repetitions) on table tags and after that insert on my many to many relationship table

Join on a CTE in SQLAlchemy

╄→尐↘猪︶ㄣ 提交于 2020-12-15 06:43:30
问题 I'm trying to formulate a SQLAlchemy query that uses a CTE to build a table-like structure of an input list of tuples, and JOIN it with one of my tables (backend DB is Postgres). Conceptually, it would look like: WITH to_compare AS ( SELECT * FROM ( VALUES (1, 'flimflam'), (2, 'fimblefamble'), (3, 'pigglywiggly'), (4, 'beepboop') -- repeat for a couple dozen or hundred rows ) AS t (field1, field2) ) SELECT b.field1, b.field2, b.field3 FROM my_model b JOIN to_compare c ON (c.field1 = b.field1)

Join on a CTE in SQLAlchemy

懵懂的女人 提交于 2020-12-15 06:41:46
问题 I'm trying to formulate a SQLAlchemy query that uses a CTE to build a table-like structure of an input list of tuples, and JOIN it with one of my tables (backend DB is Postgres). Conceptually, it would look like: WITH to_compare AS ( SELECT * FROM ( VALUES (1, 'flimflam'), (2, 'fimblefamble'), (3, 'pigglywiggly'), (4, 'beepboop') -- repeat for a couple dozen or hundred rows ) AS t (field1, field2) ) SELECT b.field1, b.field2, b.field3 FROM my_model b JOIN to_compare c ON (c.field1 = b.field1)