Sql rownumber的值不正确
我有一个表Vente,其中包含以下列:Sql rownumber的值不正确,sql,sql-server,Sql,Sql Server,我有一个表Vente,其中包含以下列: [datecol]||[Code Article]||[Prix de vente TTC]||[Code Site]||[Code Structure]||promo 我希望从vente中提取列,其中promo=0和datecol(销售日期)具有以下条件: 促销=0的28个最长日期中应包括datecol 我试着 select t1.[datecol],t1.[Code Article],t1.[Prix de vente TTC],t1.[
[datecol]||[Code Article]||[Prix de vente TTC]||[Code Site]||[Code Structure]||promo
我希望从vente中提取列,其中promo=0和datecol(销售日期)具有以下条件:
- 促销=0的28个最长日期中应包括datecol
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
我得到了不正确的值,如何修改它 你的询问毫无意义。似乎您在
a
中尝试了某种关联,但这并没有按预期工作。你说的是promo=1
,但你用的是promo=0
。你能再解释一下吗?“其中promo=1”-其中promo=0?与此问题的先前实例中的样式相同。您最终应该自己理解您希望通过此查询获得的内容。和[code Article]=t2。[code Article]
-当您尝试将左侧字段链接到t1
表别名时,服务器给了您语法错误,您删除了它,现在猜猜它在比较的左边是谁的字段?您在Dervied表中的WHERE条件,如[code-Article]=t2.[code-Article]
是无意义的,即与1=1
相同。
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