嗨,我有一个复杂的情况。我有一张桌子可以说'测试'
ID Partner Type Amount
143854 CSTC Purchase -0.81
144029 CSTC Purchase -0.69
144030 CSTC Purchase -1.33
144031 CSTC Purchase -0.47
144032 CSTC Purchase -1.8
149527 CSTC div 1574.48
149528 CSTC Purchase -1574.48
149531 CSTC div 867.53
149532 CSTC Purchase -867.53
149539 CSTC div 76.2
149540 CSTC Purchase -76.2
149550 CSTC div 8.77
149551 CSTC Purchase -8.77
149554 CSTC div 700.45
149555 CSTC Purchase -700.45
我想删除每次出现的Type =' div'并且下一行应该有Type =' Purchase'
,如果type = 'div'
和下一行type = 'Purchase'
删除了其他内容我想对type = 'div'
行执行某些更新操作。
我已经尝试过Lead我可以获得下一行输入col值但它没有帮助。
select LEAD([Type]) OVER (ORDER BY ID) Next, ID, Partner,[Type],Amount from Test where date='9/18/2014' and ([Type] = 'div' or [Type] = 'Purchase')
答案 0 :(得分:0)
请记住一个表是一个集合,一个集合没有从上到下或从左到右的顺序。如果有人再次为type = div插入一行?但是,请尝试使用以下代码查看它是否正常工作
DECLARE @T TABLE (ID INT,[Partner] VARCHAR(10), [Type] VARCHAR(20),Amount decimal (10,2))
INSERT INTO @T
VALUES
(143854 ,'CSTC' , 'Purchase ', -0.81)
,(144029 , 'CSTC' , 'Purchase' , -0.69)
,(144030 , 'CSTC', 'Purchase' , -1.33)
,(144031 , 'CSTC', 'Purchase' , -0.47)
,(144032 , 'CSTC' , 'Purchase' , -1.8)
,(149527 , 'CSTC' , 'div' , 1574.48)
,(149528 , 'CSTC' , 'Purchase' , -1574.48)
,(149531 , 'CSTC' , 'div' , 867.53)
,(149532 , 'CSTC' , 'Purchase' , -867.53)
,(149539 ,'CSTC', 'div' , 76.2)
,(149540 ,'CSTC' , 'Purchase' , -76.2)
,(149550 , 'CSTC' , 'div' , 8.77)
,(149551 , 'CSTC' , 'Purchase' , -8.77)
,(149554 , 'CSTC', 'div' , 700.45)
,(149555 , 'CSTC', 'Purchase' , -700.45)
;with cte as
(
select *
,(SELECT TOP 1 Amount FROM @t WHERE ID > t.ID AND [type] = 'purchase' ORDER BY ID ASC) as nxt
,(SELECT TOP 1 Amount FROM @t WHERE ID < t.ID AND [type] = 'div' ORDER BY ID DESC) AS pvs
FROM @T T
)
DELETE FROM cte WHERE (Amount + nxt = 0) OR (Amount + pvs = 0)
select * FROM @T
答案 1 :(得分:0)
我的LEAD解决方案:
IF OBJECT_ID('Test1', 'U') IS NOT NULL
DROP TABLE Test1;
go
CREATE TABLE Test1 ( ID INTEGER NOT NULL
, Partner VARCHAR(100) NOT NULL
, Type VARCHAR(20) NOT NULL
, Amount FLOAT NOT NULL );
go
INSERT INTO Test1 ( ID, Partner, Type, Amount )
VALUES ( 143854, 'CSTC', 'Purchase', -0.81 )
, ( 144029, 'CSTC', 'Purchase', -0.69 )
, ( 144030, 'CSTC', 'Purchase', -1.33 )
, ( 144031, 'CSTC', 'Purchase', -0.47 )
, ( 144032, 'CSTC', 'Purchase', -1.8 )
, ( 149527, 'CSTC', 'div ', 1574.48 )
, ( 149528, 'CSTC', 'Purchase', -1574.48 )
, ( 149531, 'CSTC', 'div ', 867.53 )
, ( 149532, 'CSTC', 'Purchase', -867.53 )
, ( 149539, 'CSTC', 'div ', 76.2 )
, ( 149540, 'CSTC', 'Purchase', -76.2 )
, ( 149550, 'CSTC', 'div ', 8.77 )
, ( 149551, 'CSTC', 'Purchase', -8.77 )
, ( 149554, 'CSTC', 'div ', 700.45 )
, ( 149555, 'CSTC', 'Purchase', -700.45 )
go
WITH CTE_LEAD
AS
(
SELECT LEAD([Type]) OVER (ORDER BY ID) NextType
, ID
, Partner
, [Type]
, Amount
from Test1
)
SELECT *
FROM CTE_LEAD
WHERE NextType = 'Purchase' AND Type = 'div';
WITH CTE_LEAD
AS
(
SELECT LEAD([Type]) OVER (ORDER BY ID) NextType
, ID
, Partner
, [Type]
, Amount
from Test1
)
DELETE FROM Test1
WHERE ID IN (
SELECT ID
FROM CTE_LEAD
WHERE NextType = 'Purchase' AND Type = 'div'
UNION ALL
SELECT ID + 1
FROM CTE_LEAD
WHERE NextType = 'Purchase' AND Type = 'div'
);
答案 2 :(得分:0)
米勒离开了左边正在打破它
WITH TestWithRowNumber AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY ID ASC) AS RowNumber
FROM Test
)
Select *
From TestWithRownumber FirstRow
join TestWithRownumber secondrow
on secondrow.RowNumber = firstrow.RowNumber + 1
and secondrow.type = 'Purchase'
and firstrow.type = 'div'
删除声明
WITH TestWithRowNumber AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY ID ASC) AS RowNumber
FROM Test
)
delete Test
From TestWithRownumber FirstRow
join TestWithRownumber secondrow
on secondrow.RowNumber = firstrow.RowNumber + 1
and secondrow.type = 'Purchase'
and firstrow.type = 'div'
join test
on test.ID = FirstRow.ID
or test.ID = secondrow.ID