表(不设主键)结构如下所示:
商店名 商品名 销售额
第一百货 彩电 1600
第一百货 冰箱 1700
第一百货 照相机 1800
第二百货 彩电 1600
第二百货 冰箱 1700
第二百货 照相机 1800我现在使用sql语句进行查询得到如下结果:
商店名 销售额第1位商品名 销售额第2位商品名 1,2位销售额合计 合计值占总销售额的比例(四舍五入)
第一百货 照相机 冰箱 3500 69
第二百货 照相机 冰箱 3500 69问题是sql语句该怎么写,有请各位大大指教。谢谢。
商店名 商品名 销售额
第一百货 彩电 1600
第一百货 冰箱 1700
第一百货 照相机 1800
第二百货 彩电 1600
第二百货 冰箱 1700
第二百货 照相机 1800我现在使用sql语句进行查询得到如下结果:
商店名 销售额第1位商品名 销售额第2位商品名 1,2位销售额合计 合计值占总销售额的比例(四舍五入)
第一百货 照相机 冰箱 3500 69
第二百货 照相机 冰箱 3500 69问题是sql语句该怎么写,有请各位大大指教。谢谢。
解决方案 »
- sa登陆的简单问题
- 从vb中查询数据库太慢怎么办?
- 显示字段的问题(在线等待!!!!)
- 每天大概2000w数据, 数据还在增长, 6-7小时时间, sqlserver 设计需要注意什么.
- 请问,SQL7升级SQL2000后报ROOLBACK错误。。兼容性?
- 有谁想要分呀,有个简单的问题
- 机器先装了linux后又还原为98,再装2000就总提示0000001xxx等错说硬件有问题或设置
- 怎么去掉字段中的前两位?把8613499999999更新为13499999999
- 求sql 困惑了我好久,没人回答。分都散得差不多了
- 在什麼網站上有下載相關SQL SERVER的幫助軟件
- SQL 企业管理器不见
- 求问一个比较棘手的问题
if object_id('[tb]') is not null drop table [tb]
create table [tb] (商店名 varchar(8),商品名 varchar(6),销售额 int)
insert into [tb]
select '第一百货','彩电',1600 union all
select '第一百货','冰箱',1700 union all
select '第一百货','照相机',1800 union all
select '第二百货','彩电',1600 union all
select '第二百货','冰箱',1700 union all
select '第二百货','照相机',1800
go
select 商店名,销售额第1位商品名=max(case mc when 1 then 商品名 else null end),
销售额第2位商品名=max(case mc when 2 then 商品名 else null end),
[1,2位销售额合计]=sum(case when mc in(1,2) then 销售额 end),
合计值占总销售额的比例=cast(round(sum(case when mc in(1,2) then 销售额 end)*100.0/sum(销售额),0) as int)
from
(select *,mc=(select count(1) from tb where 商店名=a.商店名 and 销售额>=a.销售额) from [tb] a)a
group by 商店名
--结果:
商店名 销售额第1位商品名 销售额第2位商品名 1,2位销售额合计 合计值占总销售额的比例
-------- --------- --------- ----------- -----------
第二百货 照相机 冰箱 3500 69
第一百货 照相机 冰箱 3500 69
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([商店名] varchar(8),[商品名] varchar(6),[销售额] int)
insert [ta]
select '第一百货','彩电',1600 union all
select '第一百货','冰箱',1700 union all
select '第一百货','照相机',1800 union all
select '第二百货','彩电',1600 union all
select '第二百货','冰箱',1700 union all
select '第二百货','照相机',1800--------------------------------查询开始------------------------------
;with a as
(
select id=row_number() over(partition by[商店名] order by [销售额] desc),* from [ta]
)
,b as
(
select [商店名] ,
(select[商品名] from a where id=1 and [商店名]=t.[商店名])as [销售额第1位商品名] ,
(select[商品名] from a where id=2 and [商店名]=t.[商店名]) as[销售额第2位商品名],
sum([销售额]) as [1,2位销售额合计],
ceiling((select sum(t.[销售额])*100.00/ sum([销售额])from [ta] where [商店名]=t.[商店名] group by [商店名])) as[合计值占总销售额的比例]
from a as t where id<=2
group by [商店名]
)
select * from b
/*
商店名 销售额第1位商品名 销售额第2位商品名 1,2位销售额合计 合计值占总销售额的比例
-------- --------- --------- ----------- ---------------------------------------
第二百货 照相机 冰箱 3500 69
第一百货 照相机 冰箱 3500 69(2 行受影响)
*/
if object_id('[ta]') is not null drop table [ta]
go
create table [ta]([商店名] varchar(8),[商品名] varchar(6),[销售额] int)
insert [ta]
select '第一百货','彩电',1600 union all
select '第一百货','冰箱',1700 union all
select '第一百货','照相机',1800 union all
select '第二百货','彩电',1600 union all
select '第二百货','冰箱',1700 union all
select '第二百货','照相机',1800--------------------------------查询开始------------------------------
--2000
select [商店名] ,
max(case id when 1 then [商店名] else null end) as [销售额第1位商品名] ,
max(case id when 2 then [商店名] else null end) as [销售额第2位商品名],
sum([销售额]) as [1,2位销售额合计],
ceiling((select sum(t.[销售额])*100.00/ sum([销售额])from [ta] where [商店名]=t.[商店名] group by [商店名])) as[合计值占总销售额的比例]
from
(
select *,id=(select count(1)+1 from ta where [商店名]=a.[商店名] and [销售额]>a.[销售额] )from ta a
) t where id<=2
group by [商店名]
/*
商店名 销售额第1位商品名 销售额第2位商品名 1,2位销售额合计 合计值占总销售额的比例
-------- --------- --------- ----------- ---------------------------------------
第二百货 照相机 冰箱 3500 69
第一百货 照相机 冰箱 3500 69(2 行受影响)
*/