与联合表相比排名 sql
rank over union with joined tables sql
我需要进行复杂的 SQL 查询并向其中添加行号。
我的查询有 3 tables 加入,添加了一个带联合的自定义行。
无论我怎么尝试,我都会遇到语法错误,请帮助我找到解决方案。
主查询:
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() 添加到此 table 而不是获得与行号相同的数字
我的版本是:
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')
如果您排名的值在记录顺序中不是唯一的,您将在 RANK、DENSE_RANK 和 ROW_NUMBER 中获得重复值。
您正在对联合中的两个查询应用排名,每个查询都将 return 一个包含 1 的独立排名顺序。我认为您想 return 两个联合语句中的唯一值然后用子查询对结果进行排名。
您可以通过将唯一值设置为
来避免这种情况
SELECT
Count=Row_Number() OVER (Order by UniqueValue) as LineNumber
FROM
(
SELECT
UniqeValue=NEWID()
FROM
X
UNION
SELECT
UniqeValue=NEWID()
FROM
X
)AS X
如果我理解你的问题,那么我认为你需要将你的 ROW_NUMBER
表达式放在 union 之外,如下所示:
SELECT
ROW_NUMBER() OVER ( ORDER BY T.SupplierItemCode) AS LineNumber,
T.EAN,
T.CustomsCode,
T.SupplierItemCode,
T.ItemDescription,
T.ItemNote,
T.VATType,
T.PackageType,
T.OrderQuantity,
T.UnitOfMeasure,
T.OrderedUnitNetPrice
FROM
(
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'
) T
我需要进行复杂的 SQL 查询并向其中添加行号。 我的查询有 3 tables 加入,添加了一个带联合的自定义行。 无论我怎么尝试,我都会遇到语法错误,请帮助我找到解决方案。
主查询:
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() 添加到此 table 而不是获得与行号相同的数字 我的版本是:
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')
如果您排名的值在记录顺序中不是唯一的,您将在 RANK、DENSE_RANK 和 ROW_NUMBER 中获得重复值。
您正在对联合中的两个查询应用排名,每个查询都将 return 一个包含 1 的独立排名顺序。我认为您想 return 两个联合语句中的唯一值然后用子查询对结果进行排名。
您可以通过将唯一值设置为
来避免这种情况SELECT
Count=Row_Number() OVER (Order by UniqueValue) as LineNumber
FROM
(
SELECT
UniqeValue=NEWID()
FROM
X
UNION
SELECT
UniqeValue=NEWID()
FROM
X
)AS X
如果我理解你的问题,那么我认为你需要将你的 ROW_NUMBER
表达式放在 union 之外,如下所示:
SELECT
ROW_NUMBER() OVER ( ORDER BY T.SupplierItemCode) AS LineNumber,
T.EAN,
T.CustomsCode,
T.SupplierItemCode,
T.ItemDescription,
T.ItemNote,
T.VATType,
T.PackageType,
T.OrderQuantity,
T.UnitOfMeasure,
T.OrderedUnitNetPrice
FROM
(
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'
) T