我有一个表,其中包含每日库存数据,我想按周进行汇总。每日餐桌是:
CREATE TABLE [dbo].[TempDaily] (
[Symbol] CHAR (10) NOT NULL,
[CloseDate] DATE NOT NULL,
[DailyHi] FLOAT (53) NULL,
[DailyLow] FLOAT (53) NULL,
[AdjClose] FLOAT (53) NOT NULL,
[WeekEnd] DATE NULL
);
我想在每周表中插入一周的摘要:
CREATE TABLE [dbo].[Weekly] (
[Symbol] CHAR (10) NOT NULL,
[WeekEnd] DATE NOT NULL,
[WeeklyHi] FLOAT (53) NOT NULL,
[WeeklyLow] FLOAT (53) NOT NULL,
[AdjClose] FLOAT (53) NULL
);
[AdjClose]列设置为允许为空,因为我当前的解决方法是先插入其他4列,然后使用来自第三张表的每周AdjClose值更新每周表,这确实很慢。
将每周的前四列数据放入表中很简单:
strSQL = "INSERT INTO Weekly (Symbol, WeekEnd, WeeklyHi, WeeklyLow) " &
"SELECT Symbol, WeekEnd, MAX(DailyHi), MIN(DailyLow) " &
"FROM TempDaily " &
"GROUP BY Symbol, WeekEnd "
获取每周AdjClose并不是那么简单,但是我可以将数据插入临时表中,然后从中更新“每周”表:
strSQL = "INSERT INTO Test (WeekEnd, AdjClose) " &
"Select wdata.WeekEnd, MAX(wdata.AdjClose) " &
"FROM " &
"(Select CloseDate, WeekEnd, " &
"FIRST_VALUE(AdjClose) OVER (PARTITION BY WeekEnd ORDER BY CloseDate
DESC ROWS UNBOUNDED PRECEDING) As AdjClose " &
"FROM TempDaily) wdata " &
"GROUP BY wdata.WeekEnd "
我真的更喜欢用一条语句将所有5列的数据插入到Weekly表中,而不是依靠笨拙的解决方法,但是我一直无法弄清楚如何将2条语句联接起来。
“每日”表中的数据采用以下格式(为清楚起见添加了空格):
Symbol, CloseDate, DailyHi,DailyLow,AdjClose, WeekEnd
AAPL ,5/31/2019, 177.99, 175.00, 175.07, 6/1/2019
AAPL ,5/30/2019, 179.23, 176.67, 178.30, 6/1/2019
AAPL ,5/29/2019, 179.35, 176.00, 177.38, 6/1/2019
AAPL ,5/28/2019, 180.59, 177.91, 178.23, 6/1/2019
AAPL ,5/24/2019, 182.14, 178.62, 178.97, 5/25/2019
AAPL ,5/23/2019, 180.54, 177.81, 179.66, 5/25/2019
AAPL ,5/22/2019, 185.71, 182.55, 182.78, 5/25/2019
AAPL ,5/21/2019, 188.00, 184.70, 186.60, 5/25/2019
AAPL ,5/20/2019, 184.35, 180.28, 183.09, 5/25/2019
每周表格的结尾应为:
Symbol, WeekEnd, WeeklyHi, WeeklyLo, AdjClose
AAPL , 6/1/2019, 180.59, 175.00, 175.07
AAPL , 5/25/2019, 188.00, 177.81, 178.97
我正在将MS Access数据库转换为VB.NET,并认为这将是一个相当简单的端口。差异比我预期的要多得多。任何帮助表示赞赏。
下面介绍的解决方案是我尝试过的一种解决方案,但它会生成一个SQLException:''('附近的语法不正确。'wdata'附近的语法不正确。所以我仍在尝试解决这一问题。
strSQL = "INSERT INTO Weekly (Symbol, WeekEnd, WeeklyHi, WeeklyLow, AdjClose) " &
"Select A.Symbol, A.WeekEnd, A.WeeklyHi, A.WeeklyLow, ISNULL(B.AdjClose, 0) as AdjClose " &
"FROM " &
"(SELECT Symbol, WeekEnd, MAX(DailyHi) as WeeklyHi, MIN(DailyLow) as WeeklyLow " &
"FROM TempDaily " &
"GROUP BY Symbol, WeekEnd ) A " &
"LEFT JOIN " &
"(Select wdata.WeekEnd, MAX(wdata.AdjClose) as AdjClose" &
"FROM " &
"(Select CloseDate, WeekEnd, " &
"FIRST_VALUE(AdjClose) OVER (PARTITION BY WeekEnd ORDER BY CloseDate
DESC ROWS UNBOUNDED PRECEDING) As AdjClose " &
"FROM TempDaily) wdata " &
"GROUP BY wdata.WeekEnd) B ON A.WeekEnd = B.WeekEnd "
答案 0 :(得分:0)
我认为您可以做自己想做的事情,像这样(更新)加入他们:
strSQL = "INSERT INTO Weekly (Symbol, WeekEnd, WeeklyHi, WeeklyLow, AdjClose) " &
"Select A.Symbol, A.WeekEnd, A.WeeklyHi, A.WeeklyLow, ISNULL(B.AdjClose, 0) as AdjClose " &
"FROM " &
"(SELECT Symbol, WeekEnd, MAX(DailyHi) as WeeklyHi, MIN(DailyLow) as WeeklyLow " &
"FROM TempDaily " &
"GROUP BY Symbol, WeekEnd ) A " &
"LEFT JOIN " &
"(Select wdata.WeekEnd, MAX(wdata.AdjClose) as AdjClose " &
"FROM " &
"(Select CloseDate, WeekEnd, " &
"FIRST_VALUE(AdjClose) OVER (PARTITION BY WeekEnd ORDER BY CloseDate " &
"DESC ROWS UNBOUNDED PRECEDING) As AdjClose " &
"FROM TempDaily) wdata " &
"GROUP BY wdata.WeekEnd) B ON A.WeekEnd = B.WeekEnd "