计算重量排名前三与剩余的所占总重量的百分比 sql

ling201003 2010-06-02 09:48:33
数据如下:
ID VID Weight
1 3 20
2 2 30
3 3 34
4 1 40
5 4 21
6 5 24
7 1 50
8 7 81
想要得到的结果:
VID Percentage
1 30%
7 27%
3 18%
其它 25%

sql要怎么写?
...全文
201 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ling201003 2010-06-02
  • 打赏
  • 举报
回复
感激。。各位大虾们。。
ling201003 2010-06-02
  • 打赏
  • 举报
回复
一楼的 树上的鸟儿 谢啦
永生天地 2010-06-02
  • 打赏
  • 举报
回复

create table tb (id int,vid int,weight int)
insert tb
select 1,3,20 union all
select 2,2,30 union all
select 3,3,34 union all
select 4,1,40 union all
select 5,4,21 union all
select 6,5,24 union all
select 7,1,50 union all
select 8,7,81



select * from(
select top 3 ltrim(vid) vid, left(sum(Weight)*100.0/hj,2)+'%' Percentage
from tb t1,(select sum(Weight) as hj from tb) t2
group by vid,hj
order by sum(Weight) desc
)t
union all

select '其他',left(100-
(select sum(s) from(
select top 3 vid, sum(Weight) as s
from tb t1,(select sum(Weight) as hj from tb) t2
group by vid,hj
order by sum(Weight) desc
)t) *100.0/(select sum(Weight) as hj from tb),2)+'%'

/*
vid Percentage
------------ ----------
1 30%
7 27%
3 18%
其他 25%

(所影响的行数为 4 行)

*/
黄_瓜 2010-06-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 beirut 的回复:]

SQL code
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[VID] int,[Weight] int)
insert [tb]
select 1,3,20 union all
select 2,2,30 union all
select 3,3,3……
[/Quote]
修改


--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[VID] int,[Weight] int)
insert [tb]
select 1,3,20 union all
select 2,2,30 union all
select 3,3,34 union all
select 4,1,40 union all
select 5,4,21 union all
select 6,5,24 union all
select 7,1,50 union all
select 8,7,81

--------------------------------查询开始------------------------------
select [VID],Percentage from
(
select top 3 [VID]=ltrim([VID]),Percentage=ltrim(cast(sum([Weight])*100.0/(select sum([Weight]) from [tb]) as decimal(9,2)))+'%'
from [tb]
group by [VID]
order by sum([Weight]) desc
) t
union all
select '其它',ltrim(cast(
(
(select sum([Weight]) from [tb])-
(select sum ([Weight]) from(select top 3 [Weight]=sum([Weight]) from [tb] group by [VID] order by sum([Weight])desc)t))*100.0/
(select sum([Weight]) from [tb]) as decimal(9,2)))+'%'

/*
VID Percentage
------------ ------------------------------------------
1 30.00%
7 27.00%
3 18.00%
其它 25.00%

(4 行受影响)

*/
htl258_Tony 2010-06-02
  • 打赏
  • 举报
回复
--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([ID] [int],[VID] [int],[Weight] [int])
INSERT INTO [tb]
SELECT '1','3','20' UNION ALL
SELECT '2','2','30' UNION ALL
SELECT '3','3','34' UNION ALL
SELECT '4','1','40' UNION ALL
SELECT '5','4','21' UNION ALL
SELECT '6','5','24' UNION ALL
SELECT '7','1','50' UNION ALL
SELECT '8','7','81'


-->SQL查询如下:
;WITH t AS
(
SELECT TOP 3 LTRIM(VID) VID,SUM([Weight]) AS sumWeight
FROM [tb]
GROUP BY VID
ORDER BY sumWeight DESC
),
t1 AS
(
SELECT '其它' AS VID,sum(sumWeight) sumWeight
FROM (
SELECT VID,SUM([Weight]) AS sumWeight
FROM [tb]
GROUP BY VID
EXCEPT
SELECT * FROM t
) t1
)
SELECT VID,ltrim(cast(sumWeight*100./(SELECT SUM([Weight]) FROM tb) AS dec(9,2)))+'%' AS 百分比
FROM (
SELECT * FROM t
UNION ALL
SELECT * FROM t1
) AS t
/*
VID 百分比
------------ ------------------------------------------
1 30.00%
7 27.00%
3 18.00%
其它 25.00%

(4 行受影响)
*/
黄_瓜 2010-06-02
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[VID] int,[Weight] int)
insert [tb]
select 1,3,20 union all
select 2,2,30 union all
select 3,3,34 union all
select 4,1,40 union all
select 5,4,21 union all
select 6,5,24 union all
select 7,1,50 union all
select 8,7,81

--------------------------------查询开始------------------------------

select top 3 [VID],Percentage=ltrim(cast(sum([Weight])*100.0/(select sum([Weight]) from [tb]) as decimal(9,2)))+'%'
from [tb]
group by [VID]
order by [VID]
/*
VID Percentage
----------- ------------------------------------------
1 30.00%
2 10.00%
3 18.00%

(3 行受影响)
*/
chuifengde 2010-06-02
  • 打赏
  • 举报
回复
declare @a table(ID INT, VID INT, WEIGHT INT)
insert @a select 1, 3, 20
union all select 2, 2, 30
union all select 3, 3, 34
union all select 4, 1, 40
union all select 5, 4, 21
union all select 6, 5, 24
union all select 7, 1, 50
union all select 8, 7, 81

DECLARE @b TABLE(a int,b DECIMAL(20,2))
INSERT @b SELECT TOP 3 vid,SUM(WEIGHT)*1.0/(SELECT SUM(WEIGHT ) FROM @a )
FROM @a GROUP BY vid ORDER BY 2 desc

SELECT ltrim(a) VID ,ltrim(b*100)+'%' Percentage FROM @b
UNION ALL
SELECT '其它',ltrim((1-sum(b))*100)+'%' from @b

--result
/*VID Percentage
------------ ------------------------------
1 30.00%
7 27.00%
3 18.00%
其它 25.00%

(所影响的行数为 4 行)*/
PowerBI系列课程之内置视觉对象和常用自定义视觉对象专题 内置视觉对象Stacked bar chart  堆积条形图Stacked column chart 堆积柱形图 - X轴按类别显示Clustered bar chart  簇状条形图Clustered column chart  簇状柱形图100% Stacked bar chart 百分比堆积条形图100% Stacked column chart 百分比堆积柱形图Line Chart  折线图-预测功能Area Chart  面积图Stacked Area Chart  堆积面积图Line and stacked column chart 折线和堆积柱形图-双Y轴Line and clustered column chart 折线和簇状柱形图Ribbon Chart  丝带图Waterfall Chart  瀑布图Funnel  漏斗图Scatter chart  散点图Pie Chart  饼图 Donut Chart 环形图TreeMap 树状图Map  地图 Filled Map 着色地图Shape Map 形状地图Gauge 仪表Card 卡片图Multi-row card 多行卡片KPI  Table 表格-条件样式Matrix 矩阵详解  Key influencers  关键影响者Decomposition tree 分解树Q&A 问答2021.6月增加分页表格视觉对象自定义视觉对象视觉对象使用建议下载排名20,免费实用的视觉对象 AllDemo pbix介绍 Pareto 帕累托图DrillDown Donut  可钻取饼图Word Cloud   文字云Gantt  甘特图Infographic Designer  信息柱状图Timeline Slicer  时间轴切片器Chiclet Slicer 图片切片器Text Filter 文本筛选器HierarchySlicer  层级切片器Pulse Chart脉动图Power KPI Matrix  KPI矩阵Animated Bar Chart Race 动态条形图Advance Card  高级卡片Sankey Chart  桑基图Radar Chart   雷达图Dial Gauge  码表Waffle 华夫比图Quadrant 象限图 Tornado Chart  龙卷风图Histogram Chart  直方图 Box and Whisker chart 盒线图Sunburst  阳光图Chord Chart 和弦图Bullet Chart  子弹图HTML Content  html解析视觉对象  

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧