没有为'l'的第4列指定列名

时间:2014-06-04 14:34:24

标签: sql

我使用以下代码收到此错误。我也得到了' c'

第3列的错误

以下是我使用此代码获得的错误的快照。 Errors I am getting

我已在下面更新了我的SQL。这是截至6/5/14

USE SYNLIVE 

SELECT 
 INLOC.Itemkey, l.Description, INLOC.Location, INLOC.Qtyonhand, 
   sum(l.POqtyRemn) [POqtyRemn], 
   SUM(c.Qtyord) AS [COqtyOrd], h.Statusflg
FROM 
   INLOC INLOC

INNER JOIN 
 (SELECT 
   POLIN.Itemkey, POLIN.Description, POLIN.Location, POLIN.Pono,
   SUM(POLIN.Qtyremn) AS [POqtyRemn] 
FROM 
   POLIN POLIN 

GROUP BY 
   POLIN.Itemkey, POLIN.Description, POLIN.Location, POLIN.Pono) l ON INLOC.Itemkey = l.Itemkey
 INNER JOIN 
   (SELECT 
   POHDR.Statusflg, POHDR.Pono
FROM 
   POHDR POHDR 
   WHERE POHDR.Statusflg = 'NEW' OR POHDR.Statusflg = 'OPEN'
GROUP BY 
    POHDR.Statusflg, POHDR.Pono) poh ON l.Pono = poh.Pono
JOIN 
   OELIN c ON INLOC.Itemkey = c.Itemkey


INNER JOIN 
  (SELECT 
    OEHDR.Statusflg, OEHDR.Ordno
FROM
    OEHDR 
    WHERE OEHDR.Statusflg = 'NEW' OR OEHDR.Statusflg = 'OPEN'
GROUP BY
    OEHDR.Statusflg, OEHDR.Ordno) h ON c.Ordno = h.Ordno


WHERE 

 ((INLOC.Location = 'SPL') AND (l.POqtyRemn > 0)) OR ((INLOC.Location = 'SPL') AND (c.Qtyord > 0))
GROUP BY 
 INLOC.Itemkey, l.Description, INLOC.Location, h.Statusflg, inloc.Qtyonhand
    /* Add other fields that you are pulling -- you must group by all fields (or have a calc on them .. i.e. Sum(field) */
ORDER BY INLOC.Itemkey

3 个答案:

答案 0 :(得分:2)

看起来像this issue with later versions of office

即别名使MS Query混淆。尝试更换

SUM(somefield) as SomeAlias

SUM(somefield) as [SomeAlias]

答案 1 :(得分:1)

更新:我看到你现在正在追求什么...哇...你必须按前3个字段分组,并在你的第一个选择语句中总结最后3个字段...我更新了sql以下

请注意对最终“group by”的更改。它必须包括所有字段......否则......自己运行单独的select语句并确保它们是有效的...除了“group by”之外似乎没问题。

至于你的错误...在它上面运行select语句创建L表。基于这个错误,我相信这给你带来了麻烦...还在“描述”这个词周围添加了[]方括号,因为它是一个关键词......不应该伤害你......但是有些东西可能是好的开始的地方。一旦你有这个声明工作...并通过...修复组,让我们知道会发生什么。

 SELECT 
           POLIN.Itemkey, POLIN.Description, POLIN.Location, 
           SUM(POLIN.Qtyremn) AS [POqtyRemn] 
        FROM 
           X.dbo.POLIN POLIN 
        GROUP BY 
           Itemkey, Description, Location

- 经过几次调整 - 我把它弄平了......所以你不再需要这个组...我还在各个子查询中添加了where子句(内连接选择语句)。

如果没有表模式,我不能保证我的所有语法都是正确的,但看看......也正如我所提到的......确保运行子查询并将它们与总数进行比较... for ItemKey(进行抽查)和总计......

SELECT 
 INLOC.Itemkey, l.Description, INLOC.Location, INLOC.Qtyonhand, 
   l.POqtyRemn, 
   c.Qtyord, h.Statusflg
FROM 
  (
      select Itemkey, sum(QtyOnHand) [QtyOnHand] 
      from  INLOC 
      where Location = 'SPL'
      group by ItemKey 
  ) INLOC

INNER JOIN 
    (
        SELECT   Itemkey, Description, Location, Pono,SUM(Qtyremn) AS [POqtyRemn] 
        FROM POLIN 
        GROUP BY Itemkey, Description, Location, Pono
        having SUM(Qtyremn) > 0 --This will only return an ItemKey if it has remaining Qty on the PO [POQtyRemn]
    ) l ON INLOC.Itemkey = l.Itemkey

INNER JOIN 
   (
        SELECT Statusflg, Pono
        FROM POHDR 
        WHERE Statusflg = 'NEW' OR Statusflg = 'OPEN'
        GROUP BY 
            Statusflg, Pono
    ) poh ON l.Pono = poh.Pono
JOIN 
       OELIN c ON INLOC.Itemkey = c.Itemkey
INNER JOIN 
    (
        SELECT Statusflg, Ordno
        FROM OEHDR 
        WHERE Statusflg = 'NEW' OR Statusflg = 'OPEN'
        GROUP BY Statusflg, Ordno
    ) h ON c.Ordno = h.Ordno
WHERE 
    (l.POqtyRemn > 0) OR (c.Qtyord > 0)
ORDER BY INLOC.Itemkey

答案 2 :(得分:0)

我必须将ALIAS - es添加到所有列,然后才能工作。

示例:

替换

INLOC.Location

INLOC.Location AS Location