Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count distinct 3

Status
Not open for further replies.

Thorsten3

Programmer
Jan 28, 2001
22
JP
This query gives me a list of 1085 records:

select distinct clone_id, stage from t_expression

but I don't need the list, I only need to count them
How can I do this?

This command works, but it is not what I want:
select count (distinct clone_id) from t_expression

These commands do not work:
select count (distinct clone_id, stage) from t_expression
select count distinct clone_id, stage from t_expression
select distinct count (clone_id, stage) from t_expression

Does anybody know the proper command??
I'm using DB2
 
As far as I'm aware, the command which you say works is correct. I tried it on DB2 V7.2 and it worked.

select count (distinct clone_id) from t_expression

Why don't you wish to use this?
 
Does the following help?

select count(*) from t_expression
group by clone_id,stage
 
Sorry it was not clear enough.

select count (distinct clone_id) from t_expression

...is not what I'm interested in. I would like to know how many records there are, where the combination of clone_id and stage are distinct. The command above doesn't contain the stage. But if I include the stage:

select count (distinct clone_id, stage) from t_expression

...the command does not work any more, allthough this command:

select distinct clone_id, stage from t_expression

...works well!
 
Does the following help?
select count(*) from t_expression group by clone_id,stage

basically Yes, but like this:
select distinct clone_id, stage from t_expression

...it gives me a list. As I execute it from a java program, I don't want to run through the loops of the result set and count the loops, but would like to get the result (number of rows) directly with the command.


 
Wrap a query that returns all distinct records of stage and clone_id in a 'select count(*) from ( )' statement:

SELECT count(*)
FROM (
select distinct clone_id, stage
from t_expression
)

This will return a single number that is the number of distinct clone_id, stage rows.
 
I like crufty's suggestion.

SQL Server requires adding an alias name to the pseudo table. Perhaps DB2 is the same... I dunno.


SELECT count(*)
FROM (
select distinct clone_id, stage
from t_expression
) as q
 
Thanks crufty and bperry this worked

SELECT count(*) FROM (
select distinct clone_id, stage
from t_expression) as q

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top