Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created December 25, 2016 12:48
Show Gist options
  • Save Pzixel/a0697c05814d1a007273c6a35059c492 to your computer and use it in GitHub Desktop.
Save Pzixel/a0697c05814d1a007273c6a35059c492 to your computer and use it in GitHub Desktop.
Returns all products and count of being first customer order for each of these products
WITH CTE AS
(
SELECT ProductId, [CustomerOrderNumber] = ROW_NUMBER() OVER(PARTITION BY [CustomerId] ORDER BY [DateCreated])
FROM [Sales]
)
SELECT ProductId, SUM(IIF(CTE.CustomerOrderNumber = 1, 1, 0))
FROM CTE
GROUP BY ProductId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment