如何根据相邻的平等选择项目? [英] How do I select the items based on their adjacent equality?

查看:54
本文介绍了如何根据相邻的平等选择项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

试试这个:

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





第二列返回名称计数。



Second column returns a count of name.


这篇关于如何根据相邻的平等选择项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆