显示sql数据库中的一个唯一行

时间:2014-02-12 06:05:31

标签: sql unique

我想从表中显示一个具有相同值的重复行数的唯一行。我试过这个SQL查询,

select  
    table1.activityhp, table2.id, table1.sequenceno 
from 
    ServiceDataElementsNew as table1, 
    VerticalsumNew as table2 
where 
    table1.verticalsumid = table2.id

得到这个结果:

    activityhp                      id      sequenceno
    Total new and repeat acceptors  1   5
    Total new and repeat acceptors  1   5
    Total new and repeat acceptors  1   5
    Total new and repeat acceptors  1   5
    Total new and repeat acceptors  1   5
    Total new and repeat acceptors  1   5
    New acceptors                   2       6
    New acceptors                   2   6
    New acceptors                   2   6
    Repeat acceptors            3   10
    Repeat acceptors            3   10
    Repeat acceptors            3   10

但我想要这个:

    activityhp                      id       sequenceno
    Total new and repeat acceptors  1   5
    New acceptors                   2       6
    Repeat acceptors            3   10

感谢您的帮助!

6 个答案:

答案 0 :(得分:0)

请尝试使用DISTINCT关键字,以消除重复的条目。

select  DISTINCT
    table1.activityhp, 
    table2.id,
    table1.sequenceno 
from 
    EthioHIMS_ServiceDataElementsNew as table1,
        EthioHIMS_VerticalsumNew as table2 
where table1.verticalsumid=table2.id

参考:Eliminating Duplicates with DISTINCT (MSDN)

答案 1 :(得分:0)

您可以使用不同,如下所述:

select distinct 
    table1.activityhp, 
    table2.id,
    table1.sequenceno 
from
    EthioHIMS_ServiceDataElementsNew as table1 ,
    EthioHIMS_VerticalsumNew as table2 
where 
    table1.verticalsumid=table2.id

答案 2 :(得分:0)

只需使用DISTINCT功能即可获得所需的输出。

答案 3 :(得分:0)

select Distinct 
    table1.activityhp, 
    table2.id,
    table1.sequenceno 
from
    EthioHIMS_ServiceDataElementsNew as table1 ,
    EthioHIMS_VerticalsumNew as table2 
where 
    table1.verticalsumid=table2.id

或者

select 
        table1.activityhp, 
        table2.id,
        table1.sequenceno,count(*) 
    from
        EthioHIMS_ServiceDataElementsNew as table1 ,
        EthioHIMS_VerticalsumNew as table2 
    where 
        table1.verticalsumid=table2.id
        group by table1.activityhp, 
                 table2.id,
                 table1.sequenceno
        having count(*)=1 

答案 4 :(得分:0)

使用distinct和inner join。

select
    distinct
    table1.activityhp, 
    table2.id,
    table1.sequenceno 
from
    EthioHIMS_ServiceDataElementsNew as table1
inner join EthioHIMS_VerticalsumNew as table2 
    on table1.verticalsumid=table2.id

答案 5 :(得分:0)

   **add to query** 

        group by table1.activityhp , table2.id ,table1.sequenceno