添加或删除重复行

发布于 2024-08-24 01:51:46 字数 625 浏览 9 评论 0原文

我有这样的输出:

id  name    date        school  school1
1   john    11/11/2001  nyu ucla
1   john    11/11/2001  ucla    nyu
2   paul    11/11/2011  uft mit
2   paul    11/11/2011  mit uft

我想实现这一点:

id  name    date        school  school1
1   john    11/11/2001  nyu ucla
2   paul    11/11/2011  mit uft

我正在使用直接 join ,如下所示:

select distinct
  a.id, a.name,
  b.date,
  c.school

  a1.id, a1.name,
  b1.date,
  c1.school

from table a, table b, table c,table a1, table b1, table c1

where
a.id=b.id
and...

有什么想法吗?

I have an output like this:

id  name    date        school  school1
1   john    11/11/2001  nyu ucla
1   john    11/11/2001  ucla    nyu
2   paul    11/11/2011  uft mit
2   paul    11/11/2011  mit uft

I would like to achieve this:

id  name    date        school  school1
1   john    11/11/2001  nyu ucla
2   paul    11/11/2011  mit uft

I am using direct join as in:

select distinct
  a.id, a.name,
  b.date,
  c.school

  a1.id, a1.name,
  b1.date,
  c1.school

from table a, table b, table c,table a1, table b1, table c1

where
a.id=b.id
and...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫嚣ゝ 2024-08-31 01:51:46

我们将需要更多信息,例如您的表格包含什么以及您想要什么。
我注意到的一件事是你有一所学校,然后是 school1。 3nf 规定,即使您认为关系只是 1 或 2 个附加项目,您也不应该重复字段并向其附加数字以获取更多信息。您需要创建第二个表来存储与 1 到多所学校关联的用户。

We will need more information such as what your tables contain and what you are after.
One thing I noticed is you have a school and then school1. 3nf states that you should never duplicate fields and append numbers to them to get more information even if you think that the relationship will only be 1 or 2 additional items. You need to create a second table that stores a user associated with 1 to many schools.

十级心震 2024-08-31 01:51:46

我同意其他人的观点,即您的源表和所需的输出都是糟糕的设计。虽然您可能无法对源表执行任何操作,但我建议您使用以下代码和输出:

Select id, name, date, school from MyTable;
union
Select id, name, date, school1 from MyTable;
(repeat as necessary)

这将为您提供以下格式的结果:(

id  name    date        school
1   john    11/11/2001  nyu
1   john    11/11/2001  ucla
2   paul    11/11/2011  mit
2   paul    11/11/2011  uft

注意:在我的 SQL 版本中,联合查询会自动选择不同的记录,因此 不需要不同的标志)
使用这种格式,您可以轻松计算每个学生的学校数量、每个学校的学生数量等。

如果处理时间和/或存储空间是这里的一个因素,您可以将其拆分为 2 个表,其中 1 个包含 id,姓名&日期,另一个带有 id &学校(基本上就是乔恩刚才说的)。但如果您只是进行一些简单的统计,这应该足够了。

I agree with everyone else that both your source table and your desired output are poor design. While you probably can't do anything about your source table, I recommend the following code and output:

Select id, name, date, school from MyTable;
union
Select id, name, date, school1 from MyTable;
(repeat as necessary)

This will give you results in the format:

id  name    date        school
1   john    11/11/2001  nyu
1   john    11/11/2001  ucla
2   paul    11/11/2011  mit
2   paul    11/11/2011  uft

(Note: in my version of SQL, union queries automatically select distinct records so the distinct flag isn't needed)
With this format, you could easily count the number of schools per student, number of students per school, etc.

If processing time and/or storage space is a factor here, you could then split this into 2 tables, 1 with the id,name & date, the other with the id & school (basically what JonH just said). But if you're just working up some simple statistics, this should suffice.

耶耶耶 2024-08-31 01:51:46

这个问题实在是太难以抗拒了,所以我只是猜测一下我们正在处理的数据结构。问题中没有具体说明该技术。这是在 Transact-SQL 中。

create table student
(
    id int not null primary key identity,
    name nvarchar(100) not null default '',
    graduation_date date not null default getdate(),
)
go

create table school
(
    id int not null primary key identity,
    name nvarchar(100) not null default ''
)
go

create table student_school_asc
(
    student_id int not null foreign key references student (id),
    school_id int not null foreign key references school (id),
primary key (student_id, school_id)
)
go

insert into student (name, graduation_date) values ('john', '2001-11-11')
insert into student (name, graduation_date) values ('paul', '2011-11-11')
insert into school (name) values ('nyu')
insert into school (name) values ('ucla')
insert into school (name) values ('uft')
insert into school (name) values ('mit')
insert into student_school_asc (student_id, school_id) values (1,1)
insert into student_school_asc (student_id, school_id) values (1,2)
insert into student_school_asc (student_id, school_id) values (2,3)
insert into student_school_asc (student_id, school_id) values (2,4)



select
    s.id,
    s.name,
    s.graduation_date as [date],
    (select max(name) from 
        (select name, 
                RANK() over (order by name) as rank_num 
         from school sc
         inner join student_school_asc ssa on ssa.school_id = sc.id
         where ssa.student_id = s.id) s1 where s1.rank_num = 1) as school,
    (select max(name) from 
        (select name, 
                RANK() over (order by name) as rank_num 
         from school sc
         inner join student_school_asc ssa on ssa.school_id = sc.id
         where ssa.student_id = s.id) s2 where s2.rank_num = 2) as school1

from
    student s

结果:

id  name  date       school  school1 
--- ----- ---------- ------- --------
1   john  2001-11-11 nyu     ucla
2   paul  2011-11-11 mit     uft

This problem was just too irresistable, so I just took a guess at the data structures that we are dealing with. The technology wasn't specified in the question. This is in Transact-SQL.

create table student
(
    id int not null primary key identity,
    name nvarchar(100) not null default '',
    graduation_date date not null default getdate(),
)
go

create table school
(
    id int not null primary key identity,
    name nvarchar(100) not null default ''
)
go

create table student_school_asc
(
    student_id int not null foreign key references student (id),
    school_id int not null foreign key references school (id),
primary key (student_id, school_id)
)
go

insert into student (name, graduation_date) values ('john', '2001-11-11')
insert into student (name, graduation_date) values ('paul', '2011-11-11')
insert into school (name) values ('nyu')
insert into school (name) values ('ucla')
insert into school (name) values ('uft')
insert into school (name) values ('mit')
insert into student_school_asc (student_id, school_id) values (1,1)
insert into student_school_asc (student_id, school_id) values (1,2)
insert into student_school_asc (student_id, school_id) values (2,3)
insert into student_school_asc (student_id, school_id) values (2,4)



select
    s.id,
    s.name,
    s.graduation_date as [date],
    (select max(name) from 
        (select name, 
                RANK() over (order by name) as rank_num 
         from school sc
         inner join student_school_asc ssa on ssa.school_id = sc.id
         where ssa.student_id = s.id) s1 where s1.rank_num = 1) as school,
    (select max(name) from 
        (select name, 
                RANK() over (order by name) as rank_num 
         from school sc
         inner join student_school_asc ssa on ssa.school_id = sc.id
         where ssa.student_id = s.id) s2 where s2.rank_num = 2) as school1

from
    student s

Result:

id  name  date       school  school1 
--- ----- ---------- ------- --------
1   john  2001-11-11 nyu     ucla
2   paul  2011-11-11 mit     uft
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文