简体   繁体   English

SQL-为计算目的创建临时表

[英]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). 我想从我正在使用的应用程序表中获取一些信息(让我们称之为表TICKETS)。 There are two columns that have the information I need, MODIFIED and CREATED. 有两列具有我需要的信息,MODIFIED和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. 我的想法是创建一个新表(让我们称之为RESULT),其中所有HH的值都为TIME:每天的MM,第二列的结果为:SUM((CREATED <= TIME)AND(MODIFIED) > TIME))对于TICKET表的所有行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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