Stata odbc SQL多个CTE在一个查询中

时间:2015-02-23 21:15:09

标签: sql stata common-table-expression

我正在尝试在“odbc load,exec(”WITH ...“语句中)执行多个CTE表达式。我确认这两个CTE提取所需的信息。

然而,Stata似乎并不喜欢使用两个CTE。我尝试用分号分隔两者,比如SQL状态,但它返回“'语法不正确';'。”。

有什么建议吗?这是我要查看的代码:

. #delimit ;
delimiter now ; 
. odbc load, exec("
> WITH BIRegionSIC (Region, BranchID, Branch, SIC, CoreMarket, MarketSegment)
>         AS
>         (
>         SELECT
>                 [A].Region,
>                 [A].BranchID,
>                 [A].Branch,
>                 [B].SIC,
>                 [B].[Core Market] AS CoreMarket,
>                 [B].[Market Segment] AS MarketSegment
>         FROM
>                 [B]
>                 INNER JOIN [A]
>                 ON [B].Region = [A].Region
>         )
> ;WITH cteRNE (Year, Month, SoldTo, BranchID, cteSales)
>         AS
>         (
>         SELECT 
>                 Year([Invoice Date]) AS Year, 
>                 Month([Invoice Date]) AS Month,
>                 [Sold to (Number)] AS SoldTo,
>                 [Sales Office] AS BranchID,
>                 Sum([Invoiced Sales]) AS cteSales
>         FROM [A]
>                 INNER JOIN [Sales]
>                 ON [A].BranchID = [Sales].[Sales Office]
>         WHERE [A].Region = 'NE'
>         GROUP BY
>                 Year([Invoice Date]),
>                 Month([Invoice Date]),
>                 [Sales Office],
>                 [Sold to (Number)]
>         )
> SELECT
>                 BIRegionSIC.Region,
>                 BIRegionSIC.BranchID,
>                 BIRegionSIC.Branch,
>                 BIRegionSIC.CoreMarket,
>                 BIRegionSIC.MarketSegment,
>                 cteRNE.Year,
>                 cteRNE.Month,
>                 SUM([cteRNE].[cteSales]) AS Sales
>         FROM
>                 (
>                 cteRNE 
>                 INNER JOIN
>                         [C]
>                         ON cteRNE.SoldTo = [C].[Sold To]
>                 )
>                 INNER JOIN
>                         BIRegionSIC
>                         ON cteRNE.BranchID = BIRegionSIC.BranchID
>         GROUP BY
>                 BIRegionSIC.Region,
>                 BIRegionSIC.BranchID,
>                 BIRegionSIC.Branch,
>                 BIRegionSIC.CoreMarket,
>                 BIRegionSIC.MarketSegment,
>                 cteRNE.Year,
>                 cteRNE.Month
>         ORDER BY
>                 BIRegionSIC.Region ASC,
>                 BIRegionSIC.BranchID ASC,
>                 BIRegionSIC.Branch ASC,
>                 BIRegionSIC.CoreMarket ASC,
>                 BIRegionSIC.MarketSegment ASC,
>                 cteRNE.Year ASC,
>                 cteRNE.Month ASC
> ") 
> dsn(sourcefile) clear;
The ODBC driver reported the following diagnostics
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ';'.
SQLSTATE=42000
r(682);

end of do-file

r(682);

感谢您的任何建议!我分界,因此代码在do文件中是可读的,并且在没有将分隔符设置为分号的情况下运行会导致相同的错误。

此致

赖安

1 个答案:

答案 0 :(得分:0)

您应该通过以下方式声明多个CTE:

WITH cte AS (
)

, cte2 AS (
)

多次使用WITH - 子句。