通过SQL删除副本
我需要在计算选择之前删除重复项。我需要计算进行了多少个测试。患者在同一测试中无法获得相同的价值。我曾经尝试过不同的
,但它将是(DISTILL)而不是组为
组,而不是(组By)
组。它几乎是相同的,但是不同的
将删除我在dister_test表中需要的其他列。我将视图作为一种很好的做法,但我不确定这有帮助。
TLDR:我想改善一个嵌套的组,但我找不到方法。
table: patient_test (12 rows)
Patient | Test | Value
--------------------------------
Laura | Test A | 1.3
Laura | Test B | 8.9 <-
Laura | Test B | 8.9 <-
Laura | Test C | 8.9
Charles | Test B | 8.9
Charles | Test B | 4.6
Charles | Test C | 5.6
Maria | Test A | 1.2 <--
Maria | Test A | 1.2 <--
Maria | Test B | 1.2
Maria | Test B | 8.9
Maria | Test C | 1.2
我排除了这种方式的重复
SELECT * FROM patient_test
GROUP BY Patient , Test, Value
:
table: patient_test (10 rows)
Patient | Test | Value
--------------------------------
Laura | Test A | 1.3
Laura | Test B | 8.9
Laura | Test C | 8.9
Charles | Test B | 8.9
Charles | Test B | 4.6
Charles | Test C | 5.6
Maria | Test A | 1.2
Maria | Test B | 1.2
Maria | Test B | 8.9
Maria | Test C | 1.2
然后我以这样的方式对它们进行计数:(我想改进的)
SELECT TestName, count(*) AS Result FROM
(SELECT * FROM patient_test
GROUP BY Patient , Test, Value) AS tempPatient
GROUP BY tempPatient.TestName;
我正在使用视图,但是我认为将重复项排除在外,而它们 by 立即可能是更好的。
SELECT TestName, count(*) AS Result FROM
(CustomView) AS tempPatient
GROUP BY tempPatient.TestName;
我不能将此用作解决方案的一部分,因为它将删除我为其他过程所需的其他列。
SELECT DISTINCT Patient,Test,Value
FROM patient_test
这就是我需要的:
TestName | Result
----------------------
Test A | 2
Test B | 5
Test C | 3
我认为这不是两次通过运行组的必要条件。我想找到一种使用示例排除重复项的方法。 任何帮助或建议都很好。
I need remove duplicates in SELECT before count them. I need to count how many test have been done. A patient can't get same value for the same test. I had tried DISTINCT
but it would be a GROUP BY(DISTINCT)
instead of GROUP BY(GROUP BY)
. It is pretty much the same but DISTINCT
will remove other columns I need in patient_test table. I used a view as a good practice, but I'm not sure that is helping.
TLDR: I want to improve a nested GROUP BY, but I can't find a way.
table: patient_test (12 rows)
Patient | Test | Value
--------------------------------
Laura | Test A | 1.3
Laura | Test B | 8.9 <-
Laura | Test B | 8.9 <-
Laura | Test C | 8.9
Charles | Test B | 8.9
Charles | Test B | 4.6
Charles | Test C | 5.6
Maria | Test A | 1.2 <--
Maria | Test A | 1.2 <--
Maria | Test B | 1.2
Maria | Test B | 8.9
Maria | Test C | 1.2
I exclude duplicates this way
SELECT * FROM patient_test
GROUP BY Patient , Test, Value
to get:
table: patient_test (10 rows)
Patient | Test | Value
--------------------------------
Laura | Test A | 1.3
Laura | Test B | 8.9
Laura | Test C | 8.9
Charles | Test B | 8.9
Charles | Test B | 4.6
Charles | Test C | 5.6
Maria | Test A | 1.2
Maria | Test B | 1.2
Maria | Test B | 8.9
Maria | Test C | 1.2
Then I count them this way: (What I want to improve)
SELECT TestName, count(*) AS Result FROM
(SELECT * FROM patient_test
GROUP BY Patient , Test, Value) AS tempPatient
GROUP BY tempPatient.TestName;
I'm using a view, but I think that exclude duplicates and them GROUP BY
right away could be better.
SELECT TestName, count(*) AS Result FROM
(CustomView) AS tempPatient
GROUP BY tempPatient.TestName;
I cannot use this one as a part of the solution, because it will remove other columns I need for other procedures.
SELECT DISTINCT Patient,Test,Value
FROM patient_test
This is what I need:
TestName | Result
----------------------
Test A | 2
Test B | 5
Test C | 3
I think that is not necessary to run GROUP BY
twice. I would like to find a way to exclude duplicates using the examples.
Any help or suggestion would be nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与其他数据库不同,mySQL允许使用
dintife
内部引用的多个列 count():请参阅 demo 。
。
Unlike other databases MySql allows more than one columns referenced with
DISTINCT
insideCOUNT()
:See the demo.
似乎您做了什么。我会做一些非常相似的事情:
如果您的表中有重复项,则必须使用分组或删除它们 - 在这种情况下,这几乎是标准的操作程序。
Seems fine what you have done. I would do something very similar:
If you have duplicates in the table, you have to use grouping or distinct to remove them - that is pretty much standard operating procedure in a case like this.