Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SergiyOsadchyy/cdb74a7599daa32530164685877e3e84 to your computer and use it in GitHub Desktop.
Save SergiyOsadchyy/cdb74a7599daa32530164685877e3e84 to your computer and use it in GitHub Desktop.
MS SQL get dates from previous week (DATEADD and DATEDIFF)
SELECT salesDate
, customerName
, productName
, salesQty
, salesQty * productPrice as 'Total'
FROM testDB.dbo.salesDaily
INNER JOIN testDB.dbo.customers ON testDB.dbo.customers.custId = customerId AND customerName = 'Customer2'
INNER JOIN testDB.dbo.products ON testDB.dbo.products.prodId = productId
WHERE salesDate >= DATEADD(WK, DATEDIFF(WK, 0, GETDATE()) - 1, 0)
AND salesDate < DATEADD(WK, DATEDIFF(WK, 0, GETDATE()), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment