如何计算同一列中的两个不同的列值?

时间:2019-02-06 19:39:46

标签: sql-server

我想查看一个值在下一个新值出现之前重复的次数。

这是我目前所看到的:

---------------------------------------------------------------
| timestamp              | nameID | Value   | Row# | accountID|
---------------------------------------------------------------
| 2019-02-02 00:00:13:743| 17730  | Value 1 | 1    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:14:743| 17730  | Value 2 | 1    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:15:743| 17730  | Value 2 | 2    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:16:743| 17730  | Value 2 | 3    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:17:743| 17730  | Value 1 | 2    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:18:743| 17730  | Value 2 | 4    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:19:743| 17730  | Value 2 | 5    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:20:743| 17730  | Value 2 | 6    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:21:743| 17730  | Value 1 | 3    | 82607201 |
---------------------------------------------------------------
| 2019-02-02 00:00:22:743| 17730  | Value 2 | 7    | 82607201 |
---------------------------------------------------------------

这就是我想看到的:

---------------------------------------------------------------------------
| timestamp              | nameID | Value   | Count of Value 2 | accountID|
---------------------------------------------------------------------------
| 2019-02-02 00:00:13:743| 17730  | Value 1 | (3)              | 82607201 |
--------------------------------------------------------------------------
| 2019-02-02 00:00:17:743| 17730  | Value 1 | (3)              | 82607201 |
---------------------------------------------------------------------------
| 2019-02-02 00:00:22:743| 17730  | Value 1 | (1)              | 82607201 |
---------------------------------------------------------------------------

我尝试使用Row_Number()OVER分区,但是它没有提供我想要的东西。

SELECT [timestamp]
,nameID
,ROW_NUMBER() OVER(PARTITION BY Value ORDER BY timestamp ASC) AS Row#
,accountID
FROM #TEMP_Base3
ORDER BY timestamp ASC

3 个答案:

答案 0 :(得分:2)

您可以尝试下一种方法。首先,找到值更改的时间,然后查找数字组,最后选择数据。

输入:

nt
=> ('_stack_', ['data', 'inside', 'the', 'stack'])

声明:

CREATE TABLE #Table (
   [timestamp] datetime,
   nameID int,
   [Value] varchar(10),
   accountID int
)
INSERT INTO #Table
   ([timestamp], nameID, Value, accountID)
VALUES
   ('2019-02-02 00:00:13:743', 17730, 'Value 1', 82607201),
   ('2019-02-02 00:00:14:743', 17730, 'Value 2', 82607201),
   ('2019-02-02 00:00:15:743', 17730, 'Value 2', 82607201),
   ('2019-02-02 00:00:16:743', 17730, 'Value 2', 82607201),
   ('2019-02-02 00:00:17:743', 17730, 'Value 1', 82607201),
   ('2019-02-02 00:00:18:743', 17730, 'Value 2', 82607201),
   ('2019-02-02 00:00:19:743', 17730, 'Value 2', 82607201),
   ('2019-02-02 00:00:20:743', 17730, 'Value 2', 82607201),
   ('2019-02-02 00:00:21:743', 17730, 'Value 1', 82607201),
   ('2019-02-02 00:00:22:743', 17730, 'Value 2', 82607201)

输出:

;WITH ChangesCTE AS (
   SELECT 
      *,
      CASE
         WHEN [Value] = LAG([Value]) OVER (ORDER BY [timestamp]) THEN 0
         ELSE 1
      END AS ChangeMode
   FROM #Table
), GroupsCTE AS (
   SELECT 
      *,
      SUM(ChangeMode) OVER (ORDER BY [timestamp]) AS GroupID
   FROM ChangesCTE
)
SELECT 
   g.[timestamp],
   g.nameID,
   g.[Value],
   g.accountID,
   c.[Count]
FROM GroupsCTE g
LEFT JOIN (
   SELECT GroupID, COUNT(*) AS [Count] 
   FROM GroupsCTE 
   GROUP BY GroupID
) c ON g.GroupID = c.GroupId - 1

答案 1 :(得分:0)

使用分组依据

SELECT column_name(s), count(1) as tally
FROM table_name
GROUP BY column_name(s)
ORDER BY column_name(s);

答案 2 :(得分:0)

StringBuilder