【问题标题】:Inserting complex count() query results to table将复杂的 count() 查询结果插入表
【发布时间】:2016-09-09 03:27:20
【问题描述】:
  • 注意:这个问题以前被过度简化了;附加问题是为了更好地理解我的问题

我有一家公司的发货数据表,称为 t_shipment。 (部分)标头为 acc_num、type_of_business、contract_exception、payment_status 等。

我需要为其他部门的工作做一个总结表。因此,我使用 CREATE 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);)

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

结果/回顾表可能有多达 20 多个标题,其他查询也可能稍微复杂一些。 我需要一种方法来正确地将 SELECT/COUNT-ed 数据插入到回顾表中,并将它们保持在一行中。

编辑:根据 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;

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

【问题讨论】:

  • SELECT SUM(fruit = 'apple') apple, SUM(fruit = 'banana') banana FROM my_table; 我不知道你为什么要存储这些派生数据

标签: mysql select mariadb


【解决方案1】:

试试下面的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;

【讨论】:

  • 谢谢,但实际上查询比这更长(我不利于过度简化它。)e.g.:INSERT INTO top_shipper (vol_lumber, vol_oil)SELECT count(acc_num) from ship_main where type_of_business = 'LMB' and (contract_exception = 'VALID' OR payment_status IS NOT NULL),select count(acc_num) from where type_of_busines = 'OIL' and (contract_exception = 'VALID' OR payment_status IS NOT NULL);) 结果/回顾表可能有多达 25 个标题和每个查询也可能稍微复杂一些。
  • @Rheine 那么你为什么不再次编辑你的帖子,让我们更清楚你的问题。:D
  • 已编辑,请查看。谢谢。
  • @Rheine 我已经更新了insert sql,请再检查一遍。
  • 谢谢。我试过了,但它仍然返回语法错误。我的问题已使用我使用的确切查询进行了更新。
猜你喜欢
  • 2014-10-20
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 2014-11-08
相关资源
最近更新 更多