2013-03-15 102 views
1

我有顯示重複的結果,我不想要。我們有一個列調用addresstypes,它返回一個B或L,這取決於在db中輸入的內容。如果選擇B,則輸入數據是不正確的,因爲這是交付地址和法定地址。不顯示重複

當拉取數據,我得到的序列號等,但我得到一些兩倍......這有兩個B的地址數據& L.

這裏是我的查詢的人 - 我怎樣才能使雙列不顯示?

USE inventory 
SELECT distinct 
dbo.addressinfo.locationinfoid, dbo.equipmentlocationscurrent.serialnum, dbo.addressinfo.addresstype 
FROM dbo.equipmentlocationscurrent 
full join dbo.addressinfo 
on dbo.equipmentlocationscurrent.locationinfoid = dbo.addressinfo.locationinfoid 
where (clientName = 'cps lease') 
and (locationtype = 'merchant') 
and (addresstype = 'b' or addresstype = 'l') 
order by serialnum 

樣品超過包含在選擇列表中的任何列結果

locationinfoid serialnum 
2887540  301-252-800 B 
2887540  301-252-800 L 
+0

如果你想獲得不同的'locationinfoid'然後,哪'serialnum'和'addresstype'你想從重複的行顯示每個'locationinfoid'? – 2013-03-15 15:07:16

+0

他們看起來不像重複。糾正我,如果我錯了 – matcheek 2013-03-15 15:07:42

+0

它並不重要,這個報告,只是不是兩個 – jlongpre 2013-03-15 15:07:59

回答

0

鮮明的回報任何不同的記錄。所以,如果你有不同的價值觀爲addressinfo.locationinfoidserialnumaddressinfo.addresstype,那麼你會得到重複記錄

在這種情況下,它看起來像你在兩個不同的addresstypes拉着所以你會得到2如果「重複」記錄locationinfoidserialnum對有其中之一。包含返回值的編輯確認了這一點。

如果您不在乎addresstype那麼請不要將它包含在選擇列表中。嘗試:

USE inventory 
SELECT distinct 
dbo.addressinfo.locationinfoid, dbo.equipmentlocationscurrent.serialnum 
FROM dbo.equipmentlocationscurrent 
full join dbo.addressinfo 
on dbo.equipmentlocationscurrent.locationinfoid = dbo.addressinfo.locationinfoid 
where (clientName = 'cps lease') 
and (locationtype = 'merchant') 
and (addresstype = 'b' or addresstype = 'l') 
order by serialnum 
+0

哦,我認爲這將工作......謝謝! – jlongpre 2013-03-15 15:30:42

1

根據您的評論,因爲它並不重要的addresstype值你想選擇每個locationinfoid,使用GROUP BY locationinfoid, serialnumMAX

SELECT 
    a.locationinfoid, 
    e.serialnum, 
    MAX(a.addresstype) 
FROM dbo.equipmentlocationscurrent AS e 
full join dbo.addressinfo AS a on e.locationinfoid = a.locationinfoid 
where clientName = 'cps lease' 
and locationtype = 'merchant' 
and addresstype = 'b' or addresstype = 'l' 
GROUP BY a.locationinfoid, e.serialnum 
order by serialnum; 

這會給你不同的值爲locationinfoid

0

你有很多選擇,

  • 從結果中排除的地址類型,並使用不同的
  • 按功能
  • 使用聚合功能可按和組做一個交集聯盟的clausules的OFR知道重複

如果您不想獲取地址類型並想知道重複項,我會以這種方式重寫查詢:

SELECT  A.locationinfoid, E.serialnum 
FROM dbo.equipmentlocationscurrent E 
inner join dbo.addressinfo A 
on E.locationinfoid = A.locationinfoid 
where (clientName = 'cps lease') 
and (locationtype = 'merchant') 
and (addresstype = 'b') 
order by serialnum 

INTERSECT 

SELECT  A.locationinfoid, E.serialnum 
FROM dbo.equipmentlocationscurrent E 
inner join dbo.addressinfo A 
on E.locationinfoid = A.locationinfoid 
where (clientName = 'cps lease') 
and (locationtype = 'merchant') 
and (addresstype = 'L') 
order by serialnum 

如果你只是想獲取他們都使用不同的

SELECT DISTINCT A.locationinfoid, E.serialnum 
    FROM dbo.equipmentlocationscurrent E 
    FULL join dbo.addressinfo A 
    on E.locationinfoid = A.locationinfoid 
    where (clientName = 'cps lease') 
    and (locationtype = 'merchant') 
    and (addresstype IN('b', 'l')) 
    order by serialnum 

您也可以使用像MAX,或MIN聚合函數您clausule,這種方式不包括在您選擇的地址類型你不需要用你的獨特clausule

SELECT A.locationinfoid, E.serialnum, MAX(A.addresstype) AS addresstype 
     FROM dbo.equipmentlocationscurrent E 
     FULL join dbo.addressinfo A 
     on E.locationinfoid = A.locationinfoid 
     where (clientName = 'cps lease') 
     and (locationtype = 'merchant') 
     and (addresstype IN('b', 'l')) 
     Group by A.locationinfoid, E.serialnum 
     order by serialnum