VFP 9: SQL: count()

This is a discussion on VFP 9: SQL: count() within the xbase forums in Other Databases category; How do I, in one query, get the count of the number of distinct values for a particular column? For example: clcode other ------ ----- ABC 12 ABC 34 DEF 17 DEF 21 DEF 58 GHI 29 The result that I want is the number of different clcode values in the above: 3. I thought I had done this before with but one query, but can not figure it out now. Have I been bitten by a bitrot bug or something? Sincerely, Gene Wirchenko Computerese Irregular Verb Conjugation: I have preferences. You have biases. He/She has prejudices....

Go Back   Database Forum > Other Databases > xbase

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-30-2008, 06:41 PM
Default VFP 9: SQL: count()

How do I, in one query, get the count of the number of distinct
values for a particular column? For example:
clcode other
------ -----
ABC 12
ABC 34
DEF 17
DEF 21
DEF 58
GHI 29

The result that I want is the number of different clcode values in
the above: 3.

I thought I had done this before with but one query, but can not
figure it out now.

Have I been bitten by a bitrot bug or something?

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Reply With Quote
  #2  
Old 07-30-2008, 07:07 PM
Default Re: VFP 9: SQL: count()

Select clcode Group by clcode
?_tally

Dan

Gene Wirchenko wrote:
> How do I, in one query, get the count of the number of distinct
> values for a particular column? For example:
> clcode other
> ------ -----
> ABC 12
> ABC 34
> DEF 17
> DEF 21
> DEF 58
> GHI 29
>
> The result that I want is the number of different clcode values in
> the above: 3.
>
> I thought I had done this before with but one query, but can not
> figure it out now.
>
> Have I been bitten by a bitrot bug or something?
>
> Sincerely,
>
> Gene Wirchenko
>
> Computerese Irregular Verb Conjugation:
> I have preferences.
> You have biases.
> He/She has prejudices.



Reply With Quote
  #3  
Old 07-30-2008, 08:59 PM
Default Re: VFP 9: SQL: count()

"Dan Freeman" wrote:

>Select clcode Group by clcode
>?_tally


Blindsided again. I use _tally quite often, and I did not even
think of doing this.

Thank you.

[snip]

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Reply With Quote
  #4  
Old 07-30-2008, 09:07 PM
Default Re: VFP 9: SQL: count()

On Wed, 30 Jul 2008 16:59:17 -0700, Gene Wirchenko wrote:

>"Dan Freeman" wrote:
>
>>Select clcode Group by clcode
>>?_tally

>
> Blindsided again. I use _tally quite often, and I did not even
>think of doing this.
>
> Thank you.


Don't forget to make sure you set _tally to 0 before the select call so you
don't get bitten by the sometimes bug.

>
>[snip]
>
>Sincerely,
>
>Gene Wirchenko
>
>Computerese Irregular Verb Conjugation:
> I have preferences.
> You have biases.
> He/She has prejudices.

My real address is jazzncats at rogers dot com
Reply With Quote
  #5  
Old 07-31-2008, 06:25 AM
Default Re: VFP 9: SQL: count()

Hi Gene

> How do I, in one query, get the count of the number of distinct
> values for a particular column? For example:
> clcode other
> ------ -----
> ABC 12
> ABC 34
> DEF 17
> DEF 21
> DEF 58
> GHI 29
>
> The result that I want is the number of different clcode values in
> the above: 3.
>
> I thought I had done this before with but one query, but can not
> figure it out now.


SELECT count(*) FROM (SELECT clcode FROM yourTable GROUP BY clCode)

or

SELECT Count(DISTINCT clcode) FROM yourTable


Regards
Bernhard Sander
Reply With Quote
  #6  
Old 07-31-2008, 12:49 PM
Default Re: VFP 9: SQL: count()

Gene Wirchenko wrote:
> "Dan Freeman" wrote:
>
>> Select clcode Group by clcode
>> ?_tally

>
> Blindsided again. I use _tally quite often, and I did not even
> think of doing this.
>
> Thank you.


You're welcome. As Bernhard posts, there are other ways.

One of the great things about VFP is there are always 3 ways to do any given
thing. One of the worst things about VFP is there are always 3 ways to do
any given thing.

Dan


Reply With Quote
  #7  
Old 07-31-2008, 07:56 PM
Default Re: VFP 9: SQL: count()

Bernhard Sander wrote:

>> How do I, in one query, get the count of the number of distinct
>> values for a particular column? For example:
>> clcode other
>> ------ -----
>> ABC 12
>> ABC 34
>> DEF 17
>> DEF 21
>> DEF 58
>> GHI 29
>>
>> The result that I want is the number of different clcode values in
>> the above: 3.
>>
>> I thought I had done this before with but one query, but can not
>> figure it out now.

>
>SELECT count(*) FROM (SELECT clcode FROM yourTable GROUP BY clCode)


I tried this one before posting: Syntax error. I thought it
would work, too. Does anyone know what is wrong with it?

>or
>
>SELECT Count(DISTINCT clcode) FROM yourTable


Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Reply With Quote
  #8  
Old 08-01-2008, 04:58 AM
Default Re: VFP 9: SQL: count()


>>
>>SELECT count(*) FROM (SELECT clcode FROM yourTable GROUP BY clCode)


> I tried this one before posting: Syntax error. I thought it
> would work, too. Does anyone know what is wrong with it?


You have to add an alias for the derived table:
SELECT count(*) FROM (SELECT clcode FROM yourTable GROUP BY clCode) as somealias
--
JS
Reply With Quote
  #9  
Old 08-01-2008, 03:21 PM
Default Re: VFP 9: SQL: count()

i79947-at-fake.uwasa.fi.invalid wrote:

>>>SELECT count(*) FROM (SELECT clcode FROM yourTable GROUP BY clCode)

>
>> I tried this one before posting: Syntax error. I thought it
>> would work, too. Does anyone know what is wrong with it?

>
>You have to add an alias for the derived table:
>SELECT count(*) FROM (SELECT clcode FROM yourTable GROUP BY clCode) as somealias


Thank you. That works.

(It is not very intuitive though. Giving a name that one does
not use?)

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Reply With Quote
  #10  
Old 08-04-2008, 06:09 AM
Default Re: VFP 9: SQL: count()

Hi Gene,

> (It is not very intuitive though. Giving a name that one does
> not use?)

That's true, that's also the reason, why I did not add this silly alias. But I
also did not test that line 8-( . I think, there are a lot of things in this SQL
that I don't find intuitive...
I only tested the other command (... count(DISTINCT *) ... ), since I could not
find this combination in the help file but knew it from other SQL dialects...

Regards
Bernhard Sander

Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 10:06 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.