Skip to content

Instantly share code, notes, and snippets.

@cdeutsch
Created September 30, 2013 19:10
Show Gist options
  • Save cdeutsch/6768605 to your computer and use it in GitHub Desktop.
Save cdeutsch/6768605 to your computer and use it in GitHub Desktop.
SELECT ISNULL(NULLIF(ca.Value, ''), cc.Email) AS 'Company',
SUM([OrderTotal]) as Total,
SUM([OrderTotal]-RefundedAmount) as AdjustedTotal
FROM [dbo].[Nop_Order] oo
INNER JOIN Nop_Customer cc on oo.CustomerID = cc.CustomerID
LEFT OUTER JOIN Nop_CustomerAttribute ca on cc.CustomerId = ca.CustomerId and ca.[Key] = 'Company'
WHERE RefundedAmount < OrderTotal
AND OrderTotal > 0
GROUP BY ISNULL(NULLIF(ca.Value, ''), cc.Email)
ORDER BY AdjustedTotal DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment