2012-03-30 29 views
1

我將不勝感激一些幫助創建正確的SQL來檢索我的第三個表「表C」中匹配記錄只有一個值,如下所示。表A列出了客戶ID,名稱和狀態。表B列出了外鍵客戶ID和會員卡號。表C列出了會員卡號的銷售情況。SQL連接A,B,不同C

select A.cifno, A.cfna1, A.cfstate, B.hbmast.cifno, B.hbmast.hbcid, C.cast(ewmast.ewcid as dec(12,0)) as "eWire ID" 
from library.cfmast cfmast join library.hbmast hbmast 
on cfmast.cfcif# = hbmast.cifno left join library.ewmast ewmast 
on hbmast.hbcid = cast(ewmast.ewcid as dec (12,0)) 

的樣本數據:

table A 
**A.cifno, A.cfna1, A.cfstate** 
J00022, John, OH 
B00019, Ben, TX 


table B 
**B.hbmast.cifno, B.hbmast.hbcid** 
J00022, 5555000
B00019, 555500007878 


table C 
**ewmast.ewcid** 
5555000
5555000
5555000
555500007878 

所需的輸出是:

J00022, John, OH, J00022, 5555000, 5555000
B00019, Ben, TX, B00019, 555500007878, 555500007878 

- 不 -

J00022, John, OH, J00022, 5555000, 5555000
J00022, John, OH, J00022, 5555000, 5555000
J00022, John, OH, J00022, 5555000, 5555000
B00019, Ben, TX, B00019, 555500007878, 555500007878 
+3

你聽說過'DISTINCT'? – Kayser 2012-03-30 13:20:32

回答

3

您應該在您的選擇中使用關鍵字distinct

即,是這樣的:

select distinct .... 
+0

謝謝,我在那裏有一個日期格式聲明,使不同的總是拋出一個錯誤,刪除,所有更好。 date(digits(datfld7))as「New Date」 – macunte 2012-03-30 15:45:36

1

你可以使用DISTINCT運算符

SELECT DISTINCT * 
FROM YOURTABLE 
1

在你的左邊加入到表C,你會想要把一個「不空」的條款。這將確保只顯示匹配的行,否則將被忽略。只是說。

2

瞧...

select DISTINCT A.cifno, A.cfna1, A.cfstate, B.hbmast.cifno, B.hbmast.hbcid,   C.cast(ewmast.ewcid as dec(12,0)) as "eWire ID" 
from library.cfmast cfmast join library.hbmast hbmast 
on cfmast.cfcif# = hbmast.cifno left join library.ewmast ewmast 
on hbmast.hbcid = cast(ewmast.ewcid as dec (12,0)) 
相關問題