我有這樣的:選擇一行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))
'選擇不同的ID,ID2從tablename'。 – jarlh
您的查詢提到了3列,而示例數據只有2列。顯示真實的表格結構。 – Serg
真正的表結構有6列,但檢查是關於列bsn和列ao_percentage,其他列僅供參考。 – user7219039