If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > count distinct

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-04, 21:06
winterain winterain is offline
Registered User
 
Join Date: Oct 2003
Posts: 22
Unhappy count distinct

Hi... hereby i hv a problem

At my database, I hv a field "component_detail_key",
and the data is :
T002811_1
T002811_2
T002812_1
T002812_2
T002813_1
T002813_2

I get the data before _ :
T002811
T002811
T002812
T002812
T002813
T002813

Now I hv to count distinct, the output should be 3.
I use 2 for loop in this function but can't do that.
Below is my coding :

int distinct = 0;

for (int i = 1; i < a; i++){
for (int j = 1; j < a; j++)
{
if (swkey[i]==swkey[j])
{
distinct = distinct;
} else {
distinct ++;
}
} // End for
}

The answer is :5 , 10 , 15 , 20 , 25 , 30

Anyone can help???pls~
Reply With Quote
  #2 (permalink)  
Old 02-20-04, 08:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: count distinct

Why don't you just do this in SQL something like this:

SELECT COUNT( DISTINCT( SUBSTR( code, 1, INSTR(code,'_')-1 )))
FROM table
...;

I'm using Oracle SUBSTR and INSTR functions, but most DBMSs have something similar.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 02-20-04, 21:37
winterain winterain is offline
Registered User
 
Join Date: Oct 2003
Posts: 22
Smile

May I know what is the meaning for :INSTR(code,'_')-1?
The "code" is field or...?

Thx~
Reply With Quote
  #4 (permalink)  
Old 02-21-04, 05:35
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
INSTR(string1, string2) returns the position of string2 within string1. Therefore, SUBSTR(string1, 1, INSTR(string1, string2)-1) will return a new string containing all the chacracters of string1, prior to the occurence of string2.

Example,

String1('123456');
String2('4');

SUBSTR(string1, 1, INSTR(string1, string2)-1) = "123"
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #5 (permalink)  
Old 02-23-04, 22:36
winterain winterain is offline
Registered User
 
Join Date: Oct 2003
Posts: 22
Smile

oh i c~
Thanks very much!!!
Reply With Quote
  #6 (permalink)  
Old 02-23-04, 23:19
winterain winterain is offline
Registered User
 
Join Date: Oct 2003
Posts: 22
BUt still hv error : invalid command for INSTR...
I am using jsp... is it can't support this code?
Reply With Quote
  #7 (permalink)  
Old 02-23-04, 23:30
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
I would recommend that the DBMS perform the complete operation.

Java:
IndexOf(string)
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 02-23-04 at 23:32.
Reply With Quote
  #8 (permalink)  
Old 02-24-04, 00:47
winterain winterain is offline
Registered User
 
Join Date: Oct 2003
Posts: 22
sorry not understand this......
I am fresh with Java

is it
String sqlsw = "SELECT SUBSTRING_INDEX(component_detail_key, '_', 1) AS sw FROM component_detail WHERE component_key=?";
?
but still can't....

Last edited by winterain; 02-24-04 at 01:04.
Reply With Quote
  #9 (permalink)  
Old 02-25-04, 03:36
winterain winterain is offline
Registered User
 
Join Date: Oct 2003
Posts: 22
I solve the problem by CHARINDEX,
thx very much for your help
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

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