在列中填写缺失值

时间:2017-10-12 11:29:43

标签: sql tsql

我有一张桌子,我有一个tendertype,显示在salesrow上已经完成了哪些付款。

更新:抱歉,我不够清楚 - 它应该由SalesHeader分组,因为另一个销售主管可以有不同的值

  

示例1 中,我只有一笔付款的记录。这应该是   出现在所有记录上

     

示例2 中,我对所有记录都有2笔付款。但我应该采取   其中一个并填写空值,但保留另一个原样。

示例数据

DECLARE @Table TABLE (
SalesHeader varchar(10),Amount int,TenderType nvarchar(50)
)

INSERT INTO @Table
VALUES

('A',1   ,'MasterCard' ),
('A',10  ,NULL        ),
('A',100 ,NULL        ),
('A',10  ,NULL        ),
('A',1   ,NULL        ),
('B',1   ,'CASH'      ),
('B',100 , NULL        )
select * from @Table

Image that shows how it looks like

到目前为止我尝试了什么?

SELECT 
  [Fakturanummer] as SalesHeader

  ,[Omsætning] as Amount

  ,a.[TenderType]
  ,x.TenderType 
FROM [CE_COG-BI-EDW].[dm].[SALG_TRANSAKTIONER] a 

CROSS APPLY(
select top 1 TenderType from  [CE_COG-BI-EDW].[dm].[SALG_TRANSAKTIONER] t
where TenderType is not null and a.Fakturanummer = t.Fakturanummer
)x

where DATASET = 'se' and   Fakturanummer in ('5133324','104893')
order by Fakturanummer

enter image description here

示例1:

SalesHeader    | Amount    | TenderType
----------------------------------------
A                1           MasterCard
A                10          NULL
A                100         NULL
A                10          NULL
A                1           NULL
B                10          CASH
B                100         NULL

期望的结果1

SalesHeader    | Amount    | TenderType
----------------------------------------
A                1           MasterCard
A                10          MasterCard
A                100         MasterCard
A                10          MasterCard
A                1           MasterCard
B                1           CASH
B                100         CASH

示例2:

SalesHeader    | Amount    | TenderType
----------------------------------------
A                1           MasterCard
A                10          CASH
A                100         NULL
A                10          NULL
A                1           NULL

期望的结果2

SalesHeader    | Amount    | TenderType
----------------------------------------
A                1           MasterCard
A                10          CASH
A                100         MasterCard
A                10          MasterCard
A                1           MasterCard

我尝试了一些CROSS APPLY,但我无法理解它。

2 个答案:

答案 0 :(得分:2)

UPDATE myTABLE
SET TenderType = (
        SELECT TOP 1 TenderType
        FROM myTable
        WHERE TenderType IS NOT NULL
        )
WHERE TenderType IS NULL

答案 1 :(得分:2)

好吧,好吧。写它是有帮助的。这让我心烦意乱

似乎我应该这样做,并且感谢@apomene对前1的评论 - 我想通了。

SELECT 
  [Fakturanummer] as SalesHeader

  ,[Omsætning] as Amount

  ,a.[TenderType]
  ,case when a.TenderType IS not null then a.TenderType else x.TenderType 
 end as newTenderType   
 FROM [CE_COG-BI-EDW].[dm].[SALG_TRANSAKTIONER] a 

 CROSS APPLY(
  select top 1 TenderType from  [CE_COG-BI-EDW].[dm].[SALG_TRANSAKTIONER] t
 where TenderType is not null and a.Fakturanummer = t.Fakturanummer
 )x

where DATASET = 'se' and   Fakturanummer in ('5133324','104893')
order by Fakturanummer