你的问题真的不清楚....
http://sqlfiddle.com/#!2/d0bc3/1/0
create table GL_Table (
Country varchar(20),
Account varchar(20),
Amount float
);
insert into GL_Table values ('USA','PTBI',100);
insert into GL_Table values ('USA','PTBI',100);
insert into GL_Table values ('USA','PTBI',100);
insert into GL_Table values ('USA','MLS_ENT',101);
insert into GL_Table values ('USA','RSC_DEV',102);
insert into GL_Table values ('USA','RSC_DEV',102);
insert into GL_Table values ('USA','NOTHING',777);
insert into GL_Table values ('USA','NOTHING',777);
所以现在的数据是:
COUNTRY ACCOUNT AMOUNT
USA PTBI 100
USA PTBI 100
USA PTBI 100
USA MLS_ENT 101
USA RSC_DEV 102
USA RSC_DEV 102
USA NOTHING 777
USA NOTHING 777
然后SUM
只添加某些内容的行的查询将如下所示:
INSERT INTO GL_TABLE
SELECT COUNTRY,
concat('SUM OF ', ACCOUNT) as NEW_ACCT_NAME,
SUM(AMOUNT) as SUM_ACCOUNT
FROM GL_TABLE
WHERE ACCOUNT IN ('PTBI', 'MLS_ENT','VAL_ALLOW','RSC_DEV')
GROUP BY COUNTRY, ACCOUNT;
现在数据将是 ( select * from gl_table
):
COUNTRY ACCOUNT AMOUNT
USA PTBI 100
USA PTBI 100
USA PTBI 100
USA MLS_ENT 101
USA RSC_DEV 102
USA RSC_DEV 102
USA NOTHING 777
USA NOTHING 777
USA SUM OF MLS_ENT 101
USA SUM OF PTBI 300
USA SUM OF RSC_DEV 204
再次查看您的查询,看起来您想要一个国家/地区的总和?不知道...(不会有太大变化)
INSERT INTO GL_TABLE
SELECT COUNTRY,
concat('SUM FOR ', COUNTRY) as NEW_ACCT_NAME,
SUM(AMOUNT) as SUM_ACCOUNT
FROM GL_TABLE
WHERE ACCOUNT IN ('PTBI', 'MLS_ENT','VAL_ALLOW','RSC_DEV')
GROUP BY COUNTRY;
所以数据最终会...
COUNTRY ACCOUNT AMOUNT
USA PTBI 100
USA PTBI 100
USA PTBI 100
USA MLS_ENT 101
USA RSC_DEV 102
USA RSC_DEV 102
USA NOTHING 777
USA NOTHING 777
USA SUM FOR USA 605