简体   繁体   中英

SQL - Creating counts for 1st, 2nd, 3rd occasion in a list

I have a data set with a date stamp and an ID number as shown:

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

From this data, I need to populate a table that has the following fields: Date, ID, First_attempt, Second_Attempt, Third_Attempt, Total_Attempts.

With that data, the file would look like this:

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

I have been able to get the first attempts with this code:

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

and the Total Attempts (per day) with this code:

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

Can someone help with scripting the second and third attempts??

Try this:

;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 a sqlfiddle with a demo.

And the results are:

╔════════════╦═════╦═══════════════╦════════════════╦═══════════════╦════════════════╗
║    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 ║
╚════════════╩═════╩═══════════════╩════════════════╩═══════════════╩════════════════╝

I have answered the question using switch statement in sql Hope this works

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

Do you need three columns of type bit (true/false) or can you just replace with one column of type int and increment the total attempts?

Then you can use the following query

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

Edit: Before you Update the column Total_Attemps, check if Total_Attempts < 3

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
 ;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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM