SQL Server-第二高的销售查询

时间:2018-08-24 06:16:44

标签: sql sql-server

我正在研究这个问题:

  

请显示2009年销售第二高的制造商和2010年销售第二大的制造商。

我尝试找到销售方面排名第二的制造商,但遇到很多错误。有人可以解释我哪里出错了吗?

这是我的代码:

select
  top 1 dim_date.year,
  dim_model.idmanufacturer,
  sum(fact_transactions.totalprice)
from
  (
    select
      top 2 dim_date.year,
      dim_model.idmanufacturer,
      sum(fact_transactions.totalprice)
    from
      fact_transactions
      join dim_date on dim_date.date = fact_transactions.date
      join dim_model on dim_model.idmodel = fact_transactions.idmodel
    where
      dim_date.year = 2009
    Group by
      dim_date.year,
      dim_model.idmanufacturer
    Order by
      sum(fact_transactions.totalprice) Desc
  ) RESULT
Order by
  sum(fact_transactions.totalprice);

以下是错误消息:

Msg 8155, Level 16, State 2, Line 77
No column name was specified for column 3 of 'Result'.
Msg 4104, Level 16, State 1, Line 65
The multi-part identifier "dim_date.year" could not be bound.
Msg 4104, Level 16, State 1, Line 65
The multi-part identifier "dim_model.idmanufacturer" could not be bound.
Msg 4104, Level 16, State 1, Line 65
The multi-part identifier "fact_transactions.totalprice" could not be bound.
Msg 4104, Level 16, State 1, Line 77
The multi-part identifier "fact_transactions.totalprice" could not be bound.

6 个答案:

答案 0 :(得分:0)

使用以下代码,让我知道是否发生任何错误。

SELECT TOP 1 year, 
             idmanufacturer, 
             test 
FROM   (SELECT TOP 2 dim_date.year, 
                     dim_model.idmanufacturer, 
                     Sum(fact_transactions.totalprice) test 
        FROM   fact_transactions 
               JOIN dim_date 
                 ON dim_date.date = fact_transactions.date 
               JOIN dim_model 
                 ON dim_model.idmodel = fact_transactions.idmodel 
        WHERE  dim_date.year = 2009 
        GROUP  BY dim_date.year, 
                  dim_model.idmanufacturer 
        ORDER  BY Sum(fact_transactions.totalprice) DESC) RESULT 
ORDER  BY test; 

如果有任何问题,请创建SQL小提琴。

答案 1 :(得分:0)

第2个元素的限制为1,1,第3个元素的限制为2,1,依此类推。

可以请您尝试一下。

select dim_date.year, dim_model.idmanufacturer, sum(fact_transactions.totalprice) from fact_transactions join dim_date on dim_date.date = fact_transactions.date join dim_model on dim_model.idmodel =fact_transactions.idmodel where dim_date.year = 2009 Group by dim_date.year, dim_model.idmanufacturer Order by sum(fact_transactions.totalprice) Desc ) RESULT Order by sum(fact_transactions.totalprice) limit 1,1;

答案 2 :(得分:0)

您可能正在尝试执行以下操作:

SELECT 
  [year]
, Manufacturer
, TotalPrice
FROM (
SELECT *
, ROW_NUMBER() OVER(PARTITION BY idmanufacturer ORDER BY [year] ASC, TotalPrice DESC) RN
FROM (
    SELECT
      dim_date.year 
    , dim_model.idmanufacturer Manufacturer
    , SUM(fact_transactions.totalprice) TotalPrice
    FROM 
        fact_transactions 
    JOIN dim_date  ON dim_date.date = fact_transactions.date
    JOIN dim_model ON dim_model.idmodel =fact_transactions.idmodel
    where 
         dim_date.year IN(2009,2010)
    GROUP BY 
      dim_date.year 
    , dim_model.idmanufacturer      
 ) RESULT
) D
WHERE 
    RN = 2

步骤:

  1. 获取2009年和2010年每个制造商的TotalPrice的总和。
  2. 使用ROW_NUMBER()对结果进行排序以获取可以轻松选择的订单号。
  3. 通过为每个制造商选择排名第二的销售来最终确定结果。

说明:

第一个查询:

SELECT
  dim_date.year 
, dim_model.idmanufacturer Manufacturer
, SUM(fact_transactions.totalprice) TotalPrice
FROM 
    fact_transactions 
JOIN dim_date  ON dim_date.date = fact_transactions.date
JOIN dim_model ON dim_model.idmodel =fact_transactions.idmodel
where 
     dim_date.year IN(2009,2010)
GROUP BY 
  dim_date.year 
, dim_model.idmanufacturer  

将汇总2009年和2010年的所有dim_model.idmanufacturer交易,并将汇总该期间每个idmanufacturer的总价。

然后,我们将其括在子查询中,以便可以使用ROW_NUMBER()重新排序结果以达到我们的优势,并按年份和每个idmanufacturer的总价对结果进行排序(对它们进行编号)。

SELECT *
, ROW_NUMBER() OVER(PARTITION BY idmanufacturer ORDER BY [year] ASC, TotalPrice DESC) RN
FROM (
    SELECT
      dim_date.year 
    , dim_model.idmanufacturer Manufacturer
    , SUM(fact_transactions.totalprice) TotalPrice
    FROM 
        fact_transactions 
    JOIN dim_date  ON dim_date.date = fact_transactions.date
    JOIN dim_model ON dim_model.idmodel =fact_transactions.idmodel
    where 
         dim_date.year IN(2009,2010)
    GROUP BY 
      dim_date.year 
    , dim_model.idmanufacturer      
 ) RESULT
) D

此方法将扩大我们从所需位置获得所需结果的机会。根据您的要求,我们将为每个制造商获得第二笔最高销售额(第二行)。因此,如果他们要求您获得第三,第五甚至第十名。您可以重复使用相同的查询,只需将rn从2更改为所需的任何位置即可。

答案 3 :(得分:0)

使用row_number()

select
  dim_date.year,
  dim_model.idmanufacturer,
  sum(fact_transactions.totalprice)
from
  (
    select
      row_number() over ( order by sum(fact_transactions.totalprice) desc ) as rowNumber,
      dim_date.year,
      dim_model.idmanufacturer,
      sum(fact_transactions.totalprice) as total
    from
      fact_transactions
      join dim_date on dim_date.date = fact_transactions.date
      join dim_model on dim_model.idmodel = fact_transactions.idmodel
    Group by
      dim_date.year,
      dim_model.idmanufacturer
  ) RESULT
where
  RESULT.rowNumber = 2

答案 4 :(得分:0)

这应该适用于sql-server:

select
    res2.*
from
    (
    select
        *,
        DENSE_RANK() OVER (
    ORDER BY
        res1.price ) rank_price
    from
        (
        select
            dim_date.year,
            dim_model.idmanufacturer,
            sum(fact_transactions.totalprice) price
        from
            fact_transactions
        join dim_date on
            dim_date.date = fact_transactions.date
        join dim_model on
            dim_model.idmodel = fact_transactions.idmodel
        where
            dim_date.year = 2009
        Group by
            dim_date.year,
            dim_model.idmanufacturer ) res1 ) res2
where
    rank_price = 2;

答案 5 :(得分:0)

只需使用offset / fetch

select dim_date.year,
  dim_model.idmanufacturer,
  sum(fact_transactions.totalprice)
from
  fact_transactions
  join dim_date on dim_date.date = fact_transactions.date
  join dim_model on dim_model.idmodel = fact_transactions.idmodel
where
  dim_date.year = 2009
Group by
  dim_date.year,
  dim_model.idmanufacturer
Order by
  sum(fact_transactions.totalprice) Desc
offset 1 fetch first 1 row only;

此ANSI / ISO标准功能自SQL Server 2012起就可以使用。Here是了解更多有关此功能的地方。