1

我有这样的输出:

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...

有任何想法吗?

4

3 回答 3

0

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

于 2010-03-04T20:06:47.280 回答
0

我同意其他所有人的观点,即您的源表和所需的输出都是糟糕的设计。虽然您可能无法对源表做任何事情,但我推荐以下代码和输出:

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 版本中,联合查询会自动选择不同的记录,因此不需要distinct标志)使用这种格式,您可以轻松计算每个学生的学校数量、每个学校的学生数量等。

如果处理时间和/或存储空间是这里的一个因素,那么您可以将其拆分为 2 个表,1 个带有 id、name 和 date,另一个带有 id 和 school(基本上是 JonH 刚才所说的)。但是,如果您只是在处理一些简单的统计数据,这应该就足够了。

于 2010-03-04T20:42:00.050 回答
0

这个问题太不可抗拒了,所以我只是猜测了我们正在处理的数据结构。问题中没有具体说明该技术。这是在 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
于 2010-03-04T20:58:37.157 回答