我的问题正是我用于标题的问题。我有一个带有两个数据集的.rdl文件,这些数据集很难在一个脚本中合并,因此我想创建一个饼图,将它们作为单独的数据集使用。我需要找到添加两个东西的百分比(一个来自第一个数据集,一个来自第二个),所以基本上我需要这个添加然后像下一个那样的分区:
(first_thing + second_thing)/(总和(first_thing)+ SUM(second_thing))
例如我在一个表中有dog_sales而在另一个表中有cat_sales,我需要做一个饼图,其中包含本月狗和猫的销售百分比。
我该怎么做?
答案 0 :(得分:0)
我不知道在一个饼图中将两个数据集连接在一起的方法。相反,我建议将数据集合并到一个饼图可以使用的新数据集中。
按月使用cat vs dog sales的示例,我创建了一个如何将2个数据集连接在一起的示例。
您可以使用" total_sales"在最终查询中填充饼图值的列,您可以使用" month_of_sale"作为系列组。
-- Create temp table to store Cat sales data
DECLARE @catSales TABLE(
Sales INT
, MonthOfSale INT);
-- Create temp table to store Dog sales data
DECLARE @dogSales TABLE(
Sales INT
, MonthOfSale INT)
--Populate cat sales table with data
INSERT INTO @catSales
( Sales, MonthOfSale )
VALUES ( 50, -- Sales - int
1 -- MonthOfSale - int
)
INSERT INTO @catSales
( Sales, MonthOfSale )
VALUES ( 100, -- Sales - int
2 -- MonthOfSale - int
)
INSERT INTO @catSales
( Sales, MonthOfSale )
VALUES ( 75, -- Sales - int
3 -- MonthOfSale - int
)
--Populate dog sales table with data
INSERT INTO @dogSales
( Sales, MonthOfSale )
VALUES ( 150, -- Sales - int
1 -- MonthOfSale - int
)
INSERT INTO @dogSales
( Sales, MonthOfSale )
VALUES ( 80, -- Sales - int
3 -- MonthOfSale - int
)
INSERT INTO @dogSales
( Sales, MonthOfSale )
VALUES ( 200, -- Sales - int
4 -- MonthOfSale - int
)
--View data in cat sales table (note that months 1, 2, and 3 are all populated with data, but not month 4.
SELECT Sales
, MonthOfSale
FROM @catSales
--View data in dog sales table (note that months 1, 3, and 4 are all populated with data, but not month 2.
SELECT Sales
, MonthOfSale
FROM @dogSales
--Join the data from Cat and Dog sales together based on the month
SELECT cs.sales AS 'cat_sales'
, ds.sales AS 'dog_sales'
, ISNULL(cs.sales, 0) + ISNULL(ds.sales, 0) AS 'total_sales' -- Use ISNULL to convert missing data into 0 so that it adds correctly
, ISNULL(cs.MonthOfSale, ds.MonthOfSale) AS 'month_of_sale' -- If there are not cat sales, then the cs.MonthOfSale will be null and ds.MonthOfSale should be used instead
FROM @catSales cs
FULL OUTER JOIN @dogSales ds ON cs.MonthOfSale = ds.MonthOfSale -- Use full outer join to include all data from both tables