如何计算

时间:2018-04-08 05:46:50

标签: sql sql-server tsql

我不确定我的问题是否有意义,但看看我的代码我认为应该。我在根据Select语句中创建的columuns提出NumberOfPurchases时遇到了麻烦。所以在select语句中有两列基于case语句,它们是OrderNumber和Price类别。现在我需要计算所有订单,例如名为PurchaseOrder的所有行也是1.Low Price和所有名为PurchaseOrder和2.AveragePRice的行等等。这就是为什么需要对其进行分组,OrderNumber和PriceCategory是不参与GroupBy子句但是应该在NumberOfPurchases列中的聚合函数的输入。因此,在分组时将它们放在select语句中是合法的。我已经提供了下面的代码,其余的代码已被注释掉,因为我还没有得到它。

select OrderNumber = CASE 
                      WHEN (orderid % 10) > 7 THEN 'Purchase Order'
                      WHEN (orderid % 10) < 2 THEN 'Discounted Order'
                      WHEN (orderid % 2)  = 1 THEN 'Special Order'
                      WHEN (orderid % 2)  = 0 THEN 'Open Markent Purchase Agreement'
                      ELSE 'Missing Value' END
, PriceCategory = CASE 
                WHEN val < 1000 THEN '1. Low Price'
                WHEN val >= 1000 AND val <= 3000 THEN '2. Average Price'
                WHEN val > 3000 THEN '3.High End Price'
                ELSE 'Missing Value' END
, NumberOfPurchases = --Not sure what to write here
--, TotalCost =
--, AverageTotalCost=
from Sales.OrderValues
group by OrderNumber, PriceCategory
order by PriceCategory,OrderNumber

我尝试了不同的计算方法,但我的一个问题是当我写下类似的情况时,例如OrderNumber =&#39;某些事情&#39;该列无法识别。发生这件事并不奇怪。这是预期的输出。enter image description here

以下代码应有助于可视化正在进行的操作。

select OrderNumber = CASE 
                      WHEN (orderid % 10) > 7 THEN 'Purchase Order'
                      WHEN (orderid % 10) < 2 THEN 'Discounted Order'
                      WHEN (orderid % 2)  = 1 THEN 'Special Order'
                      WHEN (orderid % 2)  = 0 THEN 'Open Markent Purchase Agreement'
                      ELSE 'Missing Value' END
, PriceCategory = CASE 
                WHEN val < 1000 THEN '1. Low Price'
                WHEN val >= 1000 AND val <= 3000 THEN '2. Average Price'
                WHEN val > 3000 THEN '3.High End Price'
                ELSE 'Missing Value' END
--, NumberOfPurchases =
--, TotalCost =
--, AverageTotalCost=
from Sales.OrderValues
--group by OrderNumber, PriceCategory
order by PriceCategory,OrderNumber

我得到了这个输出

Discounted Order    1. Low Price
Discounted Order    1. Low Price
Discounted Order    1. Low Price
Discounted Order    1. Low Price
.
.
.
88 rows as the previous statement


Open Markent Purchase Agreement 1. Low Price
Open Markent Purchase Agreement 1. Low Price
Open Markent Purchase Agreement 1. Low Price
Open Markent Purchase Agreement 1. Low Price
Open Markent Purchase Agreement 1. Low Price
Open Markent Purchase Agreement 1. Low Price
.
.
130 rows of the previous statement

Purchase Order  1. Low Price
Purchase Order  1. Low Price 
Purchase Order  1. Low Price
.
.
82 rows of the previous statement
Special Order   1. Low Price
Special Order   1. Low Price
Special Order   1. Low Price
Special Order   1. Low Price
Special Order   1. Low Price
.
.
127 rows of the  previous statement

Discounted Order    2. Average Price
Discounted Order    2. Average Price
Discounted Order    2. Average Price
Discounted Order    2. Average Price
Discounted Order    2. Average Price
.
.
57 rows of the previous statement

Open Markent Purchase Agreement 2. Average Price
Open Markent Purchase Agreement 2. Average Price
Open Markent Purchase Agreement 2. Average Price
.
.
97 rows of the previous statement

1 个答案:

答案 0 :(得分:1)

Sqlserver不支持带有别名的group by

您可以让子查询获取OrderNumberPriceCategory数据,然后您可以在OrderNumberPriceCategory上进行分组。

你可以这样做的简单方法。

SELECT 
    OrderNumber,
    PriceCategory,
    COUNT(1) AS 'NumberOfPurchases',
    AVG(val) as 'AverageTotalCost',
    SUM(val) as 'TotalCost'
FROM
(
SELECT 
  OrderNumber = CASE 
                      WHEN (orderid % 10) > 7 THEN 'Purchase Order'
                      WHEN (orderid % 10) < 2 THEN 'Discounted Order'
                      WHEN (orderid % 2)  = 1 THEN 'Special Order'
                      WHEN (orderid % 2)  = 0 THEN 'Open Markent Purchase Agreement'
                      ELSE 'Missing Value' END
, PriceCategory = CASE 
                WHEN val < 1000 THEN '1. Low Price'
                WHEN val >= 1000 AND val <= 3000 THEN '2. Average Price'
                WHEN val > 3000 THEN '3.High End Price'
                ELSE 'Missing Value' END
,val
from Sales.OrderValues
) AS T
GROUP BY OrderNumber,PriceCategory
order by PriceCategory,OrderNumber

<强>结果

enter image description here