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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL question - sub select against same table

Status
Not open for further replies.

gamerland

Programmer
Apr 6, 2003
53
US
hello,
I have a set of data that should be simple to answer but I just cannot get it right.

DATATABLE

fld1 fld2
1 1
1 1
1 1
2 A
2 A
3 1
3 F

4 A
4 A

I am trying to figure out how many times fld2 is different within fld1. In this case 1 time for key 3.

As my attempts are either in error or produce useless results, I don't want to give anyone a bad start.
Thanks in advance.
 
Hi Gamer,
I'm not sure I understand you post. You say that you are trying to figure out how many times fld2 is different within fld2, but then say that for key 3, it is 1 time. Is this a typo, because it looks to me that there are 2 values for key 3 (1 and F).
If I understand you correctly, then I think the following will work:
SELECT FLD1, COUNT(DISTINCT FLD2)
FROM DATATABLE
GROUP BY FLD1

Marc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top