如果一个值存在则删除另一个
If one value exists then remove another
我一直在询问如何删除或跳过一个 post(如果另一个存在)。
这是我的table。
如果 L_ID 列具有相同的值 821 AND 201 P_ID 然后 "remove" 或不使用 201 和 sum()
然后时间
这将使 P_ID 80 和 946 只有 2 行。
这可能比我想象的要容易,但我卡住了。
试试这个代码:
SELECT *,
SUM(Time) OVER(PARTITION BY P_ID, L_ID, Date) AS 'Sum'
FROM Your_Table
WHERE L_ID <> 201
AND P_ID NOT IN (
SELECT E1.P_ID
FROM Your_Table E1
INNER JOIN Your_Table E2
ON E1.P_ID = E2.P_ID
WHERE E1.L_ID = 821 AND E2.L_ID = 201)
这样试试:
CREATE TABLE #YourTable(P_ID INT, L_ID INT, [Date] Date, [Time] DECIMAL(6,2));
INSERT INTO #YourTable VALUES
(80,201,{d'2015-08-01'},24.0)
,(80,821,{d'2015-08-01'},24.0)
,(80,822,{d'2015-08-01'},32.0)
,(946,201,{d'2015-08-01'},16.0)
,(946,821,{d'2015-08-01'},16.0)
,(946,819,{d'2015-08-01'},6.65)
,(6758,201,{d'2015-08-01'},7.25)
,(6758,200,{d'2015-08-01'},7.25)
;
--Test output
SELECT * FROM #YourTable;
--Set the SUMs in those lines with L_ID=821
UPDATE #YourTable SET [Time]=(SELECT SUM(x.[Time])
FROM #YourTable AS x
WHERE x.P_ID =#YourTable.[P_ID]
AND x.L_ID IN (821,201))
WHERE #YourTable.L_ID=821
--Delete the rows with L_ID=201 if there is one with 821 too
DELETE FROM #YourTable
WHERE L_ID = 201 AND EXISTS(SELECT * FROM #YourTable AS x
WHERE x.P_ID = #YourTable.P_ID AND x.L_ID =821 ) --The ID was wrong here, sorry...
--Test output
SELECT * FROM #YourTable;
--Clean up
DROP TABLE #YourTable;
结果:
P_ID L_ID Date Time
80 821 2015-08-01 48.00
80 822 2015-08-01 32.00
946 821 2015-08-01 32.00
946 819 2015-08-01 6.65
6758 201 2015-08-01 7.25
6758 200 2015-08-01 7.25
我一直在询问如何删除或跳过一个 post(如果另一个存在)。
这是我的table。
如果 L_ID 列具有相同的值 821 AND 201 P_ID 然后 "remove" 或不使用 201 和 sum()
然后时间
这将使 P_ID 80 和 946 只有 2 行。
这可能比我想象的要容易,但我卡住了。
试试这个代码:
SELECT *,
SUM(Time) OVER(PARTITION BY P_ID, L_ID, Date) AS 'Sum'
FROM Your_Table
WHERE L_ID <> 201
AND P_ID NOT IN (
SELECT E1.P_ID
FROM Your_Table E1
INNER JOIN Your_Table E2
ON E1.P_ID = E2.P_ID
WHERE E1.L_ID = 821 AND E2.L_ID = 201)
这样试试:
CREATE TABLE #YourTable(P_ID INT, L_ID INT, [Date] Date, [Time] DECIMAL(6,2));
INSERT INTO #YourTable VALUES
(80,201,{d'2015-08-01'},24.0)
,(80,821,{d'2015-08-01'},24.0)
,(80,822,{d'2015-08-01'},32.0)
,(946,201,{d'2015-08-01'},16.0)
,(946,821,{d'2015-08-01'},16.0)
,(946,819,{d'2015-08-01'},6.65)
,(6758,201,{d'2015-08-01'},7.25)
,(6758,200,{d'2015-08-01'},7.25)
;
--Test output
SELECT * FROM #YourTable;
--Set the SUMs in those lines with L_ID=821
UPDATE #YourTable SET [Time]=(SELECT SUM(x.[Time])
FROM #YourTable AS x
WHERE x.P_ID =#YourTable.[P_ID]
AND x.L_ID IN (821,201))
WHERE #YourTable.L_ID=821
--Delete the rows with L_ID=201 if there is one with 821 too
DELETE FROM #YourTable
WHERE L_ID = 201 AND EXISTS(SELECT * FROM #YourTable AS x
WHERE x.P_ID = #YourTable.P_ID AND x.L_ID =821 ) --The ID was wrong here, sorry...
--Test output
SELECT * FROM #YourTable;
--Clean up
DROP TABLE #YourTable;
结果:
P_ID L_ID Date Time
80 821 2015-08-01 48.00
80 822 2015-08-01 32.00
946 821 2015-08-01 32.00
946 819 2015-08-01 6.65
6758 201 2015-08-01 7.25
6758 200 2015-08-01 7.25