我如何才能做到这一点,所以我的运行总行采用百分比格式,其中100.00000等于100%,依此类推

时间:2019-05-01 04:13:22

标签: mysql sql mysql-workbench

select gla.account_number, gla.account_description,   
line_item_description as expense, ili.line_item_amount as cost, 

下面的那一行是我遇到麻烦的地方,我该怎么做,以便当我将line_item_amount除以line_item_amount / 100的总和时,它将输出为百分比格式。 >

sum(line_item_amount) over (order by line_item_amount desc) as RunningTotal,
line_item_amount / sum(line_item_amount / 100) over (order by 
line_item_amount desc) as percent

from
invoice_line_items ili
join general_ledger_accounts gla
on ili.account_number = gla.account_number

where account_description not like 'freight' and account_description not 
like 'telephone'

order by cost desc  

它应该是这样的: enter image description here

这就是我的样子 enter image description here

1 个答案:

答案 0 :(得分:0)

尝试乘以100.0,也可以使用ROUND

SELECT
    SUM(line_item_amount) OVER (ORDER BY line_item_amount DESC) AS RunningTotal,
    ROUND(100.0 * line_item_amount /
        SUM(line_item_amount) OVER (ORDER BY line_item_amount DESC), 2) AS percent
FROM invoice_line_items ili
INNER JOIN general_ledger_accounts gla
    ON ili.account_number = gla.account_number
WHERE
    account_description <> 'freight' AND account_description <> 'telephone'
ORDER BY
   cost DESC;

注意:

我用常规不等式替换了LIKE子句中的WHERE表达式,因为您没有使用%通配符。上面的版本与您拥有的逻辑相同,尽管也许您打算使用WHERE account_description NOT LIKE '%freight%'的形式。如果是这样,请根据需要编辑我的代码。