如何使用sql

时间:2018-03-12 15:14:57

标签: sql group-by distinct

我得到了一些测试数据表。其中主要由“测试标准”和“序列号”组成。 “序列号”代表特定测试,测试具有多个测试标准。由于失败,测试可能会运行多次,所以假设有15个不同的测试标准可以运行,但最后,如果我按“序列号”计算(测试标准)组,我可以获得超过15个。

我要做的是找到每个运行的测试条件的编号。

例如,如果我们有测试A和测试B.测试A有标准1,2运行,测试B有1,3运行。由于某种原因,标准1可能会被测试6次。 2,3只进行一次测试。

最后,我希望能得到这样一张桌子:

Test criteria run time
   1           2
   2           1
   3           1

我尝试使用count(distinct "") group by。但是,它只能给我数字,但我想要的价值。

有什么想法吗?

我非常感谢你的帮助。

(这是一些示例数据)

Click here to see the original data

here is the table I want

(这是我想要的表格)

1 个答案:

答案 0 :(得分:1)

您可以按序列号和测试标准进行分组。然后,您可以显示每个计数的测试标准。

select
    serialnumber,
    test_criteria,
    count(test_criteria) as RunTime
from table
group by
    serialnumber,
    test_criteria

如果您只想要运行每个测试的次数和测试名称,则可以从组中删除序列号。

select
    test_criteria,
    count(test_criteria) as RunTime
from table
group by
    test_criteria