将复杂的 count() 查询结果插入 table

Inserting complex count() query results to table

我有table个公司的出货数据,叫t_shipment。 (其中一些)headers 是 acc_num、type_of_business、contract_exception、payment_status 等

我需要对其他部门的工作做一个总结table。所以我使用 CREATE TABLE 创建了一个新的 table、ship_recap。

CREATE TABLE ship_recap
(vol_lumber(int), vol_oil(int);)

那我需要把t_shipment的相关数据复述到ship_recap。我用了

 INSERT INTO ship_recap (vol_lumber)
 SELECT COUNT(acc_num) from t_shipment WHERE type_of_business = 'LMB' and (contract_exception = 'VALID' OR payment_status IS NOT NULL)
 INSERT INTO ship_recap (vol_oil)
 SELECT COUNT(acc_num) from t_shipment where type_of_business = 'OIL' and (contract_exception = 'VALID' OR payment_status IS NOT NULL);)

它运行,但结果是:

 ____________________
|vol_lumber| vol_oil |
----------------------
|   150    |   NULL  |
|   NULL   |   230   |
----------------------

但我希望它们是:

 ____________________
|vol_lumber| vol_oil |
----------------------
|   150    |   230   |
----------------------

我试过使用

INSERT INTO ship_recap (vol_lumber, vol_oil)
(SELECT COUNT(acc_num) from t_shipment WHERE type_of_business = 'LMB' and (contract_exception = 'VALID' OR payment_status IS NOT NULL),
SELECT COUNT(acc_num) from t_shipment where type_of_business = 'OIL' and (contract_exception = 'VALID' OR payment_status IS NOT NULL);)

和相同逻辑的排列(例如,将逗号更改为分号,或去掉括号),但每次都返回语法错误。

results/recap table 可能有多达 20+ headers,其他查询也可能会稍微复杂一些。 我需要一种方法来正确地将 SELECT/COUNT-ed 数据插入到摘要 table 中并将它们保持在一行中。

编辑:根据 Reno 的建议,我尝试了这个

 CREATE TABLE ship_recap (vol_OIL int,vol_LUM int,vol_BEV int,processed_OIL int,processed_LUM int,processed_BEV int);
 INSERT INTO ship_recap (vol_OIL, vol_LUM, vol_BEV, processed_OIL, processed_LUM, processed_BEV)
 SELECT
  COUNT(IF(type_of_business = 'OIL' and process_date = '2016-05-12', 1, NULL)),
  COUNT(IF(type_of_business = 'LUM' and process_date = '2016-05-12', 1, NULL)),
  COUNT(IF(type_of_business = 'BEV' and process_date = '2016-05-12', 1, NULL)),
  COUNT(IF(type_of_business = 'OIL' and process_date = '2016-05-12' and (contract_exception = ‘VALID’ OR payment_status IS NOT NULL), 1, NULL)),
  COUNT(IF(type_of_business = 'LUM' and process_date = '2016-05-12' and (contract_exception = ‘VALID’ OR payment_status IS NOT NULL), 1, NULL)),
  COUNT(IF(type_of_business = 'BEV' and process_date = '2016-05-12' and (contract_exception = ‘VALID’ OR payment_status IS NOT NULL), 1, NULL)) FROM t_shipment;

一旦我更正了缺少的参数和括号,它就起作用了。

尝试关注 sql,可能对您有所帮助;)

 CREATE TABLE ship_recap (vol_OIL int,vol_LUM int,vol_BEV int,processed_OIL int,processed_LUM int,processed_BEV int);
 INSERT INTO ship_recap (vol_OIL, vol_LUM, vol_BEV, processed_OIL, processed_LUM, processed_BEV)
 SELECT
  COUNT(IF(type_of_business = 'OIL' and process_date = '2016-05-12', 1, NULL)),
  COUNT(IF(type_of_business = 'LUM' and process_date = '2016-05-12', 1, NULL)),
  COUNT(IF(type_of_business = 'BEV' and process_date = '2016-05-12', 1, NULL)),
  COUNT(IF(type_of_business = 'OIL' and process_date = '2016-05-12' and (contract_exception = ‘VALID’ OR payment_status IS NOT NULL), 1, NULL)),
  COUNT(IF(type_of_business = 'LUM' and process_date = '2016-05-12' and (contract_exception = ‘VALID’ OR payment_status IS NOT NULL), 1, NULL)),
  COUNT(IF(type_of_business = 'BEV' and process_date = '2016-05-12' and (contract_exception = ‘VALID’ OR payment_status IS NOT NULL), 1, NULL)) FROM t_shipment;