rownumber的值不正确

时间:2016-03-07 10:27:40

标签: sql sql-server

我有一张表Vente,其中包含以下列:

[datecol]||[Code Article]||[Prix de vente TTC]||[Code Site]||[Code Structure]||promo

我希望从vente中提取promo = 0和datecol(销售日期)的列具有以下条件:

  • datecol应包含在promo = 0
  • 的28个最长日期中

我试试

    select t1.[datecol],t1.[Code Article],t1.[Prix de vente TTC],t1.[Code Site],t1.[Code Structure]
      from [Vente] t1
      inner join (select distinct [datecol],
      row_number() over(partition by [Code Article],[Code Site] ,[Code Structure] order by [datecol] desc) as rn
           from [Vente] t2
            where promo = 0
            and [Code Article] = t2.[Code Article]
            and [Code Structure]=t2.[Code Structure]
            and [Code Site]=t2.[Code Site]
            and ([Code Article] is not null) and ([Code Structure] is not null) and ([Prix de Revient] is not null)
          ) a
           on a.datecol=t1.datecol
            where promo = 0  and  rn <= 28

我也尝试以下查询

select t1.[datecol],t1.[Code Article],t1.[Prix de vente TTC],t1.[Code Site],t1.[Code Structure]
      from [Vente MPX] t1
      inner join (
            select top 28 [datecol] from (select distinct [datecol]
            from [Vente MPX] t2
            where promo = 0
            and [Code Article] = t2.[Code Article]
            and [Code Structure]=t2.[Code Structure]
            and [Code Site]=t2.[Code Site]
            and ([Code Article] is not null) and ([Code Structure] is not null) and ([Prix de Revient] is not null)
            ) t 
           ) a
           on a.datecol=t1.datecol
            where promo = 0 

我的值不正确,如何修改?

0 个答案:

没有答案