【问题标题】:Incorrect syntax near the keyword 'GROUP' - MA关键字“GROUP”附近的语法不正确 - MA
【发布时间】:2016-10-10 18:28:18
【问题描述】:

我正在尝试将我的所有库存移动都放入 2015 年或 2016 年的存储桶中。在我的 From Select 查询部分中,我让它工作,但它为每个项目提供了 2 行,第一行是 2015 年的数量,2016 年的 0,第二行是 2015 年的 0 和 2016 年的数量.所以我想我可以查询 select 语句和 group 来为每个项目获取一行。但我收到“关键字 'GROUP' 附近的语法不正确”错误。有人可以让我朝着正确的方向前进吗?错误发生在 From 子句之外的第二个 Group By 上。谢谢。

SELECT [Item]
    ,[Item Desc]
    ,sum[2015 Usage]
    ,sum[2016 Usage]

FROM (SELECT 
          mtm.[item] 'Item'
          ,im.[description] 'Item Desc'
          ,case when year(convert(varchar(10), convert(date,mtm.[trans_date],1),101))='2015' then sum(mtm.[qty]) else 0 end '2015 Usage'
          ,case when year(convert(varchar(10), convert(date,mtm.[trans_date],1),101))='2016' then sum(mtm.[qty]) else 0 end '2016 Usage'

  FROM [ITEC_App].[dbo].[matltran_mst] mtm, [ITEC_App].[dbo].[item_mst] im

  WHERE
    mtm.[item] = im.[item]
    and
    mtm.[trans_type] NOT IN ('A', 'B', 'C', 'F', 'G', 'H', 'M', 'N', 'R')
    and
    mtm.[ref_type] NOT IN ('P')

  GROUP BY
    mtm.[item]
    ,im.[description]
    ,year(convert(varchar(10), convert(date,mtm.[trans_date],1),101)))

GROUP BY
    [Item]
    ,[Item Desc]

【问题讨论】:

  • 您的子查询需要一个别名
  • @juergend 是对的。在三个右括号之后和最后一组 by 之前添加一个“x”

标签: sql tsql


【解决方案1】:

你的子查询是这样的:

  (
  SELECT . . .
  GROUP BY
    mtm.[item]
    ,im.[description]
    ,year(convert(varchar(10), convert(date,mtm.[trans_date],1),101))
 )

在 SQL Server 中,所有子查询都需要一个表别名。所以,你只需要添加一个:

  (
  SELECT . . .
  GROUP BY
    mtm.[item]
    ,im.[description]
    ,year(convert(varchar(10), convert(date,mtm.[trans_date],1),101))
 ) i

【讨论】:

  • 在 SQL Server 中?哪些数据库不需要需要别名?
  • 谢谢!我确实有一个别名,但有一个 ORDER BY 问题,并考虑了其他事情!
  • @Bohemian 。 . .甲骨文为一个。
猜你喜欢
  • 2016-06-08
  • 2013-12-16
  • 2017-11-22
  • 2018-06-16
  • 2013-10-29
  • 2014-06-22
  • 1970-01-01
  • 2013-05-04
  • 2013-05-21
相关资源
最近更新 更多