common-table-expression

Select Consecutive Numbers in SQL

自闭症网瘾萝莉.ら 提交于 2020-01-24 15:16:05
问题 This feels simple, but I can't find an answer anywhere. I'm trying to run a query by time of day for each hour. So I'm doing a Group By on the hour part, but not all hours have data, so there are some gaps. I'd like to display every hour, regardless of whether or not there's data. Here's a sample query: SELECT DATEPART(HOUR, DATEADD(HH,-5, CreationDate)) As Hour, COUNT(*) AS Count FROM Comments WHERE UserId = ##UserId## GROUP BY DATEPART(HOUR, DATEADD(HH,-5, CreationDate)) My thought was to

How can I traverse a tree bottom-up to calculate a (weighted) average of node values in PostgreSQL?

落花浮王杯 提交于 2020-01-23 01:07:07
问题 The typical example for e.g. summing a whole tree in PostgreSQL is using WITH RECURSIVE (Common Table Expressions). However, these examples typically go from top to bottom, flatten the tree and perform an aggregate function on the whole result set. I have not found a suitable example (on StackOverflow, Google, etc.) for the problem I am trying to solve: Consider an unbalanced tree where each node can have an associated value. Most of the values are attached to leaf nodes, but the others may

SQL Server 2008 CTE Recursion

你。 提交于 2020-01-20 19:38:47
问题 I am trying to perform what I believe is a difficult recursion using a CTE is SQL Server 2008. I can't seem to wrap my head around this one. In the below examples you can assume a fixed depth of 3...nothing will ever be lower than that. In real life, the depth is "deeper" but still fixed. In the example I tried to simplify it some. My input data is like the below. ID PARENT_ID NAME DEPTH ------------------------------------------ 1 NULL A 1 2 1 B 2 3 2 C 3 4 1 D 2 The output of my CTE should

Sum of child levels total in a Hierarchy

若如初见. 提交于 2020-01-20 02:48:05
问题 I need to have each level be the sum of all children (in the hierarchy) in addition to any values set against that value itself for the Budget and Revised Budget columns. I've included a simplified version of my table structure and some sample data to illustrate what is currently being produced and what I'd like to produce. Sample table: CREATE TABLE Item (ID INT, ParentItemID INT NULL, ItemNo nvarchar(10), ItemName nvarchar(max), Budget decimal(18, 4), RevisedBudget decimal(18, 4)); Sample

CTE error: “Types don't match between the anchor and the recursive part”

穿精又带淫゛_ 提交于 2020-01-18 15:49:05
问题 I am executing the following statement: ;WITH cte AS ( SELECT 1 as rn, 'name1' as nm UNION ALL SELECT rn + 1, nm = 'name' + CAST((rn + 1) as varchar(255)) FROM cte a WHERE rn < 10) SELECT * FROM cte ...which finishes with the error... Msg 240, Level 16, State 1, Line 2 Types don't match between the anchor and the recursive part in column "nm" of recursive query "cte". Where am I making the mistake? 回答1: Exactly what it says: 'name1' has a different data type to 'name' + CAST((rn+1) as varchar

Postgresql: using 'with clause' to iterate over a range of dates

一曲冷凌霜 提交于 2020-01-17 08:53:53
问题 I have a database table that contains a start visdate and an end visdate. If a date is within this range the asset is marked available. Assets belong to a user. My query takes in a date range (start and end date). I need to return data so that for a date range it will query the database and return a count of assets for each day in the date range that assets are available. I know there are a few examples, I was wondering if it's possible to just execute this as a query/common table expression

Update a table using CTE and NEWID()

旧城冷巷雨未停 提交于 2020-01-16 19:59:09
问题 I want try update a table using random values from the same table, but the original table don´t have a identity to follow or any other number column... WITH cteTable1 AS ( SELECT ROW_NUMBER() OVER (ORDER BY NEWID()) AS n, ENDE_NO_Address FROM TableX ) UPDATE TableX SET ENDE_NO_Address = ( SELECT ENDE_NO_Address FROM cteTable1 WHERE cteTable1.n = This is the problem... tnks for the help 回答1: declare @T table(Col1 varchar(10)) insert into @T values (1),(2),(3),(4),(5) ;with cte as ( select *,

Return data from subselect used in INSERT in a Common Table Expression

扶醉桌前 提交于 2020-01-15 12:44:52
问题 I am trying to move bytea data from one table to another, updating references in one query. Therefore I would like to return data from the query used for the insert that is not used for the insert. INSERT INTO file_data (data) select image from task_log where image is not null RETURNING id as file_data_id, task_log.id as task_log_id But I get an error for that query: [42P01] ERROR: missing FROM-clause entry for table "task_log" I want to do something like: WITH inserted AS ( INSERT INTO file

Row_Number() CTE Performance when using ORDER BY CASE

六月ゝ 毕业季﹏ 提交于 2020-01-15 04:53:25
问题 I have a table I'd like to do paging and ordering on and was able to get a query similar to the following to do the work (the real query is much more involved with joins and such). WITH NumberedPosts (PostID, RowNum) AS ( SELECT PostID, ROW_NUMBER() OVER (ORDER BY CASE WHEN @sortCol = 'User' THEN User END DESC, CASE WHEN @sortCol = 'Date' THEN Date END DESC, CASE WHEN @sortCol = 'Email' THEN Email END DESC) as RowNum FROM Post ) INSERT INTO #temp(PostID, User, Date, Email) SELECT PostID, User

Row_Number() CTE Performance when using ORDER BY CASE

空扰寡人 提交于 2020-01-15 04:53:13
问题 I have a table I'd like to do paging and ordering on and was able to get a query similar to the following to do the work (the real query is much more involved with joins and such). WITH NumberedPosts (PostID, RowNum) AS ( SELECT PostID, ROW_NUMBER() OVER (ORDER BY CASE WHEN @sortCol = 'User' THEN User END DESC, CASE WHEN @sortCol = 'Date' THEN Date END DESC, CASE WHEN @sortCol = 'Email' THEN Email END DESC) as RowNum FROM Post ) INSERT INTO #temp(PostID, User, Date, Email) SELECT PostID, User