Postgres条件选择?

时间:2013-02-10 01:08:54

标签: sql postgresql database-design join

我不确定如何在没有示例的情况下描述这个问题,所以这是一个例子:

我有三张桌子:

表1:框

BoxID | Box Name | Box Cost
1 | Bob's Box | $20
2 | Matt's Box | $21
3 | Jacob's Box | $22
4 | Beth's Box | $23
5 | Rachel's Box | $24

表2:Box_ProcessIDs

BoxID | ParentProcessID
1 | 123
2 | 456
3 | 789
4 | 012
5 | 234

表3:Box_Processes

ParentProcess | ChildProcess | Start Time | End Time | ProcessName 
123 | AAA | 1:00 | 1:05 | Invoiced
123 | AAB | 1:30 | 1:35 | Packed
123 | BBB | 2:00 | 2:05 | Shipped

456 | CDD | 3:15 | 3:20 | Invoiced
456 | DDD | 3:25 | 3:30 | Packaging_Complete
456 | CCD | 3:35 | 3:40 | Shipped

789 | EEE | 4:15 | 4:20 | Invoiced
789 | EEF | 4:25 | 4:30 | Done_Packing
789 | EFF | 4:35 | 4:40 | Shipped

我希望输出列为: 包装箱名称,包装箱成本,包装箱运输时间,包装盒装箱时间,包装盒发票持续时间

我知道我可以使用%pack%来选择我的包装字段,我可以弄清楚如何使用Date_Part或某些来获取我的持续时间,但我不知道如何获得我的{{1 }},shipping durationpacking duration到不同的字段中。

2 个答案:

答案 0 :(得分:2)

所以你想做像这样的伪sql?

select b.box_name, b.box_cost,
bp2.end_time - bp2.start_time as box_shipping_duration,
bp3.end_time - bp3.start_time as box_packing_duration,
bp1.end_time - bp1.start_time as box_invoice_duration
from boxes b

join box_processids bpid on b.boxid = bpid.boxid

left outer join box_processes bp1 on bpid.parentprocessid = bp1.parentprocess
and processname = 'Invoiced'

left outer join box_processes bp2 on bpid.parentprocessid = bp2.parentprocess
and processname = 'Shipped'

left outer join box_processes bp3 on bpid.parentprocessid = bp3.parentprocess
and processname = 'Packed'

(需要空检查,正确的日期时间处理,更好的名称等)

答案 1 :(得分:2)

首先,您的设计存在缺陷。 time对此目的不利。一旦你过夜,它就会破裂。请改用timestamp or timestamptz

接下来,由于在PostgreSQL中自动将不带引号的标识符强制转换为小写,因此您的命名约定也不好。我把它改编成了更有用的东西。

CREATE TABLE boxes (box_id int PRIMARY KEY, box_name text, box_cost numeric);
INSERT INTO boxes VALUES
  (1, 'Bob''s Box', 20)
 ,(2, 'Matt''s Box', 21)
 ,(3, 'Jacob''s Box', 22)
 ,(4, 'Beth''s Box', 23)
 ,(5, 'Rachel''s Box', 24);

CREATE TABLE box_processids (
  box_id int 
 ,parentprocess_id int
 ,PRIMARY KEY(box_id, parentprocess_id)
);

INSERT INTO box_processids  VALUES
  (1, 123)
 ,(2, 456)
 ,(3, 789)
 ,(4, 012)
 ,(5, 234);

CREATE TABLE box_processes (
  parentprocess_id int
 ,childprocess_id char(3)
 ,start_time timestamp
 ,end_time timestamp
 ,processname text
 ,PRIMARY KEY(parentprocess_id, childprocess_id)
);

INSERT INTO box_processes VALUES
  (123, 'AAA', '2013-2-10 1:00', '2013-2-10 1:05', 'Invoiced')
 ,(123, 'AAB', '2013-2-10 1:30', '2013-2-10 1:35', 'Packed')
 ,(123, 'BBB', '2013-2-10 2:00', '2013-2-10 2:05', 'Shipped')

 ,(456, 'CDD', '2013-2-10 3:15', '2013-2-10 3:20', 'Invoiced')
 ,(456, 'DDD', '2013-2-10 3:25', '2013-2-10 3:30', 'Packaging_Complete')
 ,(456, 'CCD', '2013-2-10 3:35', '2013-2-10 3:40', 'Shipped')

 ,(789, 'EEE', '2013-2-10 4:15', '2013-2-10 4:20', 'Invoiced')
 ,(789, 'EEF', '2013-2-10 4:25', '2013-2-10 4:30', 'Done_Packing')
 ,(789, 'EFF', '2013-2-10 4:35', '2013-2-10 4:40', 'Shipped');

您的查询可能如下所示:

SELECT b.box_name
      ,b.box_cost
      ,(i.end_time - i.start_time) AS box_invoice_duration
      ,(p.end_time - p.start_time) AS box_packing_duration
      ,(s.end_time - s.start_time) AS box_shipping_duration
FROM   boxes               b
LEFT   JOIN box_processids bp USING (box_id)
LEFT   JOIN box_processes  i ON i.parentprocess_id = bp.parentprocess_id
                            AND i.processname = 'Invoiced'
LEFT   JOIN box_processes  p ON i.parentprocess_id = bp.parentprocess_id
                            AND p.processname = 'Packed'
LEFT   JOIN box_processes  s ON i.parentprocess_id = bp.parentprocess_id
                            AND s.processname = 'Shipped'

->sqlfiddle
使用to_char()以您想要的任何方式格式化interval