考虑一个包含三个行的名称的数据库表:
Peter
Paul
Mary
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将其转换为单个字符串Peter, Paul, Mary?
我需要使用其"父"表中的数据更新SQL Server 2005中的此表,如下所示:
拍卖
id (int)
udid (int)
assid (int)
Run Code Online (Sandbox Code Playgroud)
UD
id (int)
assid (int)
Run Code Online (Sandbox Code Playgroud)
sale.assid包含要更新的正确值ud.assid.
什么查询会这样做?我在考虑,join但我不确定是否可能.
正如标题所示,我想选择用a组成的每组行的第一行GROUP BY.
具体来说,如果我有一个purchases看起来像这样的表:
SELECT * FROM purchases;
Run Code Online (Sandbox Code Playgroud)
我的输出:
id | customer | total ---+----------+------ 1 | Joe | 5 2 | Sally | 3 3 | Joe | 2 4 | Sally | 1
我想查询每个产品id的最大购买量(total)customer.像这样的东西:
SELECT FIRST(id), customer, FIRST(total)
FROM purchases
GROUP BY customer
ORDER BY total DESC;
Run Code Online (Sandbox Code Playgroud)
预期产出:
FIRST(id) | customer | FIRST(total)
----------+----------+-------------
1 | Joe | 5
2 | Sally | 3
我怎么能unique row id不unique row id存在?
我的桌子是
col1 col2 col3 col4 col5 col6 col7
john 1 1 1 1 1 1
john 1 1 1 1 1 1
sally 2 2 2 2 2 2
sally 2 2 2 2 2 2
Run Code Online (Sandbox Code Playgroud)
我希望在重复删除后留下以下内容:
john 1 1 1 1 1 1
sally 2 2 2 2 2 2
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些查询,但我认为它们依赖于行ID,因为我没有得到理想的结果.例如:
DELETE
FROM table
WHERE col1 IN (
SELECT id
FROM table
GROUP BY id
HAVING (COUNT(col1) > 1)
)
Run Code Online (Sandbox Code Playgroud) 可能重复:
SQL - 如何删除重复的行?
我有一个行数非常多的表.不允许重复,但由于行如何创建的问题我知道这个表中有一些重复.我需要从关键列的角度消除额外的行.其他一些列的数据可能略有不同,但我并不关心.我仍然需要保留其中一行.SELECT DISTINCT将无法工作,因为它在所有列上运行,我需要根据键列抑制重复.
如何删除额外的行但仍保持有效?
考虑一个名为EmployeeNametable 的列Employee.目标是根据EmployeeName字段删除重复记录.
EmployeeName
------------
Anand
Anand
Anil
Dipak
Anil
Dipak
Dipak
Anil
Run Code Online (Sandbox Code Playgroud)
使用一个查询,我想删除重复的记录.
如何在SQL Server中使用TSQL?
什么相当于SQL Server中的Oracle RowID?
我在下面的表格中有以下记录
create table employee
(
EmpId number,
EmpName varchar2(10),
EmpSSN varchar2(11)
);
insert into employee values(1, 'Jack', '555-55-5555');
insert into employee values (2, 'Joe', '555-56-5555');
insert into employee values (3, 'Fred', '555-57-5555');
insert into employee values (4, 'Mike', '555-58-5555');
insert into employee values (5, 'Cathy', '555-59-5555');
insert into employee values (6, 'Lisa', '555-70-5555');
insert into employee values (1, 'Jack', '555-55-5555');
insert into employee values (4, 'Mike', '555-58-5555');
insert into employee values (5, 'Cathy', '555-59-5555');
insert into employee values (6 ,'Lisa', …Run Code Online (Sandbox Code Playgroud) 检查问题此SELECT查询需要180秒才能完成(检查问题本身的注释).
IN只能与一个值进行比较,但时差仍然很大.
为什么会那样?
可能重复:
SQL - 如何删除重复的行?
示例这是我的表:
SiteKey,名称,城市
SiteKey是自动增量,名称不同,但有时2个Sitekey将具有相同的城市.
例:
1, A , CityA
2, B, CityB
3, C, CityA
4, D, CityF
Run Code Online (Sandbox Code Playgroud)
所以我需要删除第3行,并保留1,2,4行.
它在SQL 2005及以上.
谢谢你的帮助.
sql ×10
t-sql ×6
sql-server ×4
duplicates ×2
mysql ×2
comparison ×1
csv ×1
delete-row ×1
group-by ×1
group-concat ×1
performance ×1
postgresql ×1
row-number ×1
rowid ×1
sql-delete ×1
sql-update ×1
sqlite ×1