我有一个表格,格式如下。
样本表和数据
declare @sales table
(productid int, TransDate date,SalesPerson varchar(100),Amount bigint)
insert into @sales
values
(1 , 20150101, 50 , 'S1'),
(1 , 20150201, 50 , 'S1'),
(1 , 20150301, 50 , 'S1'),
(1 , 20150401, 50 , 'S1'),
(1 , 20150501, 50 , 'S1'),
(1 , 20150601, 50 , 'S2'),
(1 , 20150701, 50 , 'S2'),
现在,我需要计算每个销售代表的总销售额和相关的销售额(由除主要销售人员以外的所有销售人员执行的销售)
例如, 对于销售人员S1,他的销售是250,而相关代理商(S2)的销售(也包括由S1为产品1服务的销售)将为100。
输出应为:
SalesPerson Date MySalesAmount RelatedSalesPersonSalesAmount
S1 20150101 50 0
S1 20150201 50 0
S1 20150301 50 0
S1 20150401 50 0
S1 20150501 50 0
S1 20150601 0 50
S1 20150701 0 50
S2 20150101 0 50
S2 20150201 0 50
S2 20150301 0 50
S2 20150401 0 50
S2 20150501 0 50
S2 20150601 50 0
S2 20150701 50 0
有人可以通过查询来帮助实现上述输出吗?
答案 0 :(得分:1)
这似乎很复杂。显然,销售人员的金额很容易:
select salesperson, sum(salesamt)
from t
group by salesperson;
对于相关金额,我们需要其他销售相同产品的销售人员的销售额。
我认为可以加入自我救助
select t.salesperson,
sum(case when t.salesperson = t2.salesperson then t.salesamnt end) as mysales,
sum(case when t.salesperson <> t2.salesperson then t.salesamnt end) as othersales
from t join
t t2
on t.product = t2.product
group by t.salesperson
答案 1 :(得分:0)
尝试一下-
declare @sales table
(productid int, transDate date, amount bigint, salesperson varchar(100))
insert into @sales
values
(1 , '20150101', 50 , 'S1'),
(1 , '20150201', 50 , 'S1'),
(1 , '20150301', 50 , 'S1'),
(1 , '20150401', 50 , 'S1'),
(1 , '20150501', 50 , 'S1'),
(1 , '20150601', 50 , 'S2'),
(1 , '20150701', 50 , 'S2')
;with cteSales as
(select s.salesperson, productid, sum(amount) amount
from @sales s
group by s.salesperson, productid)
select s1.salesperson, s1.productid,
sum(case when s1.salesperson = s2.salesperson then s1.amount else 0 end) as mysales,
sum(case when s1.salesperson <> s2.salesperson then s2.amount else 0 end) as othersales
from cteSales s1 inner join cteSales s2
on s1.productid = s2.productid
group by s1.salesperson, s1.productid
答案 2 :(得分:0)
检查以下查询:
declare @sales table
(productid int, TransDate date,SalesPerson varchar(100),Amount bigint);
insert into @sales(productid, TransDate, Amount, SalesPerson)
values
(1, '20150101', 50, 'S1'),
(1, '20150201', 50, 'S1'),
(1, '20150301', 50, 'S1'),
(1, '20150401', 50, 'S1'),
(1, '20150501', 50, 'S1'),
(1, '20150601', 50, 'S2'),
(1, '20150701', 50, 'S2'),
(1, '20150601', 10, 'S3'),
(1, '20150701', 10, 'S3');
select distinct
SalesPerson,
sum(Amount) over (partition by productid, SalesPerson) as MySalesAmount,
sum(Amount) over (partition by productid) -
sum(Amount) over (partition by productid, SalesPerson) as RelatedSalesPersonSalesAmount
from @sales;
输出:
+-------------+---------------+-------------------------------+
| SalesPerson | MySalesAmount | RelatedSalesPersonSalesAmount |
+-------------+---------------+-------------------------------+
| S1 | 250 | 120 |
| S2 | 100 | 270 |
| S3 | 20 | 350 |
+-------------+---------------+-------------------------------+
使用rextester在线进行测试。
更新:
select
x.productid,
x.SalesPerson,
y.TransDate as [Date],
iif(x.SalesPerson = y.SalesPerson, y.Amount, 0) as MySalesAmount,
iif(x.SalesPerson = y.SalesPerson, 0, y.Amount) as RelatedSalesPersonSalesAmount
from (select distinct productid, SalesPerson from @sales) as x
join @sales as y
on x.productid = y.productid
order by x.productid, x.SalesPerson;
输出:
+-----------+-------------+---------------------+---------------+-------------------------------+
| productid | SalesPerson | Date | MySalesAmount | RelatedSalesPersonSalesAmount |
+-----------+-------------+---------------------+---------------+-------------------------------+
| 1 | S1 | 01.01.2015 00:00:00 | 50 | 0 |
| 1 | S1 | 01.02.2015 00:00:00 | 50 | 0 |
| 1 | S1 | 01.03.2015 00:00:00 | 50 | 0 |
| 1 | S1 | 01.04.2015 00:00:00 | 50 | 0 |
| 1 | S1 | 01.05.2015 00:00:00 | 50 | 0 |
| 1 | S1 | 01.06.2015 00:00:00 | 0 | 50 |
| 1 | S1 | 01.07.2015 00:00:00 | 0 | 50 |
| 1 | S2 | 01.01.2015 00:00:00 | 0 | 50 |
| 1 | S2 | 01.02.2015 00:00:00 | 0 | 50 |
| 1 | S2 | 01.03.2015 00:00:00 | 0 | 50 |
| 1 | S2 | 01.04.2015 00:00:00 | 0 | 50 |
| 1 | S2 | 01.05.2015 00:00:00 | 0 | 50 |
| 1 | S2 | 01.06.2015 00:00:00 | 50 | 0 |
| 1 | S2 | 01.07.2015 00:00:00 | 50 | 0 |
+-----------+-------------+---------------------+---------------+-------------------------------+
答案 3 :(得分:0)
declare @sales table
(productid int, SalesAmt int , SalesPerson varchar(100))
insert into @sales
values
(1 , 50 , 'S1'),
(2 , 50 , 'S2'),
(1 , 100 , 'S2'),
(3, 100 , 'S1'),
(3 , 150 , 'S3'),
(2 , 100 , 'S3')
select t.salesperson, sum(SalesAmt) [Person Sale],
(select sum(SalesAmt) from @sales s where s.SalesPerson <> t.salesperson
) [Total Sale]
from @sales t
group by t.salesperson