SQL - 获取2期内的销售数据

时间:2009-12-03 15:00:25

标签: sql

我有两个表:一个包含员工信息,另一个是交易(销售)信息,包括销售人员(员工ID)

表1:员工,代码名称......

表2:销售额,employee_code,产品,日期,价格,金额

我想计算每个员工每2个时期(1月1日至3月30日和7月1日至12月31日)或任何时间段(每个时期)可以产生多少收入(总金额) - 如下:

姓名_________Period1_1_30_6______期间1_7_31_12

亚当__________________ 50B $ ______________ 70B $

David _________________ 90b $ ______________ 1000b $ ....

3 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情

DECLARE @Employees TABLE(
        EmpCode INT,
        EmpName VARCHAR(50)
)

INSERT INTO @Employees (EmpCode,EmpName) SELECT 1, 'Adam'
INSERT INTO @Employees (EmpCode,EmpName) SELECT 2, 'David'

DECLARE @sales TABLE(
        EmpCode INT,
        product VARCHAR(50),
        Date DATETIME, 
        Price FLOAT, 
        Amount FLOAT
)

INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 1, 'A', '01 Jan 2009', 5, 10
INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 1, 'A', '01 Mar 2009', 5, 10
INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 1, 'A', '01 May 2009', 5, 10
INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 1, 'A', '01 Jul 2009', 5, 10
INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 1, 'A', '01 Sep 2009', 5, 10

INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 2, 'A', '01 Jan 2009', 5, 10
INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 2, 'A', '01 May 2009', 5, 10
INSERT INTO @sales (EmpCode,product,Date,Price,Amount) SELECT 2, 'A', '01 Sep 2009', 5, 10

DECLARE @Period1Start DATETIME,
        @Period1End DATETIME,
        @Period2Start DATETIME,
        @Period2End DATETIME

SELECT  @Period1Start = '01 Jan 2009',
        @Period1End = '30 Jun 2009',
        @Period2Start = '01 Jul 2009',
        @Period2End = '31 Dec 2009'


SELECT  e.EmpName,
        Totals.Period1,
        Totals.Period2
FROM    @Employees e INNER JOIN
        (
            SELECT  EmpCode,
                    SUM(CASE WHEN Date BETWEEN @Period1Start AND @Period1End THEN Price * Amount ELSE 0 END) Period1,
                    SUM(CASE WHEN Date BETWEEN @Period2Start AND @Period2End THEN Price * Amount ELSE 0 END) Period2
            FROM    @sales
            GROUP BY EmpCode
        ) Totals ON e.EmpCode = Totals.EmpCode

答案 1 :(得分:0)

使用子查询:

select employee.name, (select sum(amount) from sales where date between <date> and <date>) as <month1>, (select sum(amount) from sales where date between <date> and <date>) as <month1>
from employee order by employee.name

答案 2 :(得分:0)

对旁观者的解决方案略有改变:

SELECT
     E.name,
     SUM(
          CASE
               WHEN S.sale_date BETWEEN @period_1_start AND @period_1_end THEN S.[amount]
               ELSE 0
          END) AS period_1_sales,
     SUM(
          CASE
               WHEN S.sale_date BETWEEN @period_2_start AND @period_2_end THEN S.[amount]
               ELSE 0
          END) AS period_2_sales
FROM
     Employees E
LEFT OUTER JOIN Sales S ON
     S.employee_code = E.employee_code AND
     (
          S.sale_date BETWEEN @period_1_start AND @period_1_end OR
          S.sale_date BETWEEN @period_2_start AND @period_2_end
     )
GROUP BY
     E.name