按组显示特定日期的百分比 [英] Group by that shows the percentage in the specific date

查看:81
本文介绍了按组显示特定日期的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下列的表:

  StockID,FundID,DateID,ValueInDate 

我想创建一个GROUP BY查询,显示每个基金的百分比,从特定日期,无参数。



只有在此查询中有 @DateID p>

任何想法?

解决方案

您可能想要使用派生表不相关的子查询)来获取特定 @DateID 的值的总和。考虑下面的例子:

$ p $ SELECT t。*,(t.ValueInDate / dt.sum_value)* 100 as perc
FROM your_table t
JOIN(
SELECT SUM(ValueInDate)sum_value,DateId
FROM your_table
WHERE DateID = @DateID
GROUP BY DateId
)dt ON(dt.DateID = t.DateID)
WHERE t.DateID = @DateID;

测试用例:

  CREATE TABLE your_table(
StockID int,FundID int,DateID int,ValueInDate decimal(10,2)
);

插入your_table值(1,1,1,35);
INSERT INTO your_table VALUES(2,1,1,75);
INSERT INTO your_table VALUES(3,2,1,25);
INSERT INTO your_table VALUES(4,2,1,50);
INSERT INTO your_table VALUES(5,3,2,15);
INSERT INTO your_table VALUES(6,3,2,25);
INSERT INTO your_table VALUES(7,4,2,30);
INSERT INTO your_table VALUES(8,4,2,60);

@DateID = 1时的结果

  + --------- + -------- + ------- -  + ------------- + ----------- + 
| StockID | FundID | DateID | ValueInDate | perc |
+ --------- + -------- + -------- + ------------- + --- -------- +
| 1 | 1 | 1 | 35.00 | 18.918919 |
| 2 | 1 | 1 | 75.00 | 40.540541 |
| 3 | 2 | 1 | 25.00 | 13.513514 |
| 4 | 2 | 1 | 50.00 | 27.027027 |
+ --------- + -------- + -------- + ------------- + --- -------- +
4行(0.00秒)


I have a table with the following columns:

StockID,FundID,DateID,ValueInDate

I want to Create a GROUP BY Query that shows the Percentage Of every Fund From The Total Value in the specific Date, Without Parameters.

I have the @DateID Parameter only in this Query

Any ideas?

解决方案

You may want to use a derived table (uncorrelated subquery) to get the total of the values for a particular @DateID. Consider the following example:

SELECT  t.*, (t.ValueInDate / dt.sum_value) * 100 as perc
FROM    your_table t
JOIN    (
            SELECT   SUM(ValueInDate) sum_value, DateId
            FROM     your_table
            WHERE    DateID = @DateID
            GROUP BY DateId
        ) dt ON (dt.DateID = t.DateID)
WHERE   t.DateID = @DateID;

Test case:

CREATE TABLE your_table (
   StockID int, FundID int, DateID int, ValueInDate decimal(10,2)
);

INSERT INTO your_table VALUES (1, 1, 1, 35);
INSERT INTO your_table VALUES (2, 1, 1, 75);
INSERT INTO your_table VALUES (3, 2, 1, 25);
INSERT INTO your_table VALUES (4, 2, 1, 50);
INSERT INTO your_table VALUES (5, 3, 2, 15);
INSERT INTO your_table VALUES (6, 3, 2, 25);
INSERT INTO your_table VALUES (7, 4, 2, 30);
INSERT INTO your_table VALUES (8, 4, 2, 60);

Result when @DateID = 1:

+---------+--------+--------+-------------+-----------+
| StockID | FundID | DateID | ValueInDate | perc      |
+---------+--------+--------+-------------+-----------+
|       1 |      1 |      1 |       35.00 | 18.918919 |
|       2 |      1 |      1 |       75.00 | 40.540541 |
|       3 |      2 |      1 |       25.00 | 13.513514 |
|       4 |      2 |      1 |       50.00 | 27.027027 |
+---------+--------+--------+-------------+-----------+
4 rows in set (0.00 sec)

这篇关于按组显示特定日期的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆