我有一家名为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数据插入到recap表中并将它们保存在一行中。
编辑:根据里诺的建议,我试过这个 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;
一旦我纠正了缺失的参数和括号,它就起作用了。
答案 0 :(得分:0)
尝试以下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;