2014-09-24 58 views
1

嗨,我有一个复杂的条件。我有一个表可以说“测试”根据当前行选择完整的下一行

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'行执行一些更新操作。

我已经试过领导我可以得到下一行输入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') 
+1

sql server 2008 does not科技支撑LEAD() – 2014-09-24 08:06:21

+0

我已经与SQL Server 2012 – 2014-09-24 08:06:36

+0

尝试过,但,这不是你如何标记问题... – 2014-09-24 08:06:53

回答

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 
0

请记住一张桌子是一组和一组没有为了既不从上到下还是留给right.What如果有人插入一行用于type =再次格设置??。然而尝试下面的代码,看看它是否工作

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 
+0

好吧,我会检查它。谢谢 – 2014-09-24 09:03:21

+0

它也不工作。 – 2014-09-24 11:17:10

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' 
    ); 
+0

这不是工作抱歉。 – 2014-09-24 10:41:13