SELECT DISTINCT without null value
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
SELECT DISTINCT without null value
How to select distinct without null value in filed?
-
- Administrator
- Posts: 78438
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SELECT DISTINCT without null value
SELECT DISTINCT FieldName
FROM MyTable
WHERE FieldName Is Not Null
FROM MyTable
WHERE FieldName Is Not Null
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: SELECT DISTINCT without null value
OK WORK!HansV wrote:SELECT DISTINCT FieldName
FROM MyTable
WHERE FieldName Is Not Null
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
-
- Administrator
- Posts: 78438
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: SELECT DISTINCT without null value
Wath you think about?HansV wrote:Yes, that should work.
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
-
- Administrator
- Posts: 78438
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SELECT DISTINCT without null value
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"
"SELECT RAPPORTO, Count(*) AS TOT_RAPP FROM [" & DT & "] WHERE RAPPORTO IS NOT NULL AND NOMINATIVO IS NULL GROUP BY RAPPORTO"
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: SELECT DISTINCT without null value
ok, tk.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"
ops! but the count is 1!
I test with:
Code: Select all
Y=rs.Fields(1).Value
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: SELECT DISTINCT without null value
Hans sorry but edited my last reply...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"
-
- Administrator
- Posts: 78438
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SELECT DISTINCT without null value
Do you loop through the recordset and get 1 for ALL records?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: SELECT DISTINCT without null value
no... thsi si my test for count and have Y=1:HansV wrote:Do you loop through the recordset and get 1 for ALL records?
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
....
note:
Y is dimensioned as Long
-
- Administrator
- Posts: 78438
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SELECT DISTINCT without null value
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
Hans