如何清空具有相同列详细信息的其他行

时间:2018-04-30 13:14:37

标签: sql-server-2008 sql-server-2012 sql-server-2008-r2 dense-rank

下面是我的SQL查询

SELECT 
o.OrderNumber, 
oi.Sku, 
Sum(Isnull(oi.Price * oi.Quantity,0)) as Price, 
DENSE_RANK() over(partition by o.orderNumber order by oi.sku) as CouponRowId
from ac_OrderItems oi 
inner join ac_Orders o on oi.OrderId = o.OrderId
Inner Join ac_OrderShipments os on oi.OrderShipmentId =os.OrderShipmentId
WHERE (oi.OrderItemTypeId IN (5))
group by o.OrderNumber, oi.Sku 

以下是我得到的记录

OrderNumber    Sku           Price     CouponRowId
90061          BLACKBERRY   -5.6900    1
90061          LEMON        -5.6900    2
90061          PEACH        -5.6900    3
90061          SHIP100      -10.920    4

但我希望我的记录为

OrderNumber    Sku           Price     
90061          BLACKBERRY   -5.6900    
               LEMON        -5.6900    
               PEACH        -5.6900   
               SHIP100      -10.920    

我希望如果订单号相同,那么所有详细记录都应该在第1行,然后在其他记录之后应该只显示Sku和价格,我们还需要删除DENSE_RANK()列

3 个答案:

答案 0 :(得分:1)

看到你的评论这是尝试在查询本身上解决它(如果OrderNumber是唯一的,我假设CouponRowID可以完成这项工作):

WITH
    tmp AS (
               SELECT o.OrderNumber, oi.Sku, SUM(ISNULL(oi.Price * oi.Quantity, 0)) AS Price, DENSE_RANK() OVER (PARTITION BY o.orderNumber ORDER BY oi.sku) AS CouponRowId
               FROM ac_OrderItems oi
                    INNER JOIN ac_Orders o ON oi.OrderId=o.OrderId
                    INNER JOIN ac_OrderShipments os ON oi.OrderShipmentId=os.OrderShipmentId
               WHERE(oi.OrderItemTypeId IN (5))
               GROUP BY o.OrderNumber, oi.Sku
           )
SELECT CASE WHEN tmp.CouponRowId=1 THEN tmp.OrderNumber END AS OrderNumber, tmp.Sku, tmp.Price
FROM tmp
ORDER BY tmp.OrderNumber, tmp.SKu;

答案 1 :(得分:1)

  

通过查看查询看起来像Partition,Clause可以帮助您   解决问题。请看下面的查询。我不太确定   关于数据模型,但看起来你需要使用分区   OrderNumber列。CTE。请将第一个;WITH CTE_Table AS ( SELECT 90061 'OrderNumber' , 'BLACKBERRY' Sku, -5.6900 'Price', 1 'CouponRowId' UNION SELECT 90061, 'LEMON', -5.6900, 2 UNION SELECT 90061, 'PEACH', -5.6900, 3 UNION SELECT 90061, 'SHIP100', -10.920, 4 UNION SELECT 90062 'OrderNumber' , 'BLACKBERRY' Sku, -5.6900 'Price', 1 'CouponRowId' UNION SELECT 90062, 'LEMON', -5.6900, 2 UNION SELECT 90062, 'PEACH', -5.6900, 3 UNION SELECT 90062, 'SHIP100', -10.920, 4 ), CTE_New AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY OrderNumber ORDER BY OrderNumber) Patrn FROM CTE_Table ) SELECT CASE WHEN Patrn =1 THEN CONVERT(VARCHAR(10),OrderNumber) ELSE '' END OrderNumber, Sku, Price, CouponRowId FROM CTE_New ;WITH CTE_Table AS ( SELECT o.OrderNumber, oi.Sku, Sum(Isnull(oi.Price * oi.Quantity,0)) as Price, DENSE_RANK() over(partition by o.orderNumber order by oi.sku) as CouponRowId from ac_OrderItems oi inner join ac_Orders o on oi.OrderId = o.OrderId Inner Join ac_OrderShipments os on oi.OrderShipmentId =os.OrderShipmentId WHERE (oi.OrderItemTypeId IN (5)) group by o.OrderNumber, oi.Sku ), CTE_New AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY OrderNumber ORDER BY OrderNumber) Patrn FROM CTE_Table ) SELECT CASE WHEN Patrn =1 THEN CONVERT(VARCHAR(10),OrderNumber) ELSE '' END OrderNumber, Sku, Price, CouponRowId FROM CTE_New 中的查询替换为您的实际表格,这类似于第二个查询。我不能测试它,因为我没有环境。请测试查询

new object

答案 2 :(得分:1)

对不起....!我无法理解你的解释.. 我只是为了得到你期望的OP而工作......

create table #ac_OrderItems(id varchar(20) --varchar, i used instead of int.Beside, varchar is easy to change to int. 
, Sku varchar(20), Price decimal, CouponRowId int)

insert into #ac_OrderItems values(
90061 , 'BLACKBERRY' ,   -5.6900 , 1)
,(90061, 'LEMON', -5.6900, 2  )
,(90061, 'PEACH', -5.6900, 3  )
,(90061, 'SHIP100', -10.920, 4 )
,(90062 , 'BLACKBERRY' ,-5.6900 ,   1  )
,(90062, 'LEMON', -5.6900, 2  )
,(90062, 'PEACH', -5.6900, 3  )
,(90062, 'SHIP100', -10.920, 4)

select * from #ac_OrderItems

;with
orders(id, sku, price, couponId, rn)
as
(
select *, ROW_NUMBER() over(partition by id order by id) from #ac_OrderItems
)select iif(rn = 1,id,''),sku,price,couponId from orders
-- if i used int, empty is denoted by 0. But you want '' this. So i used varchar id

让你了解我,你得到了什么。