将行除以列的总和以获得百分比

时间:2017-08-15 16:19:26

标签: sql sql-server tsql rows calculated-columns

我有下表:

Pets | Monday| Tuesday| Wednesday| Thursday| Friday
Dogs | 12    |  11    |    8     |     3   |   0
Cats | 16    |  13    |    5     |     8   |   4
Fish | 45    |  27    |    20    |     35  |   22
Birds| 3     |   3    |    3     |     3   |   2

我试图将每只宠物的百分比与最近一天的宠物总数相比较,所以返回的例子会是这样的:

Pets | Friday| Percentage
Dogs |  0    |     0%
Cats |  4    |     14.2%
Fish |  22   |     78%
Birds|  2    |     8.3%

我想每天都这样做,我已经设置了一个脚本,该脚本会在第二天(星期六)自动填写,因此新表格需要显示星期六的结果。

我正在考虑下面脚本的行,我需要抛出一些演员陈述等来让它返回一张桌子,但是想看看我是否在正确的路线上:

SELECT Friday / (SELECT SUM([Friday]) FROM PetSitting) from PetSitting group by pets

这里的难点在于让SQL识别一个行列然后将其除以相同的列以获得百分比,然后转到下一行并执行相同的操作等,直到获得所需结果的百分比列表为止上方。

3 个答案:

答案 0 :(得分:2)

听起来像一个简单的聚合。此外,添加更多列不会更改结果。为了清楚起见,我只是限制了样本数据。

declare @table table (Pets varchar(8), Friday int)
insert into @table
values
('Dogs',0),
('Cats',4),
('Fish',22),
('Birds',2)


select
    Pets
    ,Friday
    ,Percentage = convert(decimal(3,1),100 * (Friday / ((Sum(Friday) over (order by (select null))) * 1.0)))
from 
    @table
group by
    Pets
    ,Friday

<强>返回

+-------+--------+------------+
| Pets  | Friday | Percentage |
+-------+--------+------------+
| Birds |      2 |        7.1 |
| Cats  |      4 |       14.3 |
| Dogs  |      0 |        0.0 |
| Fish  |     22 |       78.6 |
+-------+--------+------------+

答案 1 :(得分:1)

我鼓励你看看你的桌子设计。我在这里给出一个例子,使用表变量而不是表(这样你就可以在查询窗口中运行它):

declare @pets table
(
Pet varchar(10),
SaleNumber int,
SaleDate date
)
INSERT INTO @pets VALUES
('Dogs', 12, '2017-08-07'),
('Dogs', 11, '2017-08-08'),
('Dogs', 8, '2017-08-09'),
('Dogs', 3, '2017-08-10'),
('Dogs', 0, '2017-08-11'),
('Cats', 16, '2017-08-07'),
('Cats', 13, '2017-08-08'),
('Cats', 5, '2017-08-09'),
('Cats', 8, '2017-08-10'),
('Cats', 4, '2017-08-11'),
('Fish', 45, '2017-08-07'),
('Fish', 27, '2017-08-08'),
('Fish', 20, '2017-08-09'),
('Fish', 35, '2017-08-10'),
('Fish', 22, '2017-08-11'),
('Birds', 3, '2017-08-07'),
('Birds', 3, '2017-08-08'),
('Birds', 3, '2017-08-09'),
('Birds', 3, '2017-08-10'),
('Birds', 2, '2017-08-11')

declare @lastdate date = (SELECT max(saledate) as lastdate FROM @pets)
declare @lastsum float = (SELECT sum(salenumber) as petsum FROM @pets 
WHERE saledate = @lastdate)

SELECT Pet, SaleNumber, Datename(dw,saledate) AS [Day of Week], 
salenumber / @lastsum as [Percentage of Day's Sales]
FROM @pets WHERE saledate = @lastdate

结果如下:

Pet SaleNumber  Day of Week Percentage of Day's Sales
Dogs    0   Friday  0
Cats    4   Friday  0.142857142857143
Fish    22  Friday  0.785714285714286
Birds   2   Friday  0.0714285714285714

像这样布置你的表意味着你可以每天简单地添加记录,而不必更改你的表格,上面的查询将自动获取最后几天的值。

答案 2 :(得分:0)

如果我理解你的问题,也许是一点点动态SQL

示例

Declare @Day varchar(50)='Friday'

Declare @SQL varchar(max) = '
Select Pets
      ,'+@Day+'     = sum('+@Day+') over (Partition By Pets)
      ,Percentage = format(sum('+@Day+'*1.0) over (Partition By Pets) / sum('+@Day+') over (),''0.#%'')
 From  YourTable
'
Exec(@SQL)

<强>返回

enter image description here