2017-04-03 39 views
0

我有一個SQL慢慢改變客戶的維度表,他們所有的變化。 爲e.g如何比較地址與同一個customerno在同一個表中sql

customerno   name  address    phone CrrentRecord 
    00001    abc   72 fulton drive  123    Y 
    00001    abc   72 fulton drive  321    N 
    00001    abc   69 charter dive  111    N 
    00002    xyz   62 iak grove way  111    Y 
    00002    xyz   63 ikea drive   222    N 
    00003    pqr   port capital dr  369    Y 
    00003    pqr   port capital dr1  369    N 

我想知道哪些客戶已經改變他們的地址。

我也試過鉛和滯後功能,但它並沒有幫助我。

回答

0

這樣的事情呢?

select customerno 
from YourTable 
group by customerno 
having count(distinct [address]) > 1 
0

這會爲您提供更改地址的客戶。

沒有一個理想的結果,很難說有什麼額外的信息需要

Select customerno 
From YourTable 
Group By customerno 
Having min(address)<>max(address) 
0

要檢查客戶與非當前記錄,試試這個

select customerno 
from YourTable 
group by customerno 
having count(distinct [CrrentRecord]) > 1 
+0

不知道currentrecord會做的伎倆。假設沒有69次特許潛水的記錄 –

+0

@John它仍然有效,因爲仍然有2條記錄,一條Y和另一條N.因爲它似乎是他用來識別最新記錄的標誌 – Stephen

+0

但仍然是相同的地址 –