通过SQL删除副本

发布于 2025-02-04 11:00:02 字数 2019 浏览 4 评论 0原文

我需要在计算选择之前删除重复项。我需要计算进行了多少个测试。患者在同一测试中无法获得相同的价值。我曾经尝试过不同的,但它将是(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 技术交流群。

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

发布评论

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

评论(2

何其悲哀 2025-02-11 11:00:02

与其他数据库不同,mySQL允许使用dintife内部引用的多个列 count():

SELECT Test, COUNT(DISTINCT Patient, Value) AS Result
FROM patient_test 
GROUP BY Test;

请参阅 demo

Unlike other databases MySql allows more than one columns referenced with DISTINCT inside COUNT():

SELECT Test, COUNT(DISTINCT Patient, Value) AS Result
FROM patient_test 
GROUP BY Test;

See the demo.

儭儭莪哋寶赑 2025-02-11 11:00:02

似乎您做了什么。我会做一些非常相似的事情:

SELECT 
    T.Test,
    COUNT(*) AS Result 
FROM 
    (
        SELECT DISTINCT 
            Patient,
            Test,
            Value
        FROM 
            patient_test
        ) T
GROUP BY
    T.Test
;

如果您的表中有重复项,则必须使用分组或删除它们 - 在这种情况下,这几乎是标准的操作程序。

Seems fine what you have done. I would do something very similar:

SELECT 
    T.Test,
    COUNT(*) AS Result 
FROM 
    (
        SELECT DISTINCT 
            Patient,
            Test,
            Value
        FROM 
            patient_test
        ) T
GROUP BY
    T.Test
;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文