You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL订单小计求和函数异常求助(Northwind库+SSMS环境)

Got it, let's fix your SQL script step by step. I can spot a couple of syntax issues that are causing the column name/syntax errors you're seeing:

1. 核心语法错误:小数格式化写法不对

You tried to wrap the SUM calculation and the decimal place 2 in a single parentheses, which isn't valid SQL syntax. To round the subtotal to 2 decimal places, you need to use the ROUND() function properly—wrap the entire aggregate calculation inside ROUND(), with the decimal count as the second parameter.

2. 不完整的分组子句

Your GROUP BY clause cuts off at Ord...—you need to explicitly include OrderID here, since it's a non-aggregated column in your SELECT clause (SQL requires all non-aggregated columns to be listed in GROUP BY).

修正后的完整脚本

SELECT 
    [OrderID],
    ROUND(SUM(UnitPrice * Quantity * (1 - Discount)), 2) AS [Order Subtotal]
FROM 
    [dbo].[Order Details]
GROUP BY 
    [OrderID]

可选:更精确的小数截断(避免四舍五入)

If you want to truncate the result to 2 decimals instead of rounding it, use CAST() to convert the sum to a DECIMAL type with fixed precision:

SELECT 
    [OrderID],
    CAST(SUM(UnitPrice * Quantity * (1 - Discount)) AS DECIMAL(10, 2)) AS [Order Subtotal]
FROM 
    [dbo].[Order Details]
GROUP BY 
    [OrderID]

内容的提问来源于stack exchange,提问作者DoktorAgon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:03:20