2017-02-07 62 views
-1

我有這樣的:選擇一行ID,在另一列SQL訪問不同的價值

ID A B C d ID2
1 | 1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 1 | 1 | 2
1 | 1 | 1 | 1 | 1 | 3
2 | 1 | 1 | 1 | 1 | 1
2 | 1 | 1 | 1 | 1 | 2
2 | 1 | 1 | 1 | 1 | 3
3 | 1 | 1 | 1 | 1 | 1
3 | 1 | 1 | 1 | 1 | 1
3 | 1 | 1 | 1 | 1 | 1
3 | 1 | 1 | 1 | 1 | 2
3 | 1 | 1 | 1 | 1 | 2
3 | 1 | 1 | 1 | 1 | 2

它需要做的是始終顯示ID的第一條記錄。如果值ID2發生變化,那也是必須顯示的。所以它應該看起來像這樣:

ID A B C D ID2
1 | 1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 1 | 1 | 2
1 | 1 | 1 | 1 | 1 | 3
2 | 1 | 1 | 1 | 1 | 1
2 | 1 | 1 | 1 | 1 | 2
2 | 1 | 1 | 1 | 1 | 3
3 | 1 | 1 | 1 | 1 | 1
3 | 1 | 1 | 1 | 1 | 2

我已經嘗試過,但是這樣做效果不好。它不會給我第一個值ID的記錄,而我仍然會得到雙值,而ID2不會更改。

SELECT 
IIf(IsNull(m.bsn),'',CStr(m.bsn)) AS BSN, 
IIf(IsNull(m.uitkering),'',CStr(m.uitkering)) AS Uitkering, 
IIf(IsNull(m.ingangsdatum),'',Format(m.ingangsdatum,'dd-mm-yyyy')) AS IngangsdatumUitkering, 
IIf(IsNull(m.eerste_ziektedag),'',Format(m.eerste_ziektedag,'dd-mm-yyyy')) AS EersteZiektedag, 
IIf(IsNull(m.ingangsdatum_w),'',Format(m.ingangsdatum_w,'dd-mm-yyyy')) AS DatumWijziging, 
IIf(IsNull(m.ao_percentage),'',CStr(m.ao_percentage)) AS PercentageUitkering 

FROM (((mutatie_historie AS m LEFT JOIN recht_soorten AS rs ON rs.recht_soort_id = m.recht_soort_id) 
          LEFT JOIN uitkering_soorten AS us ON us.uitkering_soort_id = m.uitkering_soort_id) 
          LEFT JOIN ao_klassen AS aok ON aok.ao_klasse_id = m.ao_klasse_id) 
          LEFT JOIN kenmerk_percentage_uitkering AS kpu ON kpu.kenmerk_code = m.kenmerk_code 

WHERE m.ongemax_dagloon > '0' 

AND (m.ao_percentage <> (SELECT TOP 1 m9.ao_percentage FROM mutatie_historie m9 WHERE m.bsn < m9.bsn AND m.ingangsdatum_w < m9.ingangsdatum_w)) 

AND not exists 
(SELECT null FROM mutatie_historie m2 WHERE m2.aanlevering > m.aanlevering  AND m2.ingangsdatum_w <= m.ingangsdatum_w AND m2.bsn = m.bsn AND 
    (SELECT Iif(IsNull(m3.uitkering), '', m3.uitkering) FROM mutatie_historie m3 WHERE m3.bsn = m2.bsn AND m3.aanlevering = m2.aanlevering AND m3.keynummer = 
     (SELECT min(keynummer) FROM mutatie_historie m4 WHERE m4.bsn = m3.bsn AND m4.aanlevering = m3.aanlevering AND m4.uitkering is not null)) = 
      (SELECT Iif(IsNull(m3.uitkering), '', m3.uitkering) FROM mutatie_historie m3 WHERE m3.bsn = m.bsn AND m3.aanlevering = m.aanlevering AND m3.keynummer = 
       (SELECT min(keynummer) FROM mutatie_historie m4 WHERE m4.bsn = m3.bsn AND m4.aanlevering = m3.aanlevering AND m4.uitkering is not null))) 

ORDER BY m.bsn, m.ingangsdatum_w, m.ao_percentage DESC; 

這是我的整個代碼。所有零件都是必需的,這是它的一部分:

AND (m.ao_percentage <> (SELECT TOP 1 m9.ao_percentage FROM mutatie_historie m9 WHERE m.bsn < m9.bsn AND m.ingangsdatum_w < m9.ingangsdatum_w)) 
+0

'選擇不同的ID,ID2從tablename'。 – jarlh

+0

您的查詢提到了3列,而示例數據只有2列。顯示真實的表格結構。 – Serg

+0

真正的表結構有6列,但檢查是關於列bsn和列ao_percentage,其他列僅供參考。 – user7219039

回答

1

它的一個基本的GROUP BY

查詢

SELECT ID, ID2 
FROM your_table_name 
GROUP BY ID, ID2 
ORDER BY ID, ID2; 
+0

我會說這是一個基本的'select distinct' ... – jarlh

+0

@ jarlh:是的..可以.. – Wanderer

2

爲什麼你需要一個像這樣的表結構您shuold使用不同

select distinct ID, ID2 
from your table 
0

首先,?也許你應該首先重新考慮你的數據庫設計。

然後,你可以做的是:

SELECT DISTINCT ID, ID2 
FROM table_name; 
+0

數據庫多年來一直存在,它不是我的。我只需要從中獲取一些數據。有一個數據庫(mutatie_historie),但有一個輸入會覆蓋數據。這就是下面的內容,要獲取所有當前數據,而不是所有bsn的數據。 – user7219039

0

要的m9其中exeeds m.bsn第一行獲得ao_percentagem.ingangsdatum_w

... 
(SELECT TOP 1 m9.ao_percentage FROM mutatie_historie m9 WHERE m.bsn < m9.bsn AND m.ingangsdatum_w < m9.ingangsdatum_w ORDER BY m9.bsn, m9.ingangsdatum_w) 
... 
相關問題