您好我创建了一个以下查询,我正面临问题
SqlQuery类
;with tbl as
(
select ROW_NUMBER() over(partition by oi.Name,T.gwtxid order by o.order_id desc) as number,
isnull(oi.Name,'') AS 'Name'
,isnull(T.gwtxid,'') as 'gwtxid'
FROM ORDERS o
INNER JOIN
ORDERITEMS oi on o.Order_ID=oi.Order_ID
left JOIN
PAYMENTS P on p.Order_ID=o.Order_ID
left JOIN
TRANSACTIONS T ON T.Transaction_ID=P.Transaction_ID
WHERE O.TOTALCHARGES>0 and o.OrderNumber is not null and o.Order_ID=195180046
)
select * from tbl where number=1
我的记录似乎如下表格结构
Number Name gwtxid
1 Skunk2 Lower 00WA90298MF810153R
1 Skunk2 Lower 03GF53362DR764524D
1 CA_Taxable 3GF53362DR764524D
1 CA_Taxable 0WA90298MF810153R
1 UPS Ground 0WA90298MF810153R
1 UPS Ground 3GF53362DR764524D
我只需要名称的唯一性,然后可以重复交易ID
以下是我的要求必需的表格结构
Name gwtxid
Skunk2 Lower 03GF53362DR764524D
CA_Taxable 0WA90298MF810153R
UPS Ground
编辑必需的表格结构其中gwtxid重复也可以很好
Name gwtxid
Skunk2 Lower 03GF53362DR764524D
CA_Taxable 0WA90298MF810153R
UPS Ground 03GF53362DR764524D
任何人都可以帮助我解决这个问题,我曾尝试过使用rownumber()并且提供了错误的记录
答案 0 :(得分:2)
(
PARTITION BY
子句)将查询结果集划分为分区。窗口功能是 分别应用于每个分区,并为每个分区重新开始计算 分区。
在你的情况下:
partition by oi.Name,T.gwtxid
为您提供了这些群组/分区:
Group #
1 Skunk2 Lower 00WA90298MF810153R
2 Skunk2 Lower 03GF53362DR764524D
3 CA_Taxable 3GF53362DR764524D
4 CA_Taxable 0WA90298MF810153R
5 UPS Ground 0WA90298MF810153R
6 UPS Ground 3GF53362DR764524D
由于示例中的每个组只包含一行,因此每条记录都会得到number
= 1。
如果您更改为partition by oi.Name
,则会获得以下群组:
Group #
1 Skunk2 Lower 00WA90298MF810153R
1 Skunk2 Lower 03GF53362DR764524D
2 CA_Taxable 3GF53362DR764524D
2 CA_Taxable 0WA90298MF810153R
3 UPS Ground 0WA90298MF810153R
3 UPS Ground 3GF53362DR764524D
然后你应该得到你正在寻找的答案。