SELECT DISTINCT without null value

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

SELECT DISTINCT without null value

Post by sal21 »

How to select distinct without null value in filed?

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT DISTINCT without null value

Post by HansV »

SELECT DISTINCT FieldName
FROM MyTable
WHERE FieldName Is Not Null
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SELECT DISTINCT without null value

Post by sal21 »

HansV wrote:SELECT DISTINCT FieldName
FROM MyTable
WHERE FieldName Is Not Null
OK WORK!

Is possible in this combination of null to count RAPPORTO?

Code: Select all

"SELECT DISTINCT RAPPORTO FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL ORDER BY RAPPORTO", cn, adOpenDynamic, adLockOptimistic

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT DISTINCT without null value

Post by HansV »

Yes, that should work.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SELECT DISTINCT without null value

Post by sal21 »

HansV wrote:Yes, that should work.
Wath you think about?

Code: Select all

"SELECT DISTINCT RAPPORTO, count (*) as TOT_RAPP FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL ORDER BY RAPPORTO", cn, adOpenDynamic, adLockOptimistic

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT DISTINCT without null value

Post by HansV »

No, that won't work. If you want to add Count, Sum, Max or similar, you must use GROUP BY:


"SELECT RAPPORTO, Count(*) AS TOT_RAPP FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL GROUP BY RAPPORTO"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SELECT DISTINCT without null value

Post by sal21 »

HansV wrote:No, that won't work. If you want to add Count, Sum, Max or similar, you must use GROUP BY:


"SELECT RAPPORTO, Count(*) AS TOT_RAPP FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL GROUP BY RAPPORTO"
ok, tk. :thankyou: :clapping:

ops! but the count is 1!
I test with:

Code: Select all

Y=rs.Fields(1).Value
perpahs the cursor in my sql string no permit the count?

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SELECT DISTINCT without null value

Post by sal21 »

HansV wrote:No, that won't work. If you want to add Count, Sum, Max or similar, you must use GROUP BY:


"SELECT RAPPORTO, Count(*) AS TOT_RAPP FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL GROUP BY RAPPORTO"
Hans sorry but edited my last reply...

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT DISTINCT without null value

Post by HansV »

Do you loop through the recordset and get 1 for ALL records?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SELECT DISTINCT without null value

Post by sal21 »

HansV wrote:Do you loop through the recordset and get 1 for ALL records?
no... thsi si my test for count and have Y=1:

Code: Select all

....
rs.open "SELECT RAPPORTO, Count(*) AS TOT_RAPP FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL GROUP BY RAPPORTO", cn, adOpenDynamic, adLockOptimistic
Y=rs.Fields(1).Value
....
i think in this acase the record 1 is the value of Count(*) AS TOT_RAPP, or not?

note:
Y is dimensioned as Long

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT DISTINCT without null value

Post by HansV »

Your line will return the count for the first RAPPORTO in the recordset. That could be 1, while the count is >1 for other records...
Best wishes,
Hans