处理日期和总结行

时间:2014-06-18 05:03:59

标签: sql sql-server

我希望生成的表格与我提供的示例表格相似,即使提供的唯一月份是5,我希望表格显示所有12个月,即使其他月份的金额为0。

我正在使用此查询,让我尝试在数量方面准确解释我想要的内容。我希望金额列包含相应月份的每笔销售总额。

Select   
    YEAR([Date]) as [Year],
    MONTH([Date]) as [Month],
    IsNull(SUM(Amount), 0) AS TotalSales
From Sales Left Outer Join Employee
On Employee.EmployeeID = Sales.EmployeeID
    Group By YEAR([Date]), MONTH([Date])
    Order By YEAR([Date]), MONTH([Date])

员工表,

+--+--+------+
|ID|  Name   |
+--+---------+
|1 |John Doe |
+--+---------+
|2 |Jane Doe |
+--+---------+

销售表,

+--+------+---------+-------+--------+
|ID|SaleID| Date    |Amount |Quantity|
+--+------+---------+-------+--------+
|1 |  1   |5-14-2014|300    |12      |
+--+------+---------+-------+--------+
|1 |  2   |5-16-2014|600    |4       |
+--+------+---------+-------+--------+
|2 |  3   |5-14-2014|452    |10      |
+--+------+---------+-------+--------+
|2 |  4   |5-16-2014|356    |2       |
+--+------+---------+-------+--------+

我想要的,

+--+---------+-----+------+--------+--------+
|ID|  Name   |Year |Month |Amount  |Quantity|
+--+---------+-----+------+--------+--------+
|1 |John Doe |2014 |5     |900     |16      |
+--+---------+-----+------+--------+--------+
|2 |Jane Doe |2014 |5     |808     |12      |
+--+---------+-----+------+--------+--------+

3 个答案:

答案 0 :(得分:0)

试试这个

Select S.ID,E.Name,YEAR(MAX([Date])) as [Year],MONTH(MAX([Date])) as [Month],
      IsNull(SUM(Amount), 0) AS TotalSales,IsNull(SUM(Quantity), 0) AS TotalQuantity
From Sales S Left Join Employee E On E.ID = S.ID
Group By S.ID,E.Name,YEAR([Date]), MONTH([Date])
Order By S.ID

<强> FIDDLE DEMO

输出:


+--+---------+-----+------+--------+--------+
|ID|  Name   |Year |Month |Amount  |Quantity|
+--+---------+-----+------+--------+--------+
|1 |John Doe |2014 |5     |900     |16      |
+--+---------+-----+------+--------+--------+
|2 |Jane Doe |2014 |5     |808     |12      |
+--+---------+-----+------+--------+--------+

<强>更新

如果您想显示月份(1-12),即使该月份的数量为0

试试这个

;With Months (Month,MonthID) 
AS
(

    select 'January' as Month , 1 as MonthID
    UNION
    select 'February' as Month , 2 as MonthID
    UNION
    select 'March' as Month , 3 as MonthID
    UNION
    select 'April' as Month , 4 as MonthID
    UNION
    select 'May' as Month , 5 as MonthID
    UNION
    select 'June' as Month , 6 as MonthID
    UNION
    select 'July' as Month , 7 as MonthID
    UNION
    select 'August' as Month , 8 as MonthID
    UNION
    select 'September' as Month , 9 as MonthID
    UNION
    select 'October' as Month , 10 as MonthID
    UNION
    select 'November' as Month , 11 as MonthID
    UNION
    select 'December' as Month , 12 as MonthID

)

SELECT R.ID,R.Name,R.[Year],M.[MonthID],M.[Month],R.TotalSales,R.TotalQUANTITY FROM
Months M LEFT JOIN
(
Select S.ID,E.Name,YEAR([Date]) as [Year],MONTH([Date]) as [Month],
      IsNull(SUM(Amount), 0) AS TotalSales,IsNull(SUM(Quantity), 0) AS TotalQuantity
From Sales S Left Join Employee E On E.ID = S.ID
Left OUTER JOIN Months ON MonthID = MONTH([Date])
Group By S.ID,E.Name,YEAR([Date]), MONTH([Date])
) R ON R.Month = M.MonthID
Order By CASE WHEN R.ID IS NULL THEN 1 ELSE 0 End,R.ID,M.MonthID

<强> FIDDLE DEMO

答案 1 :(得分:0)

SELECT e.ID,
       e.Name,
       YEAR(s.date)AS Year,
       MONTH(s.date)AS Month,
       sum(s.amount) AS Amount,
       sum(s.quantity) AS Quantity
FROM Employee e
LEFT JOIN Sales s ON e.id=s.id
GROUP BY e.ID,e.Name,year(s.date),month(s.date)

答案 2 :(得分:0)

试试这个!

select ID,Name,yaer,monht,Amount,quantity from
(
select distinct Year(Date1) as yaer,Month(Date1) as monht,rn=ROW_NUMBER()over(order by Year(Date1)) from #emp
)x,
(
select a.ID,a.Name,sum(b.Amount) as Amount,sum(b.Quantity) as quantity,rn1=row_number()over(order by a.ID) from #sales a join #emp b on a.ID=b.ID
group by a.ID,a.Name
)y
where x.rn=y.rn1

Demo