2016-05-13 58 views
0
  • 注意:此问题之前已过于简化;该问题已被追加,以更好地了解我的问题

我有一个公司的货运数据表,名为t_shipment。 (其中一些)标题是acc_num,type_of_business,contract_exception,payment_status等。将复数count()查询结果插入表

我需要为其他部门的工作侧做一个回顾表。所以我使用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数据插入到概括表中,并将它们保存在一行中。

编辑:按照雷诺的建议,我想这

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; 

它的工作,一旦我纠正了缺少的参数和括号。

+1

'SELECT SUM(水果= '苹果')苹果,SUM(水果= '香蕉')香蕉FROM MY_TABLE;'我不知道为什么你会存储这个派生的数据 – Strawberry

回答

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; 
+0

谢谢,但实际上查询比这更长(我的过于简单化它不好)。 ='LMB'且(contract_exception ='VALID'或payment_status不为NULL),' 'select count(acc_num)from type_of_busines ='OIL'and(contract_exception ='VALID'OR payment_status IS NOT NULL);)' 结果/回顾表可能有多达25个标题,每个查询也可能稍微复杂一点。 – Rheine

+0

@Rheine那么你为什么不重新编辑你的帖子,让我们更清楚你的问题please.:D – Blank

+0

编辑,请看看。谢谢。 – Rheine