2014-11-14 2 views
-1

이 SQL은 테이블 BIDdetails의 레코드 수를 반환하는 대신 테이블 Dims의 레코드 수를 반환합니다. 어떻게 해결할 수 있습니까?잘못된 테이블의 수를 반환하는 SQL

BIDReportSearch.CommandText = ("SELECT BIDdetails.Origin, BIDdetails.Destination, 
Round(Sum(Dims.ChargeableWeight)) as CWeight, count(BIDdetails.Origin) as NoOfShpt 
FROM BIDdetails LEFT JOIN DIMS ON BidDetails.BID=Dims.BID 
where BIDdetails.OrgCountry<>'AE' and BIDdetails.DestCountry='AE' and 
BIDdetails.ClosingDate>=#" & dtpBIDfrom.Value & "# and BIDdetails.ClosingDate<=#" & 
dtpBIDto.Value & "# GROUP BY BIDdetails.Origin, BIDdetails.Destination 
ORDER BY Round(Sum(Dims.ChargeableWeight)) DESC") 
+0

'BIDetails'에있는 각 행에 대해 'Dims'에 여러 행이 있기 때문에, 우리가 확신 할 수있는 세부 정보를 제공하지는 못했습니다. 하위 쿼리에서 미리 집계해야합니다. 부가 메모 : 연결은 SQL 삽입을 소개하는 좋은 방법이며 매개 변수가있는 쿼리를 사용해야합니다. –

답변

1

표현 :

count(BIDdetails.Origin) 

은 단순히 각 그룹의 BIDdetails.Origin의 NULL이 아닌 값의 수를 계산합니다. 실제로 필드별로 그룹화 했으므로 각 그룹의 행 수입니다.

count(distinct)을 고유 한 식별자로 사용하면 대부분의 데이터베이스에서 원하는 것을 얻을 수 있습니다. 아아, MS Access는 count(distinct)을 지원하지 않으므로 이러한 쿼리는 Access에서 작성하는 것이 훨씬 어렵습니다. 당신은 수행하여 단지 count 필드를 얻을 수 있습니다 :

SELECT BIDdetails.Origin, BIDdetails.Destination, count(*) as NoOfShpt 
FROM BIDdetails 
where BIDdetails.OrgCountry <> 'AE' and BIDdetails.DestCountry='AE' and 
     BIDdetails.ClosingDate>=#" & dtpBIDfrom.Value & "# and BIDdetails.ClosingDate<=#" & dtpBIDto.Value & "# 
GROUP BY BIDdetails.Origin, BIDdetails.Destination; 

그리고는 응용 프로그램에서 또는 원래 하나에 다시이 쿼리를 결합하여 두 결과를 결합.

편집 :

이 원래 쿼리입니다 :

SELECT d.Origin, d.Destination, 
     Round(Sum(Dims.ChargeableWeight)) as CWeight, count(d.Origin) as NoOfShpt 
FROM BIDdetails as d LEFT JOIN 
    DIMS 
    ON BidDetails.BID=Dims.BID 
where d.OrgCountry <> 'AE' and d.DestCountry='AE' and 
     d.ClosingDate> = #" & d.Value & "# and d.ClosingDate<=#" & dtpBIDto.Value & "# 
GROUP BY d.Origin, d.Destination 
ORDER BY Round(Sum(Dims.ChargeableWeight)) DESC 

다른 방법이있다, 당신은 다시 세부 사항에 의해 처음으로 집계합니다. 나는이 경우 쉽게 생각 :

SELECT Origin, Destination, SUM(CWeight) as CWeight, COUNT(*) as NumShip 
FROM (SELECT d.id, d.Origin, d.Destination, 
      Round(Sum(Dims.ChargeableWeight)) as CWeight, count(d.Origin) as NoOfShpt 
     FROM BIDdetails as d LEFT JOIN 
      DIMS 
      ON BidDetails.BID = Dims.BID 
     where d.OrgCountry <> 'AE' and d.DestCountry='AE' and 
      d.ClosingDate> = #" & d.Value & "# and d.ClosingDate<=#" & dtpBIDto.Value & "# 
     GROUP BY d.id, d.Origin, d.Destination 
    ) as d 
GROUP BY Origin, Destination 
ORDER BY Round(Sum(CWeight)) DESC; 

d.id이 고유 ID는 계산하려는 것에 대해 무엇이든을 의미합니다.

+0

안녕하세요, 고든, 신속한 회신에 감사드립니다. ** LEFT JOIN을 포함한 완전한 진술을 해주실 수 있습니까? ** 위의 내용은 저에게 효과적이지 않습니다. –

관련 문제