Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Portable way to express COUNT(DISTINCT list) ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-03, 09:31
rydenius rydenius is offline
Registered User
 
Join Date: May 2003
Location: Stockholm, Sweden
Posts: 15
Portable way to express COUNT(DISTINCT list) ?

Not all databases support COUNT(DISTINCT list) where 'list' is a comma separated list of more than one columns. For example DB2 and PGSQL does not (at least in the versions I've seen). Does anybody know a standard way to write a portable version of that expression, that works with most db's?

What I want to do:

SELECT COUNT(DISTINCT a, b) FROM c GROUP BY d;

...but without using multiple column arguments to COUNT DISTINCT.

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 05-02-03, 09:45
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Portable way to express COUNT(DISTINCT list) ?

Quote:
Originally posted by rydenius
Not all databases support COUNT(DISTINCT list) where 'list' is a comma separated list of more than one columns. For example DB2 and PGSQL does not (at least in the versions I've seen). Does anybody know a standard way to write a portable version of that expression, that works with most db's?

What I want to do:

SELECT COUNT(DISTINCT a, b) FROM c GROUP BY d;

...but without using multiple column arguments to COUNT DISTINCT.

Any ideas?

Oracle doesn't support that either. What one typically does, which I imagine would work on other DBMSs (but can't be sure) is:

SELECT COUNT(DISTINCT a||b) FROM c GROUP BY d;

If there are numbers and/or dates, these need to be converted to character first, which is where I imagine you will become DBMS-dependent again with date format masks etc.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 05-02-03, 10:04
rydenius rydenius is offline
Registered User
 
Join Date: May 2003
Location: Stockholm, Sweden
Posts: 15
Thanks a lot! But then I have another problem instead:

COUNT(DISTINCT ...) should exclude rows with NULL values in column a or b caused by for example OUTER JOINS. Using the techique with '||' the NULL rows will be counted.

In my case the columns a and b are both integers, so I tried something like

a || '.' || b

Do you have a smart solution for that too?
Reply With Quote
  #4 (permalink)  
Old 05-02-03, 10:14
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Quote:
Originally posted by rydenius
Thanks a lot! But then I have another problem instead:

COUNT(DISTINCT ...) should exclude rows with NULL values in column a or b caused by for example OUTER JOINS. Using the techique with '||' the NULL rows will be counted.

In my case the columns a and b are both integers, so I tried something like

a || '.' || b

Do you have a smart solution for that too?

Why not just add a WHERE clause:

WHERE a IS NOT NULL
AND b IS NOT NULL
...
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 05-02-03, 10:23
rydenius rydenius is offline
Registered User
 
Join Date: May 2003
Location: Stockholm, Sweden
Posts: 15
Sure. Sometimes one just gets blind...

Thanks!
Reply With Quote
  #6 (permalink)  
Old 05-06-03, 12:31
lrobin3 lrobin3 is offline
Registered User
 
Join Date: May 2003
Posts: 5
what about something like this:

select count(*)
from ( select a, b, count(*) from Table group by a, b )

Not as pretty, but with a little massaging, that should work in most DBMS implementations....either as a dynamic view or as a correlated sub-query in the select line.

Granted, efficiency is a little lacking.......

Layne
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On