SQL - 在列表中创建第1,第2,第3次的计数

时间:2014-08-27 15:54:51

标签: sql sql-server count counter

我有一个带有日期戳和ID号的数据集,如下所示:

Date_Stamp  | ID
2013-07-17  | ID1
2013-07-17  | ID1
2013-08-19  | ID1
2013-08-19  | ID2

从这些数据中,我需要填充一个包含以下字段的表:Date,ID,First_attempt,Second_Attempt,Third_Attempt,Total_Attempts。

使用该数据,文件将如下所示:

Date | ID | First_attempt | Second_Attempt | Third_Attempt | Total_Attempts
2013-07-17 | ID1 | 1 | 1 | 0 | 2
2013-08-19 | ID1 | 0 | 0 | 1 | 1
2013-08-19 | ID2 | 1 | 0 | 0 | 1

我已经能够使用此代码进行首次尝试:

Select  min(date_stamp) as date_stamp,
        ID,
        1 as First_attempt,
        0 as Second_Attempt,
        0 as Third_Attempt,
        0 as Total_Attempts
from Table
group by ID

以及使用此代码的总尝试次数(每天):

Select  date_stamp as date_stamp,
        ID,
        0 as First_attempt,
        0 as Second_Attempt,
        0 as Third_Attempt,
        count(ID) as Total_Attempts
from Table
group by date_stamp, ID

有人可以帮助编写第二次和第三次尝试的脚本吗?

5 个答案:

答案 0 :(得分:2)

试试这个:

;WITH CTE AS
(
    SELECT  *,
            RN = ROW_NUMBER() OVER(PARTITION BY Date_Stamp, ID ORDER BY Date_Stamp),
            Total = COUNT(*) OVER(PARTITION BY Date_Stamp, ID)
    FROM YourTable
)
SELECT  Date_Stamp AS [Date],
        ID,
        MAX(CASE WHEN RN = 1 THEN 1 ELSE 0 END) First_attempt,
        MAX(CASE WHEN RN = 2 THEN 1 ELSE 0 END) Second_attempt,
        MAX(CASE WHEN RN = 3 THEN 1 ELSE 0 END) Third_attempt,
        MAX(Total) Total_Attempts
FROM CTE
GROUP BY Date_Stamp,
         ID;

Here is一个带有演示的方形小说。

结果是:

╔════════════╦═════╦═══════════════╦════════════════╦═══════════════╦════════════════╗
║    DATE    ║ ID  ║ FIRST_ATTEMPT ║ SECOND_ATTEMPT ║ THIRD_ATTEMPT ║ TOTAL_ATTEMPTS ║
╠════════════╬═════╬═══════════════╬════════════════╬═══════════════╬════════════════╣
║ 2013-07-17 ║ ID1 ║             1 ║              1 ║             0 ║              2 ║
║ 2013-08-19 ║ ID1 ║             1 ║              0 ║             0 ║              1 ║
║ 2013-08-19 ║ ID2 ║             1 ║              0 ║             0 ║              1 ║
╚════════════╩═════╩═══════════════╩════════════════╩═══════════════╩════════════════╝

答案 1 :(得分:0)

我在sql中使用switch语句回答了这个问题 希望这有效

SELECT   Date_stamp, "Third Attempt" = 
  CASE 
     WHEN count(ID) =  3 THEN '1'
     ELSE '0'
  END
FROM tablename
GROUP BY Date_stamp;

答案 2 :(得分:0)

您是否需要三列类型为bit(true / false),或者您只需要替换为 int 类型的一列并增加总尝试次数?

然后您可以使用以下查询

Select SUM(Total_Attempts), date_stamp
FROM Table
GROUP BY date_stamp

编辑:在更新Total_Attemps列之前,请检查Total_Attempts< 3

答案 3 :(得分:0)

WITH t AS (
  SELECT
    [date]
   ,[id]
   ,ROW_NUMBER() OVER (PARTITION BY [ID] ORDER BY [date]) [num_calls]
   ,COUNT(*) OVER (PARTITION BY [ID] ORDER BY [date]) [Total_Attempts]
  FROM @MyTable
)
SELECT
  [date]
 ,[id]
 ,[1] AS [First_Attempt]
 ,[2] AS [Second_Attempt]
 ,[3] AS [Third_Attempt]
 ,[Total_Attempts]
FROM t
PIVOT(COUNT([num_calls]) FOR [num_calls] IN ([1],[2],[3])) p

答案 4 :(得分:0)

 ;WITH TEMPTABLE AS(
    SELECT  DATE_STAMP,
            ID,
            ROW_NUMBER() OVER( PARTITION BY ID ORDER BY DATE_STAMP ) AS ROWNUMBER,
            Count(ID) OVER(PARTITION BY Date_Stamp, ID) as countID
    FROM #temp)
    SELECT DATE_STAMP,
            ID,
            MAX(CASE WHEN ROWNUMBER = 1 THEN 1 ELSE 0 END )AS FIRST_ATTEMPT,
            MAX(CASE WHEN ROWNUMBER = 2 THEN 1 ELSE 0 END) AS SECOND_ATTEMPT,
            MAX(CASE WHEN ROWNUMBER = 3 THEN 1 ELSE 0 END) AS THIRD_ATTEMPT,
            MAX(countID) Total_Attempts
     FROM TEMPTABLE 
     GROUP BY DATE_STAMP,ID