简体   繁体   中英

SQL-Creation of a temporary table for calculation purposes

I want to get some information from a table of an application I am using (Lets call the table TICKETS). There are two columns that have the information I need, MODIFIED and CREATED. CREATED is the time a ticket got created and MODIFIED is the time the same ticket got modified (obviously). My question is how do I get the number of tickets that were stuck for every minute of the day (Have been created but have not been modified yet)?

CREATED          | MODIFIED 
1/1/2016 2:02:42 | 1/1/2016 2:02:48
1/1/2016 2:04:23 | 1/1/2016 2:06:02

My idea was to create a new table (Lets call it RESULT) which will have a column TIME for all values of HH:MM of day and the second column would be the result of: SUM((CREATED<=TIME) AND (MODIFIED>TIME)) for all the rows of the TICKET table.

 TIME  | RESULT 
 HH:MM |  SUM((CREATED<=TIME) AND (MODIFIED>TIME))
  1. My questions are how I implement such a thing?

  2. Is there a better approach?

Not exactly what you want, but as it's a difficult request, every bit help I think.

This one bring you the number of ticket by time of day, when you've got at least one ticket sold.

SELECT 
    LPAD(EXTRACT(HOUR FROM created), 2, '0') || ':' 
        || LPAD(EXTRACT(MINUTE FROM created), 2, '0') as timeOfDay, 
    COUNT(*) as ticketBuy
FROM t
GROUP BY 
    LPAD(EXTRACT(HOUR FROM created), 2, '0') || ':' 
        || LPAD(EXTRACT(MINUTE FROM created), 2, '0') 
ORDER BY timeOfDay;

Get you :

+-----------+-----------+
| TIMEOFDAY | TICKETBUY |
+-----------+-----------+
| 03:02     |         1 |
| 03:04     |         1 |
| 04:04     |         2 |
| 09:04     |         2 |
| 10:04     |         1 |
+-----------+-----------+

From :

+--------------------+--------------------+
| CREATED            |      MODIFIED      |
+--------------------+--------------------+
| 1/1/2016 3:02:42   |  1/1/2016 5:02:48  |
| 1/1/2016 3:04:23   |  1/1/2016 4:06:02  |
| 1/1/2016 4:04:23   |  1/1/2016 5:26:02  |
| 1/1/2016 4:04:23   |  1/1/2016 6:06:02  |
| 1/1/2016 9:04:23   |  1/1/2016 9:16:02  |
| 1/1/2016 9:04:53   |  1/1/2016 9:06:02  |
| 1/1/2016 10:04:23  |  1/1/2016 11:06:02 |
+--------------------+--------------------+

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