Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
In the below table,

    DECLARE @t1 TABLE(id INT,NAME VARCHAR(MAX))
    INSERT INTO @t1 VALUES(1,'test')
    INSERT INTO @t1 VALUES(2,'test')
    INSERT INTO @t1 VALUES(3,'best')
    INSERT INTO @t1 VALUES(4,'rest')
    INSERT INTO @t1 VALUES(5,'rest')

how can I select the records that have equals values coming adjacently? That is I am running an SQL job. It should take the adjacent equal value  count for its processing. In the below table, first time the job is running it should take count as 2 the 2nd time job is running it should be 1. Third time it should be 2. What is the query that will fetch the cont of equal records for name column? In case no equal columns it should be 1. Please help
Posted
Comments
Joan Magnet 10-Apr-15 5:23am    
Do you mean?

select count(*) from table where id=@id_act-1 or id=@id_act+1
mayooran99 10-Apr-15 5:27am    
what is @id_act here?
Joan Magnet 10-Apr-15 5:33am    
the new id added value
mayooran99 10-Apr-15 5:38am    
so from my table, it is @t1.id right?

1 solution

Try this:
SQL
SELECT [NAME], COUNT([NAME]) AS CountOfNames
FROM @t1
GROUP BY [NAME]


Second column returns a count of name.
 
Share this answer
 
Comments
mayooran99 10-Apr-15 5:28am    
I dont want the count of all items. I need the count for only the 1st set of equal items. Because the job needs the count of top n items that are equal
Maciej Los 10-Apr-15 5:39am    
Above query does not return the count of all items. It returns the count for each group: test - 2, best - 1, rest - 2. Try!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900