如何在sql中使用声明ERROR结合两个cte语句:关键字'DECLARE'附近的语法不正确

时间:2014-05-26 05:06:13

标签: sql sql-server sql-server-2008

我在CT中使用CTE for union两列。 如果我正在执行这两个查询,它工作正常。 但是我希望将这两者结合起来以获得更简单的O / P..

我收到编译时错误:

关键字'DECLARE'附近的语法不正确。

任何人都可以告诉我锄头与cte实现联盟

DECLARE @Inward DATETIME
SET @Inward = DATEADD(mm, -6, CURRENT_TIMESTAMP);
WITH cte AS 
(
SELECT 0 AS TheMonth 
UNION ALL 
SELECT TheMonth + 1 
FROM cte
WHERE TheMonth < 5
) 
SELECT  
cte.TheMonth,
isnull(sum(qty),0) as inward 
FROM 
cte 
LEFT OUTER JOIN RS_GIN_Master as g 
ON accept_date >= DATEADD(MM, cte.TheMonth, @Inward) AND accept_date < DATEADD(MM, cte.TheMonth + 1, @Inward)

UNION all 

DECLARE @Outward DATETIME
SET @Outward = DATEADD(mm, -6, CURRENT_TIMESTAMP);
WITH cte AS
(
SELECT 0 AS TheMonthO 
UNION ALL
SELECT TheMonthO + 1 
FROM cte 
 WHERE TheMonthO < 5
) 

SELECT  isnull(sum(quantity),0) as outward 
FROM 
cte 
LEFT OUTER JOIN 
RS_Sell_Order_Master as s 
ON del_date >= DATEADD(MM, cte.TheMonthO, @Outward) AND del_date < DATEADD(MM, cte.TheMonthO + 1, @Outward) and isDelivered = 1
left outer join RS_Sell_Order_Mapping as sm on sm.sell_order_no = s.sell_order_no     

2 个答案:

答案 0 :(得分:2)

哈!我今天看了,因为我有同样的问题。您必须使用&#34; WITH&#34;声明第一个CTE。关键字和您需要的每个CTE,只需用逗号分隔即可。

来自sgeedes的编辑查询

    DECLARE @Inward DATETIME
DECLARE @Outward DATETIME

SET @Inward = DATEADD(mm, -6, CURRENT_TIMESTAMP);
SET @Outward = DATEADD(mm, -6, CURRENT_TIMESTAMP);

WITH cte AS 
(
   SELECT 0 AS TheMonth 
   UNION ALL 
   SELECT TheMonth + 1 
   FROM cte
   WHERE TheMonth < 5
) 
SELECT TheMonth,sum(Inward) as InWard, sum(OutWard) as OutWard
FROM
(
SELECT  
   cte.TheMonth,
   isnull(sum(qty),0) as inward,
   0 as outward
FROM 
   cte 
      LEFT OUTER JOIN RS_GIN_Master as g 
          ON accept_date >= DATEADD(MM, cte.TheMonth, @Inward) 
         AND accept_date < DATEADD(MM, cte.TheMonth + 1, @Inward)
GROUP BY cte.TheMonth
UNION all 
SELECT  
   cte.TheMonth,
   0 as inward,
   isnull(sum(quantity),0) as outward 
FROM 
   cte 
      LEFT OUTER JOIN RS_Sell_Order_Master as s 
          ON del_date >= DATEADD(MM, cte.TheMonthO, @Outward) 
         AND del_date < DATEADD(MM, cte.TheMonthO + 1, @Outward) and isDelivered = 1
      left outer join RS_Sell_Order_Mapping as sm on 
          sm.sell_order_no = s.sell_order_no     
GROUP BY cte.TheMonth
)Z

答案 1 :(得分:2)

从哪里开始。

  • 您不能在declare语句中使用union语句。
  • Union语句必须返回相同数量的列。
  • Common table expressions只应在您的陈述开头定义。
  • 您的Sum汇总每月需要group by返回。

也许你正在寻找这样的东西:

DECLARE @Inward DATETIME
DECLARE @Outward DATETIME

SET @Inward = DATEADD(mm, -6, CURRENT_TIMESTAMP);
SET @Outward = DATEADD(mm, -6, CURRENT_TIMESTAMP);

WITH cte AS 
(
   SELECT 0 AS TheMonth 
   UNION ALL 
   SELECT TheMonth + 1 
   FROM cte
   WHERE TheMonth < 5
) 
SELECT  
   cte.TheMonth,
   isnull(sum(qty),0) as inward,
   null as outward
FROM 
   cte 
      LEFT OUTER JOIN RS_GIN_Master as g 
          ON accept_date >= DATEADD(MM, cte.TheMonth, @Inward) 
         AND accept_date < DATEADD(MM, cte.TheMonth + 1, @Inward)
GROUP BY cte.TheMonth
UNION all 
SELECT  
   cte.TheMonth,
   null as inward,
   isnull(sum(quantity),0) as outward 
FROM 
   cte 
      LEFT OUTER JOIN RS_Sell_Order_Master as s 
          ON del_date >= DATEADD(MM, cte.TheMonthO, @Outward) 
         AND del_date < DATEADD(MM, cte.TheMonthO + 1, @Outward) and isDelivered = 1
      left outer join RS_Sell_Order_Mapping as sm on 
          sm.sell_order_no = s.sell_order_no     
GROUP BY cte.TheMonth