选择具有ID的行,其他列中的值不同SQL Access

时间:2017-02-07 08:12:50

标签: sql ms-access

我有这个:

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 ))

4 个答案:

答案 0 :(得分:2)

你应该使用不同的

 select distinct ID, ID2
 from your table  

答案 1 :(得分:1)

它是一个基本的GROUP BY

<强>查询

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

答案 2 :(得分:0)

首先,为什么需要像这样的表结构?也许你应该首先重新考虑你的数据库设计。

然后,你可能会这样做:

SELECT DISTINCT ID, ID2
FROM table_name;

答案 3 :(得分:0)

要从ao_percentage的第一行获取m9m.bsnm.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)
...