2014-02-12 35 views
0

我想顯示一個表中有一個唯一的行,具有相同的值重複行數。我曾嘗試這個sql查詢,從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 

感謝您的幫助!

+0

[不良習慣踢:使用舊樣式的JOIN(http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick- using-old-style-joins.aspx) - 舊式*逗號分隔的表*樣式列表已停用ANSI-** 92 ** SQL標準(超過** 20年前**) –

回答

0
**add to query** 

     group by table1.activityhp , table2.id ,table1.sequenceno 
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)

0

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

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

只需使用DISTINCT功能,您將獲得所需的輸出。

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 
+0

I已經嘗試過,但我得到不同的結果。 – afom

+0

您正在使用什麼RDBMS? sql server?mysql?oracle? –

+0

嘗試編輯一個! –

0

使用不同和內部連接。

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