I had an issue today with CTEs running on SQL Server 2016, at the moment I worked around it using table variables, however I am not sure if the behavior is wrong or I am reading the documentation wrong.
When you run this query:
with cte(id) as
(
select NEWID() as id
)
select * from cte
union all
select * from cte
Run Code Online (Sandbox Code Playgroud)
I would expect two times the same guid, however there are 2 different ones. According to the …
我有一个这样的表:
Year, DividendYield
1950, .1
1951, .2
1952, .3
Run Code Online (Sandbox Code Playgroud)
我现在想计算总运行份额。换句话说,如果股息重新投资于新股,现在看起来会像这样:
1950 年 1 月 1 日购买的原始股份数量为 1
1950, .1, 1.1 -- yield of .1 reinvested in new shares results in .1 new shares, totaling 1.1
1951, .2, 1.32 -- (1.1 (Prior Year Total shares) * .2 (dividend yield) + 1.1 = 1.32)
1953, .3, 1.716 -- (1.32 * .3 + 1.32 = 1.716)
Run Code Online (Sandbox Code Playgroud)
我能想到的最接近的是:
declare @startingShares int = 1
; with cte_data as (
Select *,
@startingShares * DividendYield as …Run Code Online (Sandbox Code Playgroud) 我有这张层次结构表
select * from product_categories;
id | parent_id | item | rule
----+-----------+--------------+---------------
1 | | ecomm |
2 | 1 | grocceries |
3 | 1 | electronics |
5 | 3 | TV |
6 | 4 | touch_screen | Rules applied
7 | 4 | qwerty |
8 | 6 | iphone |
4 | 3 | mobile | mobile rules
Run Code Online (Sandbox Code Playgroud)
我想从 iPhone 进行遍历,一旦遇到“规则”不为 NULL 的行,我想选择该行并完成递归,我正在使用这个查询,
WITH RECURSIVE items AS (
SELECT id, item, parent_id, rule
FROM …Run Code Online (Sandbox Code Playgroud) 扩展以下问题(多选语句)我想知道我是否可以执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
所以这里的重点在于以下几行:
WHERE cdr.Location.someField = cte1.SomeField
Run Code Online (Sandbox Code Playgroud)
在cte2中哪里我引用cte1?
with子句如何在SQL Server中工作?它真的能给我一些性能提升,还是只是帮助制作更易读的脚本?
什么时候使用它?with在开始使用之前,您应该了解哪些条款?
这是我正在谈论的一个例子:
http://www.dotnetspider.com/resources/33984-Use-With-Clause-Sql-Server.aspx
我正在尝试找出我们在上一次测试过程中发现的错误.它涉及使用公用表表达式的查询.查询的主题是它模拟"第一个"聚合操作(获取此分组的第一行).
问题是,查询,似乎在某些情况下完全任意选择行-从同一组的多个行得到恢复,一些群体根本得到完全消除.但是,它总是选择正确的行数.
我已经创建了一个最小的例子来发布.有客户端和地址,以及定义它们之间关系的表.这是一个多少我期待在实际查询的简化版,但我相信它应该具有相同的特性,它是用来解释什么,我觉得是走错了一个很好的例子.
CREATE TABLE [Client] (ClientID int, Name varchar(20))
CREATE TABLE [Address] (AddressID int, Street varchar(20))
CREATE TABLE [ClientAddress] (ClientID int, AddressID int)
INSERT [Client] VALUES (1, 'Adam')
INSERT [Client] VALUES (2, 'Brian')
INSERT [Client] VALUES (3, 'Charles')
INSERT [Client] VALUES (4, 'Dean')
INSERT [Client] VALUES (5, 'Edward')
INSERT [Client] VALUES (6, 'Frank')
INSERT [Client] VALUES (7, 'Gene')
INSERT [Client] VALUES (8, 'Harry')
INSERT [Address] VALUES (1, 'Acorn Street')
INSERT [Address] VALUES (2, 'Birch Road') …Run Code Online (Sandbox Code Playgroud) 我对同一个cte查询的最后一个问题得到了如此迅速的回答,我以为我会从你的sql大师那里反弹下一个.如果我可以查看我的基本逻辑,然后显示我的代码和语法错误,任何帮助将不胜感激..
我有一个股票交易系统的三个表:符号表:顾名思义它是一个股票代码列表,每日定价/交易量表:再次,如上所述,每个记录也有一个日期字段和符号字段作为定价信息,最后是交易日期表:我们查询中所有交易日期的参考.
我想返回一个包含两个字段的记录集:符号和日期.该对代表所有交易日期和符号,这些交易日期和符号在定价量表中没有该符号的相应定价/ vol数据.合理?在我的查询中,我收到错误消息:"多部分标识符"Symb.Symbol"无法绑定." 这是我的cte查询:
WITH Symb AS
(
SELECT Symbol
FROM tblSymbolsMain
),
DatesNotNeeded AS
(
SELECT Date
FROM tblDailyPricingAndVol
WHERE (tblDailyPricingAndVol.Symbol = Symb.Symbol)
),
WideDateRange AS
(
SELECT TradingDate
FROM tblTradingDays
WHERE (TradingDate >= dbo.NextAvailableDataDownloadDateTime()) AND (TradingDate <= dbo.LatestAvailableDataDownloadDateTime())
),
DatesNeeded AS
(
SELECT TradingDate
FROM WideDateRange wdr
WHERE NOT EXISTS (SELECT * FROM DatesNotNeeded)
)
SELECT Symb.Symbol, DatesNeeded.TradingDate
FROM Symb CROSS JOIN DatesNeeded
Run Code Online (Sandbox Code Playgroud) 我有一个具有递归层次结构的表(即ID,ParentID)。对于这个层次结构中的任何项目,我希望能够带回上,下层次结构的所有列表以及每一行的级别。假设父母只能生一个孩子。
例如以下内容:
ID ParentID
--------------
1 NULL
2 1
3 2
4 NULL
5 4
6 5
Run Code Online (Sandbox Code Playgroud)
给定ID 1、2或3,我想返回:
ID ParentID Level
-----------------------
1 NULL 1
2 1 2
3 2 3
Run Code Online (Sandbox Code Playgroud)
我以前做过,但是我不记得怎么做。我知道解决方案涉及CTE,但我做对了!任何帮助表示赞赏。
sql-server recursive-query common-table-expression sql-server-2008
这是我的查询:
WITH desc_table(counter, hourly, current_weather_description, current_icons, time_stamp) AS (
Select count(*) AS counter, CASE WHEN strftime('%M', 'now') < '30'
THEN strftime('%H', 'now')
ELSE strftime('%H', time_stamp, '+1 hours') END as hourly,
current_weather_description,
current_icons,
time_stamp
From weather_events
GROUP BY strftime('%H', time_stamp, '+30 minutes'), current_weather_description
UNION ALL
Select count(*) as counter, hourly - 1, current_weather_description, current_icons, time_stamp
From weather_events
GROUP BY strftime('%H', time_stamp, '+30 minutes'), current_weather_description
Order By counter desc limit 1
),
avg_temp_table(avg_temp, hour_seg, time_stamp) AS (
select avg(current_temperatures) as avg_temp, CASE …Run Code Online (Sandbox Code Playgroud)