MySQL GROUP BY doesn't work when migrated to SQL Server 2012

前端 未结 3 1668
执笔经年
执笔经年 2021-01-23 06:08

I\'m moving my Delphi application from MySQL to SQL server 2012. In MySQL I had this query:

SELECT *,(XS+S+M+L+XL+XXL+[1Size]+Custom) as Total FROM StockData  
G         


        
3条回答
  •  鱼传尺愫
    2021-01-23 06:54

    SQL Server is working as expected. You must include all items in your SELECT list in either a GROUP BY or in an aggregate function:

    SELECT *,(XS+S+M+L+XL+XXL+[1Size]+Custom) as Total 
    FROM StockData  
    -- GROUP BY ID,StyleNr,Customer,Color, XS,S,M,L,XL,XXL,[1Size],Custom
    ORDER BY StyleNr,Customer,Color
    

    Or you might be able to use:

    SELECT StyleNr,Customer,Color, SUM(XS+S+M+L+XL+XXL+[1Size]+Custom) as Total 
    FROM StockData  
    GROUP BY StyleNr,Customer,Color
    ORDER BY StyleNr,Customer,Color;
    

提交回复
热议问题