SQL 带有评估子查询的服务器分组外部查询

SQL Server Grouped Outer Query with evaluating SubQuery

我想 运行 一个将产生聚合结果的查询。 我有以下查询来评估价格列和件数列以生成结果。见下文:

Select  Case When [Price]/[PIECES] < 10 Then 'Low Value'
             Else 
        Case When [Price]/[PIECES] >= 10 Then 'High Value'
             End
        End
AS [High/Low] 
FROM [tbl1]

然后我想获取上述查询的结果(将上述查询的值替换为以下查询中的 [High/Low Value by Piece] 列)并通过 [High/Low 单件价值]。见下文:

SELECT [High/Low Value by Piece], Sum([Pieces]) as Pieces, Sum([Price]) as Price 
FROM [tbl1]
Group by [High/Low Value by Piece]
Order by [High/Low Value by Piece]

您的 case 语句语法错误太多。修复这些问题后,您可以直接在 selectgroup by 中使用 case 语句,类似这样。

SELECT CASE
         WHEN [Price] / [PIECES] < 10 THEN 'Low Value'
         WHEN [Price] / [PIECES] >= 10 THEN 'High Value'
       END           AS [High/Low],
       Sum([Pieces]) AS Pieces,
       Sum([Price])  AS Price
FROM   [tbl1]
GROUP  BY CASE
            WHEN [Price] / [PIECES] < 10 THEN 'Low Value'
            WHEN [Price] / [PIECES] >= 10 THEN 'High Value'
          END