【问题标题】:syntax change from mySQL 5.5 to SQL Server 2005从 mySQL 5.5 到 SQL Server 2005 的语法更改
【发布时间】:2014-01-23 14:15:39
【问题描述】:

我有以下代码用mySQL 5.5 版本编写。

SELECT ss.ParcelID,ss.ParcelName,sh.Qty1,ShapeID1, sh.Qty2,sh.ShapeID2, sh.Qty3, sh.ShapeID3, sh.Qty4,sh.ShapeID4,
       MAX(case when ShapeID = 'EM' then Cng_InQty end) as EM_Qty,
       MAX(case when ShapeID = 'EM' then Cng_InWeight end) as EM_Weight,
       MAX(case when ShapeID = 'PR' then Cng_InQty end) as PR_Qty,
       MAX(case when ShapeID = 'PR' then Cng_InWeight end) as PR_Weight,
       MAX(case when ShapeID = 'AS' then Cng_InQty end) as AS_Qty,
       MAX(case when ShapeID = 'AS' then Cng_InWeight end) as AS_Weight
FROM sql_history ss
join sql_stock sh
WHERE ss.ParcelID = sh.ParcelID
GROUP BY ss.ParcelID;

我需要在 SQL Server 2005 中执行相同的查询,但我无法使用正确的语法编写查询。

【问题讨论】:

标签: mysql sql sql-server-2005


【解决方案1】:

在 SQL Server 中,不能在没有聚合函数的情况下在 SELECT 语句中使用不在 GROUP BY 中的列。要么使用聚合函数,要么将其添加到GROUP BY

SELECT 
  ss.ParcelID,
  ss.ParcelName,
  sh.Qty1,
  ShapeID1, 
  sh.Qty2,
  sh.ShapeID2, 
  sh.Qty3, 
  sh.ShapeID3, 
  sh.Qty4,
  sh.ShapeID4,
  MAX(case when ShapeID = 'EM' then Cng_InQty end) as EM_Qty,
  MAX(case when ShapeID = 'EM' then Cng_InWeight end) as EM_Weight,
  MAX(case when ShapeID = 'PR' then Cng_InQty end) as PR_Qty,
  MAX(case when ShapeID = 'PR' then Cng_InWeight end) as PR_Weight,
  MAX(case when ShapeID = 'AS' then Cng_InQty end) as AS_Qty,
  MAX(case when ShapeID = 'AS' then Cng_InWeight end) as AS_Weight
FROM sql_history ss
join sql_stock sh ON ss.ParcelID = sh.ParcelID
GROUP BY ss.ParcelID,
  ss.ParcelName,
  sh.Qty1,
  ShapeID1, 
  sh.Qty2,
  sh.ShapeID2, 
  sh.Qty3, 
  sh.ShapeID3, 
  sh.Qty4,
  sh.ShapeID4;

然而,Mysql 允许,在这种情况下,它为这些列选择任意值。

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2012-10-03
    相关资源
    最近更新 更多