--这个是待查询表
select 10 as 金额,'美元' 币种,12 房间
union all
select 20,'港币',12
union all
select 40,'美元',11
union all
select 10,'人民币',11
union all
select 10,'人民币',11
union all
select 10,'人民币',12--这个是查询结果表
select 11 房间,20 人民币,40 美元,0 港币
union all
select 12 房间,10 人民币,10 美元,20 港币请问用一条SQL实现按照房间group by 然后分别汇总 人民币 美元 港币
只能用一条SQL。 其中 三个币种是固定的。不会有新的币种出来。
select 10 as 金额,'美元' 币种,12 房间
union all
select 20,'港币',12
union all
select 40,'美元',11
union all
select 10,'人民币',11
union all
select 10,'人民币',11
union all
select 10,'人民币',12--这个是查询结果表
select 11 房间,20 人民币,40 美元,0 港币
union all
select 12 房间,10 人民币,10 美元,20 港币请问用一条SQL实现按照房间group by 然后分别汇总 人民币 美元 港币
只能用一条SQL。 其中 三个币种是固定的。不会有新的币种出来。
解决方案 »
- 不允许启动新请求,因为该请求应具有有效的事务描述。
- 积分转置为置顶天数后 如何恢复正常 架构咨询?
- 我的理解是否正确:Sql-server同步后,发布服务器可以有选择的发布某些数据,订阅服务器只能被动接受发布服务器的数据?不能把发布的数据
- 如何一次性將一個數據庫的char字段改成nchar字段?
- 讨论:对于两个都有上万行的两个表,两个表中存在关联,你是建立视图还是让数据冗余来显示?
- 江湖急救
- 在存储过程中参数里包含语句的错误,请教各位大侠。
- 请问还有没有关于SQL的论坛啊
- Sql Server 中奇怪的问题???注释也能被编译。。。
- 求助!请问在SQL中如果字段名用了“()”表示某单位如平方米,应该如何让SQL知道这是字段名的一部分?不然它老说找不到我的定义的字段。
- 菜鸟求救!修改IDENTITY的触发器,谢谢各位大侠
- 数据库如何同步?
(
select 10 as 金额,'美元' 币种,12 房间
union all
select 20,'港币',12
union all
select 40,'美元',11
union all
select 10,'人民币',11
union all
select 10,'人民币',11
union all
select 10,'人民币',12
) t
pivot
(sum(金额) for 币种 in (人民币,美元,港币)) p
if object_id('[TB]') is not null
drop table [TB]---->建表
create table [TB]([金额] int,[币种] varchar(48),[房间] varchar(78))
insert [TB]
select 10 ,'美元',12
union all
select 20,'港币',12
union all
select 40,'美元',11
union all
select 10,'人民币',11
union all
select 10,'人民币',11
union all
select 10,'人民币',12--> 查询结果
SELECT 房间,
SUM(case 币种 when '人民币' then 金额 else 0 end )as 人民币,
SUM(case 币种 when '美元' then 金额 else 0 end )as 美元,
SUM(case 币种 when '港币' then 金额 else 0 end )as 港币
FROM [TB]
group by 房间