添加或删除重复行
我有这样的输出:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们将需要更多信息,例如您的表格包含什么以及您想要什么。
我注意到的一件事是你有一所学校,然后是 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.
我同意其他人的观点,即您的源表和所需的输出都是糟糕的设计。虽然您可能无法对源表执行任何操作,但我建议您使用以下代码和输出:
这将为您提供以下格式的结果:(
注意:在我的 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:
This will give you results in the format:
(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.
这个问题实在是太难以抗拒了,所以我只是猜测一下我们正在处理的数据结构。问题中没有具体说明该技术。这是在 Transact-SQL 中。
结果:
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.
Result: