我需要创建一个复杂的SQL查询并向其添加行号。 我的查询有3个表连接,添加了一个带有union的自定义行。 无论我如何尝试,我都会遇到语法错误,请帮我找到解决方案。
主要查询:
select null as EAN,
null as CustomsCode,
ProductId as SupplierItemCode,
'![CDATA['+Product.Name+']' as ItemDescription,
'![CDATA['+Product.ShortDescription+']' as ItemNote,
null as VATType,
'CU' as PackageType,
Quantity as OrderQuantity,
'darab' as UnitOfMeasure,
UnitPriceExclTax as OrderedUnitNetPrice
from [Order] inner join OrderItem on [Order].Id=OrderItem.OrderId
Inner join Product on OrderItem.ProductId=Product.Id
where OrderId='150960'
UNION
select null as EAN,
null as CustomsCode,
'00001' as SupplierItemCode,
'![CDATA[Szállítási díj]' as ItemDescription,
'![CDATA[A termék postázási költsége]' as ItemNote,
null as VATType, 'CU' as PackageType,
'1' as OrderQuantity,
'darab' as UnitOfMeasure,
OrderShippingExclTax as OrderedUnitNetPrice
from [Order]
Where [Order].Id='150960'
我需要在此表中添加rank(),而不会获得与行号相同的数字 我的版本是:
select Row_Number() OVER (Order by ProductID) as LineNumber,
null as EAN,
null as CustomsCode,
ProductId as SupplierItemCode,
'![CDATA['+Product.Name+']' as ItemDescription,
'![CDATA['+Product.ShortDescription+']' as ItemNote,
null as VATType,
'CU' as PackageType,
Quantity as OrderQuantity,
'darab' as UnitOfMeasure,
UnitPriceExclTax as OrderedUnitNetPrice
from [Order] inner join OrderItem on [Order].Id=OrderItem.OrderId
Inner join Product on OrderItem.ProductId=Product.Id
where OrderId='150960'
UNION
select Row_Number() OVER (Order by Id) as LineNumber,
null as EAN, null as CustomsCode,
'00001' as SupplierItemCode,
'![CDATA[Szállítási díj]' as ItemDescription,
'![CDATA[A termék postázási költsége]' as ItemNote,
null as VATType, 'CU' as PackageType, '1' as OrderQuantity,
'darab' as UnitOfMeasure,
OrderShippingExclTax as OrderedUnitNetPrice
from [Order]
Where [Order].Id='150960'
导致行号:1,1,2,我得到了与Rank()相同的结果
有人可以帮忙吗?
我尝试的方式:
Select Rank() OVER (ORDER BY ProductId) as LineNumber,
From (select Row_Number() OVER (Order by ProductID) as LineNumber,
null as EAN,
null as CustomsCode,
ProductId as SupplierItemCode,
'![CDATA['+Product.Name+']' as ItemDescription,
'![CDATA['+Product.ShortDescription+']' as ItemNote,
null as VATType,
'CU' as PackageType,
Quantity as OrderQuantity,
'darab' as UnitOfMeasure,
UnitPriceExclTax as OrderedUnitNetPrice
from [Order] inner join OrderItem on [Order].Id=OrderItem.OrderId
Inner join Product on OrderItem.ProductId=Product.Id
where OrderId='150960'
UNION
select Row_Number() OVER (Order by Id) as LineNumber,
null as EAN, null as CustomsCode,
'00001' as SupplierItemCode,
'![CDATA[Szállítási díj]' as ItemDescription,
'![CDATA[A termék postázási költsége]' as ItemNote,
null as VATType, 'CU' as PackageType, '1' as OrderQuantity,
'darab' as UnitOfMeasure,
OrderShippingExclTax as OrderedUnitNetPrice
from [Order]
Where [Order].Id='150960')
答案 0 :(得分:3)
如果您排名的值在记录顺序中不唯一,您将在RANK,DENSE_RANK和ROW_NUMBER中获得重复值。
您正在对您的联合中的两个查询应用排名,并且每个查询将返回包含1的独立排名顺序。我认为您希望在两个联合语句中返回唯一值,然后使用子查询对结果进行排名。
您可以通过将唯一值设置为
来避免这种情况SELECT
Count=Row_Number() OVER (Order by UniqueValue) as LineNumber
FROM
(
SELECT
UniqeValue=NEWID()
FROM
X
UNION
SELECT
UniqeValue=NEWID()
FROM
X
)AS X
答案 1 :(得分:1)
如果我理解了您的问题,那么我认为您需要将g++
表达式置于联盟之外,如下所示:
ROW_NUMBER