简体   繁体   English

如何计算具有不同条件的同一列的不同值的百分比

[英]How to calculate a percentage on different values from same column with different criteria

I'm trying to write a query in SSRS (using SQL) to calculate an income statement percentage of sales for each month (the year is a parameter chosen by the user at runtime).我正在尝试在 SSRS 中编写一个查询(使用 SQL)来计算每个月的损益表销售额百分比(年份是用户在运行时选择的参数)。 However, the table I have to use for the data lists all of the years, months, accounts, dollars, etc together and looks like this:但是,我必须用于数据的表格列出了所有年、月、帐户、美元等,如下所示:

ACCT_YEAR ACCT_YEAR ACCT_PERIOD ACCT_PERIOD ACCOUNT_ID帐户ID CREDIT_AMOUNT CREDIT_AMOUNT
2021 2021年 1 1个 4000 4000 20000 20000
2021 2021年 2 2个 4000 4000 25000 25000
2021 2021年 1 1个 5000 5000 5000 5000
2021 2021年 2 2个 5000 5000 7500 7500
2021 2021年 1 1个 6000 6000 4000 4000
2021 2021年 2 2个 6000 6000 8000 8000

etc, etc (ACCOUNT_ID =4000 happens to be the sales account)等等等等(ACCOUNT_ID =4000恰好是销售账号)

As an example, I need to calculate例如,我需要计算

CREDIT_AMOUNT when ACCT_YEAR = 2021, ACCT_PERIOD=1, and ACCOUNT_ID=5000
/
CREDIT_AMOUNT when ACCT_YEAR = 2021, ACCT_PERIOD=1, and ACCOUNT_ID=4000

* 100

I would then do that for each ACCT_PERIOD in the ACCT_YEAR.然后,我将为 ACCT_YEAR 中的每个 ACCT_PERIOD 执行此操作。 Hope that makes sense...What I want would look like this:希望这是有道理的......我想要的是这样的:

ACCT_YEAR ACCT_YEAR ACCT_PERIOD ACCT_PERIOD ACCOUNT_ID帐户ID PERCENTAGE百分比
2021 2021年 1 1个 5000 5000 0.25 0.25
2021 2021年 2 2个 5000 5000 0.30 0.30
2021 2021年 1 1个 6000 6000 0.20 0.20
2021 2021年 2 2个 6000 6000 0.32 0.32

I'm trying to create a graph that shows the percentage of sales of roughly 10 different accounts (I know their specific account_ID's and will filter by those ID's) and use the line chart widget to show the trends by month.我正在尝试创建一个图表来显示大约 10 个不同帐户的销售额百分比(我知道他们的特定 account_ID 并将按这些 ID 进行过滤)并使用折线图小部件按月显示趋势。
I've tried CASE scenarios, OVER scenarios, and nested subqueries.我尝试过 CASE 场景、OVER 场景和嵌套子查询。 I feel like this should be simple but I'm being hardheaded and not seeing the obvious solution.我觉得这应该很简单,但我很冷静,没有看到明显的解决方案。 Any suggestions?有什么建议么? Thank you!谢谢!

You just to use a matrix with a parent column group for ACCT_YEAR and a child column group for ACCT_PERIOD.您只需使用一个矩阵,其中父列组用于 ACCT_YEAR,子列组用于 ACCT_PERIOD。 Then you can use your calculation.然后你可以使用你的计算。 If you format the textbox for percentage, you won't need to multiply it by 100.如果将文本框格式化为百分比,则不需要将其乘以 100。

Textbox value: =IIF(ACCOUNT_ID=4000, Sum(CREDIT_AMOUNT), 0) = 0, 0, IIF(ACCOUNT_ID=5000, Sum(CREDIT_AMOUNT), 0) / IIF(ACCOUNT_ID=4000, Sum(CREDIT_AMOUNT), 0)文本框值: =IIF(ACCOUNT_ID=4000, Sum(CREDIT_AMOUNT), 0) = 0, 0, IIF(ACCOUNT_ID=5000, Sum(CREDIT_AMOUNT), 0) / IIF(ACCOUNT_ID=4000, Sum(CREDIT_AMOUNT), 0)

One important behaviour to note is that window functions are applied after the where clause.需要注意的一个重要行为是 window 函数在 where 子句之后应用。

Because you need the window functions to be applied before any where clause (which would filter account 4000 out) , they need to be used in one scope, and the where clause in another scope.因为您需要在任何 where 子句(将过滤掉帐户 4000)之前应用 window 函数,所以需要在一个 scope 中使用它们,而在另一个 scope 中使用 where 子句。

WITH
  perc AS
(
  SELECT
    *,
    credit_amount * 100.0
    /
    SUM(
      CASE WHEN account_id = 4000 THEN credit_amount END
    )
    OVER (
      PARTITION BY acct_year, accr_period
    )
      AS credit_percentage
  FROM
    your_table
)
SELECT
  *
FROM
  perc
WHERE
  account_id IN (5000,6000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM