2

我正在经历某种问题。这是表模式。我有两张桌子的工作,申请。

Application: aid,aname,stime,jname
Job:jid,jname,aid,start,end

Application table:    
aid  aname   stime  
A    ABC     23-SEP-13  
B    DEF     24-SEP-13

Job table:  
jid  jname  aid      start                 end  
1    job1   A   10-OCT-13 13:06:20  11-OCT-13 13:06:45  
2    job2   A   10-OCT-13 14:06:20  11-OCT-13 14:09:55  
3    job1   B   10-OCT-13 15:16:20  11-OCT-13 15:06:45  
4    job2   B   10-OCT-13 15:26:20  11-OCT-13 15:46:45  

我需要如下输出。我需要生成每个应用程序中所有作业的开始时间和结束时间之间的差异。

aname   stime     jname   (end-start)Days  Hours  Minutes  Seconds
 ABC   23-SEP-13   job1        1            0       0       25
 ABC   23-SEP-13   job2        1            0       3       35
 DEF   24-SEP-13   job1        1            0       10      25
 DEF   24-SEP-13   job2        1            0       20      25    

我尝试使用 in 子句进行提取,但这里的问题是我无法从第二个表中检索多个列。谢谢你。

4

3 回答 3

2

像这样试试

   SELECT a.aname, 
          A.stime, 
          j.jname, 
          floor(end - start) DAYS ,
          MOD(FLOOR ((end - start) * 24), 24) HOURS ,
          MOD (FLOOR ((end - start) * 24 * 60), 60) MINUTES ,
          MOD (FLOOR ((end - start) * 24 * 60 * 60), 60) SECS
   FROM   application a,
          JOB j
   WHERE  j.aid = a.aid;

使用您的样本数据。

WITH application(aid, aname, stime) AS
(
SELECT  'A', 'ABC', TO_DATE('23-SEP-13', 'DD-MON-YY') FROM DUAL
UNION ALL
SELECT 'B', 'DEF', TO_DATE('24-SEP-13', 'DD-MON-YY') FROM DUAL
),
JOB(JID, JNAME, AID, START_, END_) AS
(
SELECT 1, 'job1', 'A', TO_DATE('10-OCT-13 13:06:20', 'DD-MON-YY HH24:MI:SS'), TO_DATE('11-OCT-13 13:06:45', 'DD-MON-YY HH24:MI:SS') FROM DUAL
UNION ALL
SELECT 2, 'job2', 'A', TO_DATE('10-OCT-13 14:06:20', 'DD-MON-YY HH24:MI:SS'), TO_DATE('11-OCT-13 14:09:55', 'DD-MON-YY HH24:MI:SS') FROM DUAL
UNION ALL
SELECT 3, 'job1', 'B', TO_DATE('10-OCT-13 15:16:20', 'DD-MON-YY HH24:MI:SS'), TO_DATE('11-OCT-13 15:06:45', 'DD-MON-YY HH24:MI:SS')  FROM DUAL
UNION ALL
SELECT 4, 'job2', 'B', TO_DATE('10-OCT-13 15:26:20', 'DD-MON-YY HH24:MI:SS'), TO_DATE('11-OCT-13 15:46:45', 'DD-MON-YY HH24:MI:SS') FROM DUAL
)
SELECT a.aname, 
     A.stime, 
     j.jname, 
     floor(end_ - start_) DAYS ,
     MOD(FLOOR ((end_ - start_) * 24), 24) HOURS ,
     MOD (FLOOR ((end_ - start_) * 24 * 60), 60) MINUTES ,
     MOD (FLOOR ((end_ - start_) * 24 * 60 * 60), 60) SECS
FROM   application a,
     JOB j
WHERE  j.aid = a.aid
AND    a.stime > to_date('23-sep-12','dd-mon-yy');
于 2013-10-03T07:53:45.623 回答
0

如我所见,我认为您的架构未正确规范化。

我建议改用以下设计:

Application (aid, aname, stime, jid)
Job (jid, jname, start, end)

申请表将包含工作申请。我们必须考虑到一份申请是针对一项工作提出的,任何工作在任何给定时间都可以有 0、1 或多个申请。因此,我在引用jidApplication表中添加了一个外键Job.jid

您可以像这样查询以获得所需的数据:

SELECT a.aname, a.stime, j.jname,
       INTERVAL (j.end - j.start) DAY TO SECOND AS end_start
  FROM job j INNER JOIN application a
       ON a.jid = j.jid

请注意,此查询仅适用于 Oracle。

于 2013-10-03T07:52:43.263 回答
0

尝试使用TIMESTAMPDIFF指定格式,例如希望以月、日、分钟或秒为单位的差异

SELECT a.aname ,a.stime ,  j.jname,  
TIMESTAMPDIFF(DAY,j.`end`,j.`start`) AS 'end-start',
TIMESTAMPDIFF(HOUR,j.`end`,j.`start`) AS `hours`,
TIMESTAMPDIFF(MINUTE,j.`end`,j.`start`) AS `minutes`,
TIMESTAMPDIFF(SECOND,j.`end`,j.`start`) AS `seconds`
FROM `Application` a
LEFT JOIN `Job` j ON (j.aid =a.aid)

注意 mysql ,请确保您为日期列使用了正确的数据类型

于 2013-10-03T07:49:37.087 回答