common-table-expression

Recursive Query using CTE in SQL Server 2005

偶尔善良 提交于 2019-12-19 09:53:07
问题 OK, here's what I'm trying to do. I'm using a CTE query in MSSQL2005. The objective of the query is to recurse through Parent child relationships of product categories and return the number of products under each category (this includes any products contained in children categories) My current version only returns the product count for the category being displayed. It's not accounting for products that may be contained within any of its children. The database dump to reproduce the problem,

Controlling the sibling order under recursive CTE?

…衆ロ難τιáo~ 提交于 2019-12-19 04:00:27
问题 I have a CTE query which looks for main leafs and sub leafs. but I'm having trouble controling the leaf selection order between 2 siblings : Each row in the table is declared as : (childID INT ,parentID INT ,NAME NVARCHAR(30),location int) Where location is a priority to sort IFF they are siblings. And so I have this tree structure : those pairs has a location priority : For example : `a` ( location=1) should be before `f` (location=2) `b` ( location=1) should be before `e` (location=2) `d` (

SQL - CTE vs VIEW

不打扰是莪最后的温柔 提交于 2019-12-18 14:55:40
问题 My question here is what is the difference between CTE and View in SQL . I mean in which case I should use the CTE and which case the View . I know that both are some kind of virtual tables but I can't differentiate their use. I found a similar question here but it's about performance. Update 1: For example: I have a database filled with trades( tbl_trade ). I need to select from 3.5 millions records only the trades that was opened the current month until current time and then manipulate the

How do you identify record pattern sequences in records using TSQL?

半城伤御伤魂 提交于 2019-12-18 13:32:36
问题 This is a fairly new exercise for me but I need to find a way to identify pattern sequences within a table. So for example, lets say I have a simple table that resembles the following: Now what I would like to do is identify and group all the records which have the sequenced pattern of values 5, 9 and 6 presenting them in a query. How would you accomplish this task using T-SQL? The results should look like this: I've looked for some potential examples of how this might be accomplished but

joining multiple common table expressions

若如初见. 提交于 2019-12-18 11:48:51
问题 I have two Query, Query1: with cte as ( select dbo.Cable.*, row_number() over(partition by dbo.Cable.TagNo order by dbo.Cable.CableRevision desc) as rn from dbo.Cable where (dbo.Cable.CableRevision = @CoreRevision ) ) select * from cte where rn = 1 and also Query2 with cte as ( select dbo.Cable.TagNo,dbo.Core.*, row_number() over(partition by dbo.Core.CoreNo order by dbo.Core.CoreRevision desc) as rn from dbo.Core INNER JOIN dbo.Cable ON dbo.Cable.Id = dbo.Core.CableId where (dbo.Core

Use of With Clause in SQL Server

匆匆过客 提交于 2019-12-18 09:08:24
问题 How does with clause work in SQL Server? Does it really give me some performance boost or does it just help to make more readable scripts? When it is right to use it? What should you know about with clause before you start to use it? Here's an example of what I'm talking about: http://www.dotnetspider.com/resources/33984-Use-With-Clause-Sql-Server.aspx 回答1: Unless you use recursive abilities, a CTE is not better performance-wise than a simple inline view. It just saves you some typing. The

Combining INSERT statements in a data-modifying CTE with a CASE expression

旧街凉风 提交于 2019-12-18 09:05:49
问题 My question is some kind of extension to Erwin Brandstetter's excellent answer in this thread on the correct use of WITH . My old query looks like this: WITH x AS ( INSERT INTO d (dm_id) SELECT dm_id FROM dm, import i WHERE dm.dm_name = i.dm_name RETURNING d_id ), y AS ( INSERT INTO z (d_id) SELECT d_id FROM x RETURNING z_id ) INSERT INTO port (z_id) SELECT z_id FROM y; This works like a charm. But now, another table ( r ) has been added (same structure as table d ) and with it the

TSQL CTE: How to avoid circular traversal?

↘锁芯ラ 提交于 2019-12-18 04:42:28
问题 I have written a very simple CTE expression that retrieves a list of all groups of which a user is a member. The rules goes like this, a user can be in multiple groups, and groups can be nested so that a group can be a member of another group, and furthermore, groups can be mutual member of another, so Group A is a member of Group B and Group B is also a member of Group A. My CTE goes like this and obviously it yields infinite recursion: ;WITH GetMembershipInfo(entityId) AS( -- entity can be

TSQL CTE: How to avoid circular traversal?

一世执手 提交于 2019-12-18 04:42:04
问题 I have written a very simple CTE expression that retrieves a list of all groups of which a user is a member. The rules goes like this, a user can be in multiple groups, and groups can be nested so that a group can be a member of another group, and furthermore, groups can be mutual member of another, so Group A is a member of Group B and Group B is also a member of Group A. My CTE goes like this and obviously it yields infinite recursion: ;WITH GetMembershipInfo(entityId) AS( -- entity can be

ORACLE (11.2.0.1.0) - Recursive CTE with a date expression

爷,独闯天下 提交于 2019-12-17 20:54:05
问题 The right answer on the following question: That is a bug that was fixed in 11.2.0.3 or later if I recall correctly. (11.2.0.1 is no longer supported anyway. 11.2.0.4 is the only 11.2 release that is still supported) – @a_horse_with_no_name The bug number is 11840579 and it was fixed in 11.2.0.3 and 12.1.0.1 – @a_horse_with_no_name Question I have a table CREATE TABLE test( from_date date, to_date date ); INSERT INTO test(from_date,to_date) --VALUES('20171101','20171115'); VALUES(TO_DATE(