2017-05-04 409 views
0

我是新來的sql,需要你的幫助來實現下面,我已經嘗試使用組和計數函數,但我得到了重複的唯一組中的所有行。sql查詢找到唯一的記錄

以下是我的源數據。

CDR_ID,TelephoneNo,Call_ID,call_Duration,Call_Plan 
543,xxx-23,12,12,500 
543,xxx-23,12,12,501 
543,xxx-23,12,12,510 
643,xxx-33,11,17,700 
343,xxx-33,11,17,700 
766,xxx-74,32,1,300 
766,xxx-74,32,1,300 
877,xxx-32,12,2,300 
877,xxx-32,12,2,300 
877,xxx-32,12,2,301 

請注意: - 的源有重複記錄的多種組合,所以當我做算一套獨特的不作爲出現次數= 1

例如: - 在源下面的數據有60每個組合

877,xxx-32,12,2,300 -- 60 records 
877,xxx-32,12,2,301 -- 60 records 

記錄我想獲得獨特的獨特的記錄,但重複的記錄也越來越在

下面是該行應該出現在獨特的羣體中。即對於CDR_ID,TelephoneNo,Call_ID,call_Duration的相同組合,將存在多個call_Plans。我想讀記錄其中只有對CDR_ID,TelephoneNo,CALL_ID,call_Duration的每一個獨特的組合,一個呼叫計劃,

CDR_ID,TelephoneNo,Call_ID,call_Duration,Call_Plan 
643,xxx-33,11,17,700 
343,xxx-33,11,17,700 
766,xxx-74,32,1,300 

這個請指點。

感謝和問候

+2

你能不能也請共享代碼,你已經嘗試到現在,只是給你一個提示嘗試在SQL – Joby

回答

-3
Select *, count(CDR_ID) 
from table 
group by CDR_ID, TelephoneNo, Call_ID, call_Duration, Call_Plan 
having count(CDR_ID) = 1 
0

基本上你應該試試這個:

SELECT A.CDR_ID, A.TelephoneNo, A.Call_ID, A.call_Duration, A.Call_Plan 
FROM YOUR_TABLE A 
INNER JOIN (SELECT CDR_ID,TelephoneNo,Call_ID,call_Duration 
      FROM YOUR_TABLE 
      GROUP BY CDR_ID,TelephoneNo,Call_ID,call_Duration 
      HAVING COUNT(*)=1 
      ) B ON A.CDR_ID= B.CDR_ID AND A.TelephoneNo=B.TelephoneNo AND A.Call_ID=B.Call_ID AND A.call_Duration=B.call_Duration 

您可以使用Windows功能COUNT(*)OVER做一個較短的查詢......

0


以下查詢會爲您提供結果

SELECT CDR_ID,TelephoneNo,Call_ID,call_Duration,Call_Plan, COUNT(*) 
FROM TABLE_NAME GROUP BY CDR_ID,TelephoneNo,Call_ID,call_Duration,Call_Plan 
HAVING COUNT(*) < 2; 

它也給你計數。如果不需要,您可以將其刪除。

+0

的'COUNT'功能已經改變,而不是比較小, ,大於錯誤提供 –

1

進行更復雜的分組,您也可以使用公用表表達式/派生表與窗口函數一起:

declare @t table(CDR_ID int,TelephoneNo nvarchar(20),Call_ID int,call_Duration int,Call_Plan int); 
insert into @t values (543,'xxx-23',12,12,500),(543,'xxx-23',12,12,501),(543,'xxx-23',12,12,510),(643,'xxx-33',11,17,700),(343,'xxx-33',11,17,700),(766,'xxx-74',32,1,300),(766,'xxx-74',32,1,300),(877,'xxx-32',12,2,300),(877,'xxx-32',12,2,300),(877,'xxx-32',12,2,301); 

with cte as 
(
select CDR_ID 
    ,TelephoneNo 
    ,Call_ID 
    ,call_Duration 
    ,Call_Plan 
    ,count(*) over (partition by CDR_ID,TelephoneNo,Call_ID,call_Duration) as c 
from (select distinct * from @t) a 
) 
select * 
from cte 
where c = 1; 

輸出:採用not exists()

select distinct * 
from t 
where not exists (
    select 1 
    from t as i 
    where i.cdr_id = t.cdr_id 
    and i.telephoneno = t.telephoneno 
    and i.call_id = t.call_id 
    and i.call_duration = t.call_duration 
    and i.call_plan <> t.call_plan 
) 

+--------+-------------+---------+---------------+-----------+---+ 
| CDR_ID | TelephoneNo | Call_ID | call_Duration | Call_Plan | c | 
+--------+-------------+---------+---------------+-----------+---+ 
| 343 | xxx-33  |  11 |   17 |  700 | 1 | 
| 643 | xxx-33  |  11 |   17 |  700 | 1 | 
| 766 | xxx-74  |  32 |    1 |  300 | 1 | 
+--------+-------------+---------+---------------+-----------+---+ 
+0

在問題中有兩行'cdr_id = 766',但這兩行中只有一行在您的代碼中。 – SqlZim

+0

@SqlZim這就是我在做幾次編輯之前回答問題所得到的結果...... – iamdave

+1

你說得對,我應該檢查編輯歷史。 rextester演示http:// rextester。com/TGXR99743獲取更新的答案 – SqlZim

1

rextester演示:http://rextester.com/RRNNE20636

回報:

+--------+-------------+---------+---------------+-----------+-----+ 
| cdr_id | TelephoneNo | Call_id | call_Duration | Call_Plan | cnt | 
+--------+-------------+---------+---------------+-----------+-----+ 
| 343 | xxx-33  |  11 |   17 |  700 | 1 | 
| 643 | xxx-33  |  11 |   17 |  700 | 1 | 
| 766 | xxx-74  |  32 |    1 |  300 | 1 | 
+--------+-------------+---------+---------------+-----------+-----+