Select下的子查询 - 添加where子句以排除数据

时间:2013-03-05 21:30:26

标签: sql select subquery

您好我正在为我们的客户群提取卷信息,并希望为SELECT下的列[Volume2]编写子查询,以便在财政年度= 2013时提取卷信息。由于当前正在编写查询,因此目前包含所有卷对于财政年度> = 2012年WHERE子句,但不是在那里更改它,我想为SELECT下的[Volume2]列指定它。因此,[Volume1]列将包括2012财年和2013财年的数量,[Volume2]列仅包括2013年的数量。

任何帮助表示赞赏!谢谢!

SELECT 

sum([FactActualDetail].[Volume]) AS [Volume1],
sum([FactActualDetail].[Volume]) AS [Volume2],
sum([FactActualDetail].[AdjGrossMarginAmount]) AS [AdjGrossMarginAmount],
left([FactActualDetail].[SourceCustomerProductCode],7) as [Acct #],
[DimDate].[Fiscal_Year] as [Fiscal_Year],
[DimCustomer].[TerrSalesManagerName] AS [TerrSalesManagerName],
[DimCustomer].[AccountRepsalesmanName] AS [AccountRepsalesmanName]

FROM [dbo].[FactActualDetail] [FactActualDetail] 

LEFT JOIN [dbo].[DimCustomer] [DimCustomer] ON ([FactActualDetail].[CustomerSK] = [DimCustomer].[CustomerSK])
LEFT JOIN [Common].[DimDate] [DimDate] ON ([FactActualDetail].[DeliveryDateSK] = [DimDate].[DateSK])

WHERE [EQMultiplier] > 0 AND [Fiscal_Year] >= 2012 AND ([SuperChannelCode] = 04 OR [SuperChannelCode] = 06 OR [SuperChannelCode] = 07) AND (substring([SourceCustomerProductCode],8,3) = 102) 

GROUP BY left([FactActualDetail].[SourceCustomerProductCode],7),[TerrSalesManagerName],[AccountRepsalesmanName],[Fiscal_Year]

1 个答案:

答案 0 :(得分:0)

使用

SUM(CASE WHEN Fiscal_Year = 2013 THEN FactActualDetail.Volume
         ELSE 0 END) AS Volume2

而不仅仅是

SUM(FactActualDetail.Volume) AS volume2
在您的查询中