我有这样的输出:
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...
有什么想法吗?
答案 0 :(得分:0)
我们需要更多信息,例如您的表包含什么以及您追求的内容。 我注意到的一件事是你有一所学校,然后是school1。 3nf指出,即使您认为该关系只有1或2个附加项,您也不应该复制字段并向其附加数字以获取更多信息。您需要创建第二个表来存储与1到多所学校相关联的用户。
答案 1 :(得分: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&日期,另一个与id&学校(基本上是JonH刚刚说的)。但是如果你只是编写一些简单的统计数据,那就足够了。
答案 2 :(得分: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