common-table-expression

SQL Partition data within and outside of a Common Table Expression

百般思念 提交于 2019-12-24 12:38:30
问题 I am using SQL Server 2012 I have the following sample result set. The columns with the Tot Prefix are derived from the columns to the left. What I am trying to do is partition data for a given date, and a given AssetStyle. Based on this criteria I want to sum profit and sum ACB. I then want to divide the results of these two sums for a total return number. The sample data is below. ValuationDate CustodianaccountNum AssetStyle AssetClass Profit ACB NetReturn TotProfit TotACB TotReturn 3/31

SQL Long running query / maxing out server resource e.g. RAM/CPU

ε祈祈猫儿з 提交于 2019-12-24 10:22:23
问题 I originally posted the following thread SQL Query - long running / taking up CPU resource My issue was the SARGABILITY of my query, having addressed that (see the previous thread but in short I was using a lot of ISNULL functions which were bypassing the index scans) I am now having further issues. My SQL server settings are as below: cost threshold for parallelism 5 max degree of parallelism 0 My query still takes 2:13 to run and causes CPU / Memory spikes, I have a largely capable server e

Remove duplicates from a table and re-link referencing rows to the new master

依然范特西╮ 提交于 2019-12-24 10:17:32
问题 I have a table transcription which contains passages of transcribed text and their citations with columns: text, transcription_id(PK), t_notes, citation and the second table town_transcription being the relationship table that links places (from another table) referenced in the text to that transcription record. This table has the columns: town_id(FK), transcription_id(FK), confidence_interval Many of these passages of text reference multiple towns, but stupidly I just duplicated records and

How many times are the results of this common table expression evaluated?

有些话、适合烂在心里 提交于 2019-12-24 05:40:46
问题 I am trying to work out a bug we've found during our last iteration of testing. It involves a query which uses a common table expression. The main theme of the query is that it simulates a 'first' aggregate operation (get the first row for this grouping). The problem is that the query seems to choose rows completely arbitrarily in some circumstances - multiple rows from the same group get returned, some groups simply get eliminated altogether. However, it always picks the correct number of

Recursive CTE with three tables

倾然丶 夕夏残阳落幕 提交于 2019-12-24 05:39:21
问题 I'm using SQL Server 2008 R2 SP1. I would like to recursively find the first non-null manager for a certain organizational unit by "walking up the tree". I have one table containing organizational units "ORG", one table containing parents for each org. unit in "ORG", lets call that table "ORG_PARENTS" and one table containing managers for each organizational unit, lets call that table "ORG_MANAGERS". ORG has a column ORG_ID: ORG_ID 1 2 3 ORG_PARENTS has two columns. ORG_ID, ORG_PARENT 1, NULL

Recursive CTE with three tables

天涯浪子 提交于 2019-12-24 05:38:18
问题 I'm using SQL Server 2008 R2 SP1. I would like to recursively find the first non-null manager for a certain organizational unit by "walking up the tree". I have one table containing organizational units "ORG", one table containing parents for each org. unit in "ORG", lets call that table "ORG_PARENTS" and one table containing managers for each organizational unit, lets call that table "ORG_MANAGERS". ORG has a column ORG_ID: ORG_ID 1 2 3 ORG_PARENTS has two columns. ORG_ID, ORG_PARENT 1, NULL

Does adding extraneous tables in a WITH clauses slow down a query in PostgreSQL?

天涯浪子 提交于 2019-12-24 04:18:09
问题 I have a (possibly) basic question about how Postgres executes queries containing WITH clauses. I'm wondering whether including extraneous tables in a WITH clause actually slows down the query. That is, if the "temporary" table created in a WITH clause is never called outside of a WITH clause, is that "temporary" table actually created? In the first example, I am joining two "temporary" tables created using WITH clauses: --Example 1 WITH temp1 as ( SELECT * from table_1 ), temp2 as ( select *

How to stop recursion in a CTE?

别说谁变了你拦得住时间么 提交于 2019-12-24 02:02:22
问题 I have a database table which looks like this: ID | PredecessorID | Data -------------------------------------|--------------------------------------|----------------- 43b1e103-d8c6-40f9-b031-e5d9ef18a739 | null | ... 55f6951b-5ed3-46c8-9ad5-64e496cb521a | 43b1e103-d8c6-40f9-b031-e5d9ef18a739 | ... 3eaa0889-31a6-449d-a499-e4beb9e4cad1 | 55f6951b-5ed3-46c8-9ad5-64e496cb521a | ... I know that I can use a (recursive) common table expression (CTE) to get a sorted list of my data: WITH cte (ID,

SQL - calculate forecast average

给你一囗甜甜゛ 提交于 2019-12-23 23:26:23
问题 I'm trying to calculate a forecast of sales based on the 3 previous months which either can be actuals or forecast. company_id Year Month Actuals Forecast 123456 2014 1 10 123456 2014 2 15 123456 2014 3 17 123456 2014 4 14.00 123456 2014 5 15.33 123456 2014 6 15.44 123456 2014 7 14.93 Month 4 = (10+15+17)/3 Month 5 = (15+17+14)/3 Month 6 = (17+14+15.33)/3 Month 7 = (14+15.33+15.44)/3 Let's say I want to calculate the forecast for the next 18 months for each company. I'm looking at the data

What are the advantages/disadvantages of using a CTE?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 20:38:32
问题 I'm looking at improving the performance of some SQL, currently CTEs are being used and referenced multiple times in the script. Would I get improvements using a table variable instead? (Can't use a temporary table as the code is within functions). 回答1: You'll really have to performance test - There is no Yes/No answer. As per Andy Living's post above links to, a CTE is just shorthand for a query or subquery. If you are calling it twice or more in the same function, you might get better