Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Access select distinct 問題

我做了一個查詢,欄位有"病歷號","重複筆數",共計216筆, SQL語法如下 :

SELECT Count(OPD.病歷號) AS 重複筆數, OPD.病歷號

FROM OPD

GROUP BY OPD.病歷號

HAVING (((Count(OPD.病歷號))>1));

但我現在想在這個查詢新增一個欄位"門診日期"(原本資料表就有此欄),查出來卻變成246筆,SQL語法如下:

SELECT Count(OPD.病歷號) AS 重複筆數, OPD.病歷號, OPD.門診日期

FROM OPD

GROUP BY OPD.病歷號, OPD.門診日期

HAVING (((Count(OPD.病歷號))>1));

不知錯在那?

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    你第一個SQL中只有用 OPD.病歷號做Group

    所以相同OPD.病歷號會Group在一起

    而第二個多了個OPD.門診日期,所以必須是相同的OPD.病歷號&OPD.門診日期才會在一起

    舉例來說

    病歷號 日期

    ================

    001 2009/01/01

    001 2009/01/02

    =====================

    用第一個敘述會查出一筆

    而用第二個敘述就會變兩筆

Still have questions? Get your answers by asking now.