Question

I'm trying to return a list of accounts that are selling a particular product (SourceCustomerProductCode = 102) and want to see accounts that have sold this product in year 2013, but if they sold it in 2012, they I want them excluded from my query (i.e. I only want to return NEW customers). My below query is returning accounts that sold this product in 2013, but includes accounts that sold it in 2012.

I know I can write this under the SELECT area using a case when for the [CaseAndGalloneVolume] statement, but need to write this statement in the where clause, because eventually I am going to add other products to my table.

Thanks for any help!

SELECT 

sum([FactActualDetail].[CaseAndGallonVolume]) AS [CaseAndGallonVolume],
sum([FactActualDetail].[AdjGrossMarginAmount]) AS [AdjGrossMarginAmount],
left([FactActualDetail].[SourceCustomerProductCode],7) as [Acct #]

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 [Fiscal_Year] = 2013 AND [EQMultiplier] > 0 AND ([SuperChannelCode] = 04 OR [SuperChannelCode] = 06 OR [SuperChannelCode] = 07) AND (substring([SourceCustomerProductCode],8,3) = 102) 

GROUP BY left([FactActualDetail].[SourceCustomerProductCode],7)
Was it helpful?

Solution

try placing

HAVING MIN([Fiscal_Year]) = 2013

at the end of the query.

OTHER TIPS

Add a new aggregation column:

sum(case when year = 2013 then 1 else 0 end) as invalidItemCount

and filter on it:

having invalidItemCount = 0

This works for arbitrary filter expressions, not just for just the year 2013.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top