Quantcast
Channel: Active questions tagged cte - Database Administrators Stack Exchange
Browsing all 216 articles
Browse latest View live
↧

oracle working with CTE with condition

DEFINE fromtable1= true;WITH firstQuery AS ( SELECT columns FROM table),secondQuery AS ( SELECT columns FROM second_table) IF @fromtable1= true BEGIN SELECT * FROM firstQuery ENDELSE BEGIN SELECT *...

View Article


SQL Server query with common table expression suspended with pageiolatch_sh

I have a cte based query which runs for a very long time, and on the Activity Monitor for the query there are multiple rows showing the query is suspended in pageiolatch_sh. The strange thing is that...

View Article


What are use cases for Recursive CTEs?

I am looking for examples of why you would use a recursive CTE, but the dozens of examples I have found in my web searching basically reduce to two:Generating a sequenceIterating through an employee...

View Article

Repeated CTEs vs Recursive CTE

I'd like to ask which of the following two functions is better for retrieving a hierarchical list of child items from a table. Here are the two function definitions:Version 1 (Recursive CTE):CREATE OR...

View Article

Image may be NSFW.
Clik here to view.

How to adapt a recursive CTE over multiple tables?

I'm using PostgreSQL 17I am modelling a package index for the Haskell ecosystem, and a feature that is useful is to determine transitive dependencies. Haskell packages can be normalised as:Package...

View Article


Create a plan guide to cache (lazy spool) CTE result

I normally create plan guides by first constructing a query that uses the correct plan, and copying it across to the similar query that doesn't. However, that is sometimes tricky, especially if the...

View Article

CTE Error when installing sp_WhoIsActive

I have several identical (near as I can tell) SQL Servers where I've recently added sp_WhoIsActive (showing some folks how much I like this tool) but one of them will not let me create the stored...

View Article

PostgreSQL 16: WITH/CTE versus Non-Deferrable Constraints

I am using PostgreSQL 16 upwards.I am currently trying to create an example for a relationship of type G-|o-----|<-H in Crow's foot lingo.This means that each row in the table g for entity type G...

View Article


is it possible to make the CTE parallel in PostgreSQL 14

I have an analyse query in PostgreSQL 14 that use many CTEs, like this:witha0 as (select count(*) from ccc0) , a1 as (select count(*) from ccc1) , a2 as (select count(*) from ccc2) , a3 as (select...

View Article


Query Locks Table And Takes Long Time to Run

I have a query that can take a long time to run. I tried running the same query in a test server and it runs very quick.However, in production it seems to hang and causes blocking of other...

View Article

Getting sum for each day based on most recent timestamp entry for each entity

I have a table that contains an ID, timestamp, and integer. Each row is a "snapshot" of the entity at a given time:TableAIDtimestampnumA2024-02-045B2024-02-043C2024-02-078A2024-02-073My goal is to get...

View Article

Image may be NSFW.
Clik here to view.

List Top Daily Rentals Per Customer using a CTE

I've been trying to learn CTEs and having a bit or trouble getting the hang of them. I wrote a query against the Sakila Sample database that lists information about horror movie rentals for each day....

View Article

Query To Show YTD Total Summing Each Month Prior To Current

I need a query to give a YTD total that should sum each month before it. For example, January YTD would equal January Total, February YTD would equal January Total + February Total, March YTD would...

View Article


calculate or get price of parent items on bill of materials

i want to calculate or get the price of parent items on bill of materials queryhere is the fiddle-> https://www.db-fiddle.com/f/o3jLgZxKNLG14mna8QGdVN/6please note in the second row, betax has qty =...

View Article

How to have CTE query recurse parent query result one by one?

I have this query used on Postgres to return place administrative levels.WITH RECURSIVE hierarchy(name, pinyin, level) AS ( SELECT p.name, p.pinyin, p.level, p.upper_place_object_id_fk from place p...

View Article


Image may be NSFW.
Clik here to view.

Org chart CTE with exact index but plan shows expensive sort node

I've got a moderately large table (148 million rows) that's got pretty common org chart attributes to it. Names are changed, but it's the same idea. I was thinking of adding a hierarchyid column to the...

View Article
Browsing all 216 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>