Last active
July 10, 2024 17:19
-
-
Save evansekeful/a6890cf673919a4f12b395c1d8b6bb78 to your computer and use it in GitHub Desktop.
SQL Server Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Find duplicate SKUs in a table | |
SELECT [SKU], COUNT([SKU]) AS NumOccurences | |
FROM [DB].[dbo].[TABLE] | |
Group by [SKU] | |
HAVING ( Count([SKU]) > 1) | |
-- Find duplicate SKUs in a table, return with other columns | |
SELECT * | |
FROM [DB].[dbo].[TABLE] | |
WHERE [SKU] IN | |
( SELECT [SKU] | |
FROM [DB].[dbo].[TABLE] | |
Group by [SKU] | |
HAVING ( Count([SKU]) > 1) ); | |
-- Delete entries | |
DELETE FROM [DB].[dbo].[TABLE] | |
WHERE [SKU] IN ('SKU1','SKU2','SKU2'); | |
-- Update multiple entries | |
-- ## Create backup - CTAS | |
SELECT * | |
INTO [DB].[dbo].[NEW_TABLE] | |
FROM [DB].[dbo].[OLD_TABLE] | |
-- ## Update statement | |
UPDATE TABLE | |
SET UPDATE_COLUMN = T.UPDATE_COLUMN | |
FROM [DB].[dbo].[TABLE] | |
JOIN ( | |
VALUES | |
('REF_VALUE1', 'UPDATE_VALUE1'), | |
('REF_VALUE2', 'UPDATE_VALUE2') | |
) T (REF_COLUMN, UPDATE_COLUMN) ON T.REF_COLUMN = TABLE.REF_COLUMN | |
-- Turn on Ad Hoc Queries/OPENROWSET | |
EXEC sp_configure 'show advanced options', 1 | |
RECONFIGURE | |
GO | |
EXEC sp_configure 'ad hoc distributed queries', 1 | |
RECONFIGURE | |
GO | |
-- Last X in Y Days methods | |
CAST(ISNULL(ROUND(SUM(CASE WHEN [DATE] > (CURRENT_TIMESTAMP - 30) | |
THEN [SALES_QUANTITY]-[RETURN_QUANTITY] END),2),0) AS int) | |
AS 'SALES_LAST30' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment