2014-06-18 39 views
0

我希望生成的表格類似於我提供的示例表,即使提供的唯一月份是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]) 

Employee表,

+--+--+------+ 
|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  | 
+--+---------+-----+------+--------+--------+ 

回答

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

+0

似乎是工作:)不過關於我的其它要求,我想對結果顯示所有個月,即使他們沒有爲出售日上市。 – Prosperity

+0

您可以編輯您的問題,並提供樣品輸出@Prosperity –

+0

基本上與您的輸出,但本月列中,將顯示該月(1-12),即使該月將有0 – Prosperity

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) 
+0

我試過這個,但它給了我一個錯誤,因爲e.id和e.name需要無論是一個聚合函數或在GROUP BY – Prosperity

+0

你得到什麼錯誤請註明這裏,所以我可以幫你 – Sadikhasan

+0

@Sadikhasan你必須包括''爲集團e.name' by'條款 –

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