common-table-expression

Creating View from Related Child Tables

自闭症网瘾萝莉.ら 提交于 2019-12-11 12:35:23
问题 I have the following general table structure (forgive the United States-centric carmakers in my contrived example): CREATE TABLE Car ( [Id] int PRIMARY KEY ) CREATE TABLE Ford ( [FordId] int PRIMARY KEY, --also a foreign key on Car [Model] nvarchar(max) ) CREATE TABLE Chevy ( [ChevyId] int PRIMARY KEY, --also a foreign key on Car [Model] nvarchar(max) ) I am wanting to create a view on top of these tables so that I can retrieve all Fords and Chevys and just have a generated column in the view

SQL Server recursive cte help wanted

ⅰ亾dé卋堺 提交于 2019-12-11 12:29:04
问题 I'm trying to generate a data set, and I think recursion is required, but I can't quite adapt the numerous examples I've found to my purposes. The end result I want is fairly simple to describe. Two tables are involved: the first with trading pricing for stocks with the relevant fields TradeDate , Symbol , and Clse (closing price), and a table with trading days listed. I'd like to partition the pricing data by symbol, ordered by date, but I'd like the partition/ row numbering to break if a

SQL: Count of rows since certain value first occurred: keep counting

…衆ロ難τιáo~ 提交于 2019-12-11 11:54:56
问题 This is a similar scenario to SQL: Count of rows since certain value first occurred In SQL Server, I'm trying to calculate the count of days since the same weather as today (let's assume today is 6th August 2018) was observed first in the past 5 days. Per town. Here's the data: +---------+---------+--------+--------+--------+ | Date | Toronto | Cairo | Zagreb | Ankara | +---------+---------+--------+--------+--------+ | 1.08.18 | Rain | Sun | Clouds | Sun | | 2.08.18 | Sun | Sun | Clouds |

Problem in string concatenation in sql server using FOR XML Path.

坚强是说给别人听的谎言 提交于 2019-12-11 11:45:26
问题 I have the below data UniqueID ID data 1 1 a 2 1 2 3 1 b 4 1 1 5 2 d 6 2 3 7 2 r The expected output being ID ConcatData 1 a,-,-,b,- 2 d,-,-,-,r What we have to do is that, the number of numeric charecters has to be replaced with those many dashes('-') and then we need to merge the data for the respective id's. I am using the below query so far declare @t table(UniqueID int identity(1,1), ID int, data varchar(10)) insert into @t select 1, 'a' union all select 1, '2' union all select 1, 'b'

Recursive sql server query 4

天大地大妈咪最大 提交于 2019-12-11 10:47:42
问题 I have 2 tables t1 and t2 with columns objectid, parentid and objectname. for each t1.objectid id I need to navigate t2 until t2.parentid is null and concatenate t2.objectname. The navigation criteria is that t1.objectid=t2.parentid .. traverse until t2.parent id is null. I tried cursor however I am not getting desired result. SCHEMA create table t1(objectid varchar(100), parentid varchar(100),objectname varchar(100)) go create table t2(objectid varchar(100), parentid varchar(100),objectname

MS SQL 2008 : Delete “duplicates” to select most recent results using multiple columns with no PK

折月煮酒 提交于 2019-12-11 10:23:21
问题 I have a table that looks like this and I need to remove duplicates to get the most recent results. These are not your standard duplicates as there is no primary key or another column where you are counting same instances of the same value. This table has a list of registered players, with the date they joined and left a team. If column EndDate is Null then it means the player is still playing for that team. PlayerID | RegID | RegDate | EndDate | Team | LastUpdate | 1 ---------| 1 ------| 10

Common table expression,WITH clause in PostgreSQL ;ERROR: relation “stkpos” does not exist

纵然是瞬间 提交于 2019-12-11 09:58:47
问题 for example the following is my query WITH stkpos as ( select * from mytbl ), updt as ( update stkpos set field=(select sum(fieldn) from stkpos) ) select * from stkpos ERROR: relation "stkpos" does not exist 回答1: Unlike MS-SQL and some other DBs, PostgreSQL's CTE terms are not treated like a view. They're more like an implicit temp table - they get materialized, the planner can't push filters down into them or pull filters up out of them, etc. One consequence of this is that you can't update

How to replace COALESCE with recursive CTE query?

天涯浪子 提交于 2019-12-11 09:53:25
问题 A follow-up question to Recursive CTE with three tables which helped me with CTE's in SQL Server. What has changed from the initial question? The table MANAGERS no longer includes rows for org.units that have no manager. The goal is to get the first non-null manager for a organizational unit. I've got it working using COALESCE and OUTER JOIN's, my question is if it is possible to use a recursive query instead? Example code follows below. DECLARE @ORG_PARENTS TABLE (ORG_ID INT, ORG_PARENT INT

Removing duplicates from a CTE based on a specific criteria

做~自己de王妃 提交于 2019-12-11 09:10:12
问题 I want to remove duplicate entries from my CTE based on a certain criteria that is if there are 2 records that have the same email address I want to have only record that has a refduserID with it. The other duplicate record which has 0 refdUserID shoud be removed. 回答1: The theory being, you could add a second CTE with an extra column. This extra column assigns a row number to every row based on certain criteria - in your case, partitioning by the email address (e.g. what column you want to

SQL Recursive CTE: Finding objects linked by property

廉价感情. 提交于 2019-12-11 08:56:20
问题 I'm just trying to understand CTE and recursion to solve an issue that I would previously have used a cursor for. create table ##ACC ( AccNo int, Property char ) Insert into ##ACC VALUES (1,'A'),(1,'B'),(2,'A'),(2,'C'),(3,'C'),(4,'D') What I'm trying to achieve is to get a list of all AccNo's, and all AccNo's they're related to via Property. So my expected results are PrimaryAccNo | LinkedAccNo 1 | 1 1 | 2 1 | 3 2 | 1 2 | 2 2 | 3 3 | 1 3 | 2 3 | 3 4 | 4 I've attempted the following code and