如果您对此案件有疑问,请与我们联系: 我想根据特定列
删除多行<Control.Resources>
<a:StringFormatConverter x:Key="StringFormatConverter"/>
</Control.Resources>
<!-- ... -->
<ContentControl
Content="{Binding SelectedOrder,
Converter={StaticResource StringFormatConverter},
ConverterParameter='Details for Order {0}'}"
ContentTemplate="{StaticResource SectionHeader}"/>
假设我要删除多行包含用户&#34;星巴克&#34;超过1,我想删除id为null的那个。 但是如果没有包含null的id,比如&#34; Burger&#34;,我只想删除一个,然后选择1,无论它是什么。 所以,它应该是这样的:
id | date | user | item | material | Text | Description |
------+------------+-----------+-------+--------------------+------+-------------+
12345 | 31.03.2015 | Starbucks | 00010 | 000000001011000106 | abcd | something |
null | 31.03.2015 | Starbucks | 00010 | 000000001011000106 | abcd | something |
54321 | 31.03.2015 | Burger | 00010 | 000000001011000106 | abcd | something |
11111 | 31.03.2015 | Burger | 00010 | 000000001011000106 | abcd | something |
可以吗?
答案 0 :(得分:3)
是的,你可以。
您需要将结果分组以保留,然后将该表与连接一起使用以连接到主表。这是一种仅使用max id和no null id保存记录的方法
Delete from [Table]
From [Table]
left join (
Select MAX(ID) as ID
from [Table]
Where ID is not Null
group by [USER],item,[date],material,[TEXT],Description
-- Results to Keep
) as P on [Table].ID=P.ID
where P.ID is null
答案 1 :(得分:0)
您还可以在Window functions
中使用CTE示例数据
DECLARE @tbl TABLE (id INT, [date] VARCHAR(10) ,[user] VARCHAR(20), item VARCHAR(10), material VARCHAR(50), [Text] VARCHAR(10), [Description] VARCHAR(20))
INSERT @tbl
SELECT 12345, '1.03.2015', 'Starbucks', '00010', '000000001011000106', 'abcd', 'something' UNION
SELECT null, '31.03.2015', 'Starbucks', '00010', '000000001011000106', 'abcd', 'something' UNION
SELECT 54321, '31.03.2015', 'Burger', '00010', '000000001011000106', 'abcd', 'something' UNION
SELECT 11111, '31.03.2015', 'Burger', '00010', '000000001011000106', 'abcd', 'something'
<强>查询强>
;WITH C AS(
SELECT ROW_NUMBER() OVER(PARTITION BY [user] ORDER BY [user]) AS Rn
,Id
FROM @tbl
WHERE id IS NOT NULL
)
DELETE FROM @tbl WHERE Id IN(
SELECT Id
FROM C
WHERE Rn != 1
AND id IS NOT NULL)
OR Id IS NULL
<强>输出强>
SELECT * FROM @tbl
id date user item material Text Description
--------------------------------------------------------------------------------
11111 31.03.2015 Burger 00010 000000001011000106 abcd something
12345 1.03.2015 Starbucks 00010 000000001011000106 abcd something
答案 2 :(得分:0)
这可以帮助
- 样本数据
Create TABLE ABC (id INT, [date] VARCHAR(10) ,[user] VARCHAR(20), item VARCHAR(10), material VARCHAR(50), [Text] VARCHAR(10), [Description] VARCHAR(20))
INSERT ABC
SELECT 12345, '1.03.2015', 'Starbucks', '00010', '000000001011000106', 'abcd', 'something' UNION
SELECT null, '31.03.2015', 'Starbucks', '00010', '000000001011000106', 'abcd', 'something' UNION
SELECT 54321, '31.03.2015', 'Burger', '00010', '000000001011000106', 'abcd', 'something' UNION
SELECT 11111, '31.03.2015', 'Burger', '00010', '000000001011000106', 'abcd', 'something'
- 结果
select * from ABC
- 查询
; With CTE AS
(SELECT *, ROW_NUMBER() over(partition by [user] order by ID desc) row_id from ABC
) DELETE from CTE where row_id >1
- 最终结果
select * from ABC