根据原始表中的远程外键值从其他表行获取SUM

时间:2013-08-19 09:20:35

标签: sqlite

我有三个表,vehicle_service_invoicevehicle_service_laborvehicle_service_parts。所有这些表都使用每个表上的invoice行相互关联。我想查看这三个名为vehicle_service的表,这些表将显示零件和人工连接到发票表时的总价格。

使用单个表格,我SUMextendedGROUP BY invoicevehicle_service_laborvehicle_service_partsvehicle date po invoice labor parts sublet total 008 2013-01-07 1301070 1111 204.00 129.11 0.00 333.11 008 2013-01-21 1301210 1122 521.00 584.70 0.00 1105.70 003 2013-02-07 1302070 2211 34.00 0.00 0.00 34.00 004 2013-02-18 1302180 2222 51.00 70.14 0.00 121.14 003 2013-03-18 1303180 3311 51.00 70.14 0.00 121.14 003 2013-04-18 1204180 4411 51.00 412.83 0.00 463.83 008 2013-04-25 1304250 4422 68.00 269.82 0.00 337.82 006 2013-05-25 1305250 5511 204.00 17.85 0.00 221.85 007 2013-05-29 1305290 5522 442.00 299.35 0.00 741.35 006 2013-06-29 1306290 6611 136.00 0.00 0.00 136.00 003 2013-07-16 1307160 7711 136.00 0.00 0.00 136.00 004 2013-08-16 1308160 8811 187.00 172.53 0.00 359.53 003 2013-09-16 1309160 9911 136.00 140.47 0.00 276.47 007 2013-10-30 1310300 1011 34.00 29.83 0.00 63.83 008 2013-11-30 1311300 0000 136.00 175.25 0.00 311.25 。但是,如何将此信息连接回vehicle_service_invoice的发票栏,以便正确显示?

我希望它看起来像这样......

vehicle_service

invoice date       unit odometer sublet sub     po
1111    2013-01-07 008  34863    0         0.00 1301070
1122    2013-01-21 008  36435    0      1105.70 1301210
2211    2013-02-07 003  35594    0        34.00 1302070
2222    2013-02-18 004  49079            121.14 1531214
3311    2013-03-18 003  36158    0       121.14 1303180
4411    2013-04-18 003           0       463.83 1204180
4422    2013-04-25 008  36516    0       337.82 1304250
5511    2013-05-25 006  48807    0       221.85 1305250
5522    2013-05-29 007  37133    0       741.35 1305290
7711    2013-06-06 003  38535    0       136.00 1307160
8811    2013-06-16 004  51588    0       359.53 1308160
9911    2013-07-16 003  39302    0       276.47 1309160
1011    2013-07-30 007  39675    0        63.83 1310300
0000    2013-08-30 008  40027    0       311.25 1311300

这些是我从中获取信息的表格。

vehicle_service_invoice

invoice labor           hours extended description
1111    Pick Up         0     0        Pick Up & Del...
1111    Fuel Leak       3     204      Locate fuel l...
1122    PM Inspection   5     340      Preventative ...
1122    NYS Inspection  0.5   45       NYS Inspection
1122    Tires           1     68       Breakdown, mo...
1122    Lights          0.5   34       Repair front ...
1122    Receptacle      0.5   34       Replace broke...
2211    Flat            0.5   34       Repair Rear Flat
2222    Door            0.75  51       Remove broken...
3311    Door            0.75  51       Remove broken...
4411    Mirror          0.75  51       Remove and Re...
4422    Batteries       1     68       Check batteri...
5511    Pick Up         0     0        Pick Up & Del...
5511    Brakes          3     204      Add fluid, br...
5522    PM Inspection   5     340      Preventative ...
5522    NYS Inspection  0.5   34       NYS Inspection
5522    Leak            1     68       Checkout Cab ...
7711    Flat Call       2     136      Service Call ...
8811    LOF             2     136      Change Oil in...
8811    Flat            0.75  51       Raise and Sup...
9911    LOF             2     136      Change Oil in...
1011    Door            0.5   34       Check Out Doo...
0000    Flats           2     136      Service call ...

vehicle_service_labor

invoice part             qty sale   extended description
1111    Fuel Treatment   1    28.73  28.73   Enertech
1111    Fuel Filter      1    76.66  76.66   PF7698
1111    Antifreeze       1    23.72  23.72   Glychol
1122    Engine Oil       16    4.48  71.68   15W40
1122    Oil Filter       1    34.76  34.76   FL1995
1122    Air Filter       2    31.26  62.52   FA1618
1122    Fuel Additive    1    14.33  14.33   Stanadyne
1122    Coolant Additive 1    12.95  12.95   DCA60L
1122    Recepticle Cover 1    35     35      KUS 091-3WH
1122    22575r16 10 P... 2   165    333      
1122    Tire Disposal... 2     8     16      Tire Disposal
1122    Tire Waste Sa... 1     2.25   2.25   NYS Tire Fee
1122    Mini Lightbulb   1     3.13   3.13   3156
1122    Bulb             2     1.04   2.08   194
2222    Pivot-Sprint Bar 1    31.11  31.11   Brapivot
2222    Door Check Right 1    39.03  39.03   15721
3311    Pivot-Sprint Bar 1    31.11  31.11   Brapivot
3311    Door Check Right 1    39.03  39.03   15721
4411    Mirror Assemb... 1   412.83 412.83   9C2Z17683CA
4422    Battery          2   134.91 269.82   665MF
5511    Brake Fluid      1     7.95  7.95    Brake Fluid
5511    Brake Parts C... 1     9.9   9.9     Brake Clean
5522    Engine Oil       16    4.48  71.68   15W40
5522    Oil Filter       1    24.86  24.86   FL2016
5522    Fuel Filter      1   109.58 109.58   PF7852
5522    Air Filter       1    69.95  69.95   PA4171
5522    Fuel Additive    1    14.33  14.33   Stanadyne
5522    Silicone Sealant 1     8.95   8.95   Silicone
8811    Engine Oil       16    4.48  71.68   15W40
8811    Oil Filter       1    24.86  24.86   FL2016
8811    Antifreeze       1    23.8   23.8    FYA - Ford Yellow
8811    Fuel Treatment   1    28.73  28.73   Enertech
8811    Windshield Wa... 1     2.25   2.25   Windshield Fluid
8811    Coolant Additive 1    12.95  12.95   DCA60L
8811    Power Steerin... 1     8.26   8.26   PSF
9911    Engine Oil       16    4.48  71.68   15W40
9911    Oil Filter       1    24.86  24.86   FL2016
9911    Fuel Treatment   1    28.73  28.73   Enertech
9911    Windshield Wa... 1     2.25   2.25   Windshield
9911    Coolant Additive 1    12.95  12.95   DCA60L
1011    Magnetic Jamb... 1    29.83  29.83   19171
0000    10 Ply Tire      1   165    165      255/75r16lt
0000    Tire Disposal... 1     8      8      Tire Disposal
0000    Tire Waste Sa... 1   2.25   2.25     NYS Tire Fee

vehicle_service_parts

{{1}}

这些数据有点谨慎,因为它只是为了说明目的,只是为了向您展示我的意思,以及我正在谈论的数据类型。

1 个答案:

答案 0 :(得分:2)

您可以使用子查询从其他表中获取总计。 为了能够将两个小计一起添加,请使用另一个子查询:

SELECT *, labor + parts AS total
FROM (SELECT unit AS vehicle,
             date,
             po,
             invoice,
             (SELECT TOTAL(extended)
              FROM vehicle_service_labor
              WHERE invoice = vehicle_service_invoice.invoice
             ) AS labor,
             (SELECT TOTAL(extended)
              FROM vehicle_service_parts
              WHERE invoice = vehicle_service_invoice.invoice
             ) AS parts
      FROM vehicle_service_invoice)
ORDER BY date,
         invoice