common-table-expression

SQL grouping interescting/overlapping rows

时间秒杀一切 提交于 2019-12-23 10:21:45
问题 I have the following table in Postgres that has overlapping data in the two columns a_sno and b_sno . create table data ( a_sno integer not null, b_sno integer not null, PRIMARY KEY (a_sno,b_sno) ); insert into data (a_sno,b_sno) values ( 4, 5 ) , ( 5, 4 ) , ( 5, 6 ) , ( 6, 5 ) , ( 6, 7 ) , ( 7, 6 ) , ( 9, 10) , ( 9, 13) , (10, 9 ) , (13, 9 ) , (10, 13) , (13, 10) , (10, 14) , (14, 10) , (13, 14) , (14, 13) , (11, 15) , (15, 11); As you can see from the first 6 rows data values 4,5,6 and 7 in

Indexing views with a CTE

徘徊边缘 提交于 2019-12-23 09:27:44
问题 So, I just found out that SQL Server 2008 doesn't let you index a view with a CTE in the definition, but it allows you to alter the query to add with schemabinding in the view definition. Is there a good reason for this? Does it make sense for some reason I am unaware of? I was under the impression that WITH SCHEMABINDING s main purpose was to allow you to index a view new and improved with more query action ;with x as ( select rx.pat_id ,rx.drug_class ,count(*) as counts from rx group by rx

Avoiding gaps in datetime intervals with CTE and start and end datetimes

五迷三道 提交于 2019-12-23 04:28:11
问题 For some reason I am seeing gaps in my time intervals using this query. I have gotten it to work when just using basic data. However, when joining my tables and specifying a WHERE clause, I see gaps in my time interval. I also need to incorporate the S.SessionEndTime in my intervals to find a count of records where there is overlap with a given 1 minute interval between the ResponseTime and SessionEndTime. Here is the query I am using. By using a derived table, I get a MAX per hour based on

CTE delete not committed until following statements complete

十年热恋 提交于 2019-12-22 16:57:48
问题 The problem I'm having is that deleted data still appears later in the same query. Naturally, in a completely separate query, the deleted data does not appear. This isn't my use-case, but I think this it's the simplest way to show the problem: CREATE TABLE company (id INT PRIMARY KEY, name TEXT); CREATE TABLE employee (id INT PRIMARY KEY, company_id INT REFERENCES company(id), name TEXT); INSERT INTO company VALUES (1, 'first company'); INSERT INTO company VALUES (2, 'second company'); INSERT

Dynamic query construction looping conditions from a XML?

寵の児 提交于 2019-12-22 12:13:40
问题 I need to construct a part of the conditions of my sql query from a xml. I have a XML like: <ROOT> <PARAMETROS> <USU_LOGIN>yleon</USU_LOGIN> <USU_NOMBREPRIMERO>Yerusha</USU_NOMBREPRIMERO> <USU_APELLIDOPRIMERO>Leon</USU_APELLIDOPRIMERO> <USU_EMAIL>yleon@email.com</USU_EMAIL> <USU_FECHACREACION>20130510</USU_FECHACREACION> <USU_CODICIONES1 TIPO="MC">AND USU_ID=4</USU_CODICIONES1> <USU_CODICIONES2 TIPO="MC">AND USU_ID=5</USU_CODICIONES2> <USU_CODICIONES3 TIPO="HG">AND USU_ID=9</USU_CODICIONES3>

multiple cte in single select statement where ctes can refer to each other

不打扰是莪最后的温柔 提交于 2019-12-22 11:38:31
问题 Expanding on following question (Multiple Select Statement) I would like to know if I can do following: WITH cte1 as ( SELECT * from cdr.Location ), cte2 as ( SELECT * from cdr.Location WHERE cdr.Location.someField = cte1.SomeField ) select * from cte1 union select * from cte2 So accent here is on following line: WHERE cdr.Location.someField = cte1.SomeField where within cte2 I'm referencing cte1 ? 回答1: Yes, you can reference previously declared CTEs in subsequent CTEs: WITH cte1 as ( SELECT

Walk parent/child tree with recursive CTE?

♀尐吖头ヾ 提交于 2019-12-22 09:59:37
问题 I am stuck with a cte, I want a query where in first parent is null. and child of pervious parent, will be the parent of next, and so on. WITH RESULT (PARENT,CHILD,TNAME,LEVEL) AS ( --anchor SELECT E.PARENT_GENERAL_KEY,E.M_GENERAL_KEY,E.NAME ,0 AS LEVEL FROM RPT_SYN_M_GENERAL AS E WHERE E.PARENT_GENERAL_KEY IS NULL UNION ALL --outer SELECT e.PARENT_GENERAL_KEY,E.M_GENERAL_KEY,e.NAME ,LEVEL +1 FROM RPT_SYN_M_GENERAL AS E INNER JOIN RESULT AS D ON E.PARENT_GENERAL_KEY=D.CHILD ) SELECT PARENT

Recursive CTE to find all ancestors OF ALL ITEMS

北城余情 提交于 2019-12-22 09:15:07
问题 I have a simple hierarchy and need to be able to generate a single table that matches EACH item in the table with ALL of its ancestors. (Caps to emphasize that this is not a duplicate question!) So here's a table: Select Item='A', Parent=null into Items union Select Item='B', Parent='A' union Select Item='C', Parent='A' union Select Item='D', Parent='B' union Select Item='E', Parent='B' union Select Item='F', Parent='C' union Select Item='G', Parent='C' union Select Item='H', Parent='D' Go ..

Change the execution plan of query in postgresql manually?

*爱你&永不变心* 提交于 2019-12-22 08:30:55
问题 Is it possible to change the order of the operations of an execution plan manually in postgresql? E.g. if I always want to have the ordering operation before a filtering (although it doesn't make sense in a normal use of postgresql), is it possible to enforce that manually by e.g. changing the internal costs of an operation? What about if I implement my own function? Is it possible to have such a function always being executed at the very end of the sql statement? 回答1: There are more ways -