Domanda

I have a problem with a program I'm trying to finish. I have a table that stores three timestamps. It's used for making statistics of queries and responses. The first timestamp is for the date and time of the query (Date_Sent), the other two are for the first time one of our expert responds to the query (FirstResponse) and when that query was satisfied (LastResponse). I'm supposed to create a graph based on the following criteria:

  1. The total number of queries per month
  2. The number of queries responded in less than two hours
  3. The number of queries responded in less than 24 hours but over two
  4. The number of queries responded in less than 48 hours but over 24
  5. The number of queries responded in less than 72 hours but over 48
  6. The number of queries responded in less than 96 hours but over 72

I'm able to do the first requirement:

SELECT Count(Date_Sent) AS TotalQueries, 
       SUBSTRING(CONVERT(VARCHAR(11), Date_Sent, 113), 4, 8) AS Month, 
       RIGHT(CONVERT(VARCHAR(7), Date_Sent, 120), 2) AS Month_Order 
FROM AskAO.dbo.AskAO_Stats 
WHERE Date_Sent BETWEEN '6/1/2012' AND '6/30/2013' AND 
      FirstResponse != '' AND 
      LastResponse != '' AND 
      FirstResponse < LastResponse   
GROUP BY SUBSTRING(CONVERT(VARCHAR(11), Date_Sent, 113), 4, 8), 
         CONVERT(VARCHAR(7), Date_Sent, 120) 
ORDER BY SUBSTRING(CONVERT(VARCHAR(11), Date_Sent, 113), 4, 8)

I'm able to get this sample result:

TotalQueries     Month   Month_Order
    655        Jun 2013      06
    289        May 2013      05

However, I don't know how to get the others :( I know I can calculate the values via DateDiff, but what I would need would be the number of the queries meeting those particular criteria per month. I would need this:

 TotalQueries   2Hrs  24Hrs  48Hrs  72Hrs  96Hrs  Month     Month_Order
     655        300    190    80     55     30    Jun 2013      06
     289        180     50    30     15     14    May 2013      05

I'm unfamiliar with complicated SQL, so I'm not sure if this is doable or not.

È stato utile?

Soluzione

try something like this

SELECT Count(Date_Sent) AS TotalQueries, 
    SUBSTRING(CONVERT(VARCHAR(11), Date_Sent, 113), 4, 8) AS Month, 
    RIGHT(CONVERT(VARCHAR(7), Date_Sent, 120), 2) AS Month_Order ,
    SUM(CASE WHEN DATEDIFF(HOUR, FirstResponse, LastResponse) < 2 THEN 1 ELSE 0 END) AS LessThan2Hours,
    SUM(CASE WHEN DATEDIFF(HOUR, FirstResponse, LastResponse) between 2 AND 24 THEN 1 ELSE 0 END) AS LessThan24Hours,
    SUM(CASE WHEN DATEDIFF(HOUR, FirstResponse, LastResponse) between 25 AND 48 THEN 1 ELSE 0 END) AS LessThan24Hours,
    SUM(CASE WHEN DATEDIFF(HOUR, FirstResponse, LastResponse) between 25 AND 48 THEN 1 ELSE 0 END) AS LessThan48Hours,
    SUM(CASE WHEN DATEDIFF(HOUR, FirstResponse, LastResponse) between 49 AND 72 THEN 1 ELSE 0 END) AS LessThan72Hours,
    SUM(CASE WHEN DATEDIFF(HOUR, FirstResponse, LastResponse) between 73 AND 96 THEN 1 ELSE 0 END) AS LessThan96Hours

FROM AskAO.dbo.AskAO_Stats 
WHERE Date_Sent BETWEEN '6/1/2012' AND '6/30/2013' AND FirstResponse != '' AND LastResponse != '' AND FirstResponse < LastResponse   
GROUP BY SUBSTRING(CONVERT(VARCHAR(11), Date_Sent, 113), 4, 8), CONVERT(VARCHAR(7), Date_Sent, 120) 
ORDER BY SUBSTRING(CONVERT(VARCHAR(11), Date_Sent, 113), 4, 8)

Altri suggerimenti

The point is COUNT() counts only values that are not null. So you can write some expression within COUNT(...) according to your logic in a way, that it equals null when you don't want to sum up the row and not null otherwise. For example, to count number of responses in less that two hours you can write:

COUNT(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) < 2 THEN Date_Sent ELSE NULL END) AS [2Hrs]

You can do all other cases similar.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top