account_id account_name sellingprice factory_id
001 stephe 200 aa
001 stephe 100 bb
002 adrian 200 aa
002 adrian 300 bb
002 adrian 100 cc
003 nweton 250 aa
003 nweton 300 cc
现在想统计结果如下,account_id, account_name,sum(sellingprice_factory_aa),sum(sellingprice_factory_bb),sum(sellingprice_factory_cc)
请高手帮忙指点下如何写sql代码,谢谢。
12 个解决方案
#1
行列转换:
select account_id ,account_name ,
sum(case when factory_id='aa' then sellingprice else 0 end),
sum(case when factory_id='bb' then sellingprice else 0 end),
sum(case when factory_id='bb' then sellingprice else 0 end)
from tb
group by account_id ,account_name
#2
select account_id, account_name,
sum(case factory_id when 'aa' then sellingprice end) as sellingprice_factory_aa,
sum(case factory_id when 'bb' then sellingprice end) as sellingprice_factory_bb,
sum(case factory_id when 'cc' then sellingprice end) as sellingprice_factory_cc
from 表格
group by account_id, account_name
#3
--SQL Server 2000
select account_id,account_name,
SUM(case when factory_id='aa' then sellingprice else 0 end) [aa],
SUM(case when factory_id='bb' then sellingprice else 0 end) [bb],
SUM(case when factory_id='cc' then sellingprice else 0 end) [cc]
from tab group by account_id,account_name
-- SQL Server 2005
select account_id,account_name,[aa],[bb],[cc]
from tab pivot (sum(sellingprice) for [factory_id] in([aa],[bb],[cc])) pvt
#4
SELECT account_id ,account_name ,
[aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[cc]=sum(case factory_id when 'bb' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id,account_name
#5
更正:
SELECT account_id ,account_name ,
[aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[cc]=sum(case factory_id when 'cc' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id,account_name
#6
多谢,另外,如果需要统计出所有客户的sellingprice该如何修改这个sql,例如除了account_name,sum(sellingprice_factory_aa),sum(sellingprice_factory_bb),sum(sellingprice_factory_cc) 还需要 sum(sellingprice_factory_aa_allaccounts), sum(sellingprice_factory_bb_allaccounts), sum(sellingprice_factory_cc_allaccounts).
#7
不能和在一起:(不知道我有没有理解lz的意思)
分两个查询:
1.
SELECT account_id ,account_name ,
[aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[cc]=sum(case factory_id when 'cc' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id,account_name
2.
SELECT account_id,
[All_aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[All_bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[All_cc]=sum(case factory_id when 'cc' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id
#8
ding
#9
select
account_id, account_name,
sum(case factory_id when 'aa' then sellingprice end) as sellingprice_factory_aa,
sum(case factory_id when 'bb' then sellingprice end) as sellingprice_factory_bb,
sum(case factory_id when 'cc' then sellingprice end) as sellingprice_factory_cc
from
tb
group by
account_id, account_name
#10
行列转换
#11
学习了
#12
-- 不知道理解对否
create table tab
(account_id char(3),account_name varchar(10),sellingprice int,factory_id char(2));
insert into tab
select '001','stephe',200,'aa' union all
select '001','stephe',100,'bb' union all
select '002','adrian',200,'aa' union all
select '002','adrian',300,'bb' union all
select '002','adrian',100,'cc' union all
select '003','nweton',250,'aa' union all
select '003','nweton',300,'cc'
select account_id,account_name,[aa],[bb],[cc] from tab
pivot (sum(sellingprice) for [factory_id] in([aa],[bb],[cc])) pvt
union all
select 'sum_by_factory','',[aa],[bb],[cc]
from (select factory_id,sum(sellingprice) s
from tab group by factory_id) p pivot (max(s) for [factory_id] in([aa],[bb],[cc])) pvt
/*
002 adrian 200 300 100
003 nweton 250 NULL 300
001 stephe 200 100 NULL
sum_by_factory 650 400 400
*/
select account_id,account_name,[aa],[bb],[cc] from
(select account_id=ISNULL(account_id,'sum'),
account_name=(case when account_name is null and account_id is null then '' else account_name end),
SUM(case when factory_id='aa' then sellingprice else 0 end) [aa],
SUM(case when factory_id='bb' then sellingprice else 0 end) [bb],
SUM(case when factory_id='cc' then sellingprice else 0 end) [cc]
from tab group by account_id,account_name with rollup) t
where account_name is not null
#1
行列转换:
select account_id ,account_name ,
sum(case when factory_id='aa' then sellingprice else 0 end),
sum(case when factory_id='bb' then sellingprice else 0 end),
sum(case when factory_id='bb' then sellingprice else 0 end)
from tb
group by account_id ,account_name
#2
select account_id, account_name,
sum(case factory_id when 'aa' then sellingprice end) as sellingprice_factory_aa,
sum(case factory_id when 'bb' then sellingprice end) as sellingprice_factory_bb,
sum(case factory_id when 'cc' then sellingprice end) as sellingprice_factory_cc
from 表格
group by account_id, account_name
#3
--SQL Server 2000
select account_id,account_name,
SUM(case when factory_id='aa' then sellingprice else 0 end) [aa],
SUM(case when factory_id='bb' then sellingprice else 0 end) [bb],
SUM(case when factory_id='cc' then sellingprice else 0 end) [cc]
from tab group by account_id,account_name
-- SQL Server 2005
select account_id,account_name,[aa],[bb],[cc]
from tab pivot (sum(sellingprice) for [factory_id] in([aa],[bb],[cc])) pvt
#4
SELECT account_id ,account_name ,
[aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[cc]=sum(case factory_id when 'bb' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id,account_name
#5
更正:
SELECT account_id ,account_name ,
[aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[cc]=sum(case factory_id when 'cc' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id,account_name
#6
多谢,另外,如果需要统计出所有客户的sellingprice该如何修改这个sql,例如除了account_name,sum(sellingprice_factory_aa),sum(sellingprice_factory_bb),sum(sellingprice_factory_cc) 还需要 sum(sellingprice_factory_aa_allaccounts), sum(sellingprice_factory_bb_allaccounts), sum(sellingprice_factory_cc_allaccounts).
#7
不能和在一起:(不知道我有没有理解lz的意思)
分两个查询:
1.
SELECT account_id ,account_name ,
[aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[cc]=sum(case factory_id when 'cc' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id,account_name
2.
SELECT account_id,
[All_aa]=sum(case factory_id when 'aa' then sellingprice else 0 end),
[All_bb]=sum(case factory_id when 'bb' then sellingprice else 0 end),
[All_cc]=sum(case factory_id when 'cc' then sellingprice else 0 end)
FROM TableName
GROUP BY account_id
#8
ding
#9
select
account_id, account_name,
sum(case factory_id when 'aa' then sellingprice end) as sellingprice_factory_aa,
sum(case factory_id when 'bb' then sellingprice end) as sellingprice_factory_bb,
sum(case factory_id when 'cc' then sellingprice end) as sellingprice_factory_cc
from
tb
group by
account_id, account_name
#10
行列转换
#11
学习了
#12
-- 不知道理解对否
create table tab
(account_id char(3),account_name varchar(10),sellingprice int,factory_id char(2));
insert into tab
select '001','stephe',200,'aa' union all
select '001','stephe',100,'bb' union all
select '002','adrian',200,'aa' union all
select '002','adrian',300,'bb' union all
select '002','adrian',100,'cc' union all
select '003','nweton',250,'aa' union all
select '003','nweton',300,'cc'
select account_id,account_name,[aa],[bb],[cc] from tab
pivot (sum(sellingprice) for [factory_id] in([aa],[bb],[cc])) pvt
union all
select 'sum_by_factory','',[aa],[bb],[cc]
from (select factory_id,sum(sellingprice) s
from tab group by factory_id) p pivot (max(s) for [factory_id] in([aa],[bb],[cc])) pvt
/*
002 adrian 200 300 100
003 nweton 250 NULL 300
001 stephe 200 100 NULL
sum_by_factory 650 400 400
*/
select account_id,account_name,[aa],[bb],[cc] from
(select account_id=ISNULL(account_id,'sum'),
account_name=(case when account_name is null and account_id is null then '' else account_name end),
SUM(case when factory_id='aa' then sellingprice else 0 end) [aa],
SUM(case when factory_id='bb' then sellingprice else 0 end) [bb],
SUM(case when factory_id='cc' then sellingprice else 0 end) [cc]
from tab group by account_id,account_name with rollup) t
where account_name is not null