|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Query helpI need a query help. How to display the record(only record) that has maximum occurrence of a particular value in a column? Say, I have a Table called Table1 with columns Col1, Col2, Col3 and Col4 and Col5. I want to display the record details that occurs maximum number of times, when I run a query. Assuming all the records will be displayed by the selection criteria. But how would I limit only the first record that has the same col3 value. (That is 30 in this case.) SELECT top 1 col1, col2, col3, col4 FROM Table1 WHERE (col1 like 'Matching%') group by col1,col2,col3, col4 order by COl1 Insert into Table1(col1, col2,col3, col4, col5)values (1,2,30,4,'MatchingValue') Insert into Table1(col1, col2,col3, col4, col5)values (2,22,30,40,'MatchingValue') Insert into Table1(col1, col2,col3, col4, col5)values (3,12,13,41,'MatchingValue') Insert into Table1(col1, col2,col3, col4, col5)values (2,22,23,44,'MatchingValue') TIA. if you have a table like htis
tbl class id grade math 1 90 math 2 80 scc 1 89 scc 2 90 select class, ID, max(grade) from tbl group by class,id will give you this math 1 90 scc 2 90 Ami wrote: Show quote > Hi All > I need a query help. > > How to display the record(only record) that has maximum occurrence of > a particular > value in a column? > Say, I have a Table called Table1 with columns Col1, Col2, Col3 and > Col4 and Col5. I want to display the record details that occurs maximum > number > of times, when I run a query. Assuming all the records will be > displayed > by the selection criteria. But how would I limit only the first record > that has the same col3 value. (That is 30 in this case.) > > SELECT top 1 col1, col2, col3, col4 FROM Table1 > WHERE (col1 like 'Matching%') > group by col1,col2,col3, col4 order by COl1 > > Insert into Table1(col1, col2,col3, col4, col5)values > (1,2,30,4,'MatchingValue') > Insert into Table1(col1, col2,col3, col4, col5)values > (2,22,30,40,'MatchingValue') > Insert into Table1(col1, col2,col3, col4, col5)values > (3,12,13,41,'MatchingValue') > Insert into Table1(col1, col2,col3, col4, col5)values > (2,22,23,44,'MatchingValue') > > TIA. Thank you, but I am not interested in finding the max value, but
interested in the maximum occurrence of the value. Assuming 90 being a value, this occurs twice. Sorry for the confusion. Thank you. SQL Ken wrote: Show quote > if you have a table like htis > tbl > class id grade > math 1 90 > math 2 80 > scc 1 89 > scc 2 90 > > select class, ID, max(grade) > from tbl > group by class,id > > will give you this > math 1 90 > scc 2 90 > > > > > > > Ami wrote: > > Hi All > > I need a query help. > > > > How to display the record(only record) that has maximum occurrence of > > a particular > > value in a column? > > Say, I have a Table called Table1 with columns Col1, Col2, Col3 and > > Col4 and Col5. I want to display the record details that occurs maximum > > number > > of times, when I run a query. Assuming all the records will be > > displayed > > by the selection criteria. But how would I limit only the first record > > that has the same col3 value. (That is 30 in this case.) > > > > SELECT top 1 col1, col2, col3, col4 FROM Table1 > > WHERE (col1 like 'Matching%') > > group by col1,col2,col3, col4 order by COl1 > > > > Insert into Table1(col1, col2,col3, col4, col5)values > > (1,2,30,4,'MatchingValue') > > Insert into Table1(col1, col2,col3, col4, col5)values > > (2,22,30,40,'MatchingValue') > > Insert into Table1(col1, col2,col3, col4, col5)values > > (3,12,13,41,'MatchingValue') > > Insert into Table1(col1, col2,col3, col4, col5)values > > (2,22,23,44,'MatchingValue') > > > > TIA. Ami wrote:
> Thank you, but I am not interested in finding the max value, but I think you would need to normalize your data first (i.e. put your> interested in the maximum occurrence of the value. Assuming > 90 being a value, this occurs twice. > Sorry for the confusion. columns into rows in a temp table or something), then the query to identify the most frequently occurring values should be easier. Chris Yes, I am interested in knowing that. Could someone help. Thank you.
Chris Lim wrote: Show quote > Ami wrote: > > Thank you, but I am not interested in finding the max value, but > > interested in the maximum occurrence of the value. Assuming > > 90 being a value, this occurs twice. > > Sorry for the confusion. > > I think you would need to normalize your data first (i.e. put your > columns into rows in a temp table or something), then the query to > identify the most frequently occurring values should be easier. > > Chris Ami wrote:
> Yes, I am interested in knowing that. Could someone help. Thank you. Please provide an expected result based on your sample data. I'm stillnot sure exactly what your requirements are. Sorry for the confusion. This is the result I expect, as there were
two records with same Col3 values. In the given resultset(4 records), there are 2 records with the same values for Col3. So I want the top record among the two. Thank you. 1,2,30,4,'MatchingValue' Chris Lim wrote: Show quote > Ami wrote: > > Yes, I am interested in knowing that. Could someone help. Thank you. > > Please provide an expected result based on your sample data. I'm still > not sure exactly what your requirements are. Ami wrote:
> Sorry for the confusion. This is the result I expect, as there were Define what you mean by 'top record'. You have no key on the table, and> two records with same Col3 values. In the given resultset(4 records), > there are 2 records with the same values for Col3. So I want the top > record among the two. Thank you. > 1,2,30,4,'MatchingValue' as you probably know, there is no physical ordering within a table. Also, what happens if there are multiple values that occur the same number of times (e.g.if '4' also occurred twice in your data)? Chris Ami wrote:
> Sorry for the confusion. This is the result I expect, as there were Also, are you only looking at Col3? What are the other columns for then?> two records with same Col3 values. In the given resultset(4 records), > there are 2 records with the same values for Col3. So I want the top > record among the two. Thank you. > 1,2,30,4,'MatchingValue'
Other interesting topics
|
|||||||||||||||||||||||