Skip to content

Instantly share code, notes, and snippets.

@chadsten
Last active September 5, 2017 16:34
Show Gist options
  • Select an option

  • Save chadsten/9ee6c1ae4c04f1aa3ef779df675b5de0 to your computer and use it in GitHub Desktop.

Select an option

Save chadsten/9ee6c1ae4c04f1aa3ef779df675b5de0 to your computer and use it in GitHub Desktop.
SELECT
PartNum,
CONVERT(DECIMAL(16, 4), (SUM(OurShipQty) * .5)) AS QuarterTotal,
CONVERT(DECIMAL(16, 4), (SUM(OurShipQty) / 3) * .5) AS MonthAvg,
CONVERT(DECIMAL(16, 4), (COUNT(PartNum)) * .5) AS Hits,
COUNT(DISTINCT CustID) AS Customers,
MAX(Plant) AS Plant
FROM
dbo.v_InvoiceRegisterForTurns
WHERE
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
GROUP BY
PartNum,
Plant
@gmay46

gmay46 commented Sep 5, 2017

Copy link
Copy Markdown

Try pasting this in for your sub queries. (Might not work in that format)

WITH AvgQty AS (SELECT
AVG(OurShipQty) * .5 AS AvgQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
),
WITH StdQty as (SELECT
STDEV(OurShipQty) * .5 AS StdQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
) ,

@gmay46

gmay46 commented Sep 5, 2017

Copy link
Copy Markdown

If you want to "Force it" you could try this. Is nasty and would probably be slow.

SELECT
PartNum,
CONVERT(DECIMAL(16, 4), (SUM(OurShipQty) * .5)) AS QuarterTotal,
CONVERT(DECIMAL(16, 4), (SUM(OurShipQty) / 3) * .5) AS MonthAvg,
CONVERT(DECIMAL(16, 4), (COUNT(PartNum)) * .5) AS Hits,
COUNT(DISTINCT CustID) AS Customers,
(SELECT
AVG(OurShipQty) * .5 AS AvgQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
) AS AvgQty,
(SELECT
STDEV(OurShipQty) * .5 AS StdQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
) AS StdQty,
MAX(Plant) AS Plant
FROM
dbo.v_InvoiceRegisterForTurns AS Register
WHERE
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(OurShipQty) <
(/*AvgQty*/(SELECT
AVG(OurShipQty) * .5 AS AvgQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
)
+
/*StdQty*/(SELECT
STDEV(OurShipQty) * .5 AS StdQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
)) AND
(OurShipQty) >
(/*AvgQty*/(SELECT
AVG(OurShipQty) * .5 AS AvgQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE') )
-
/*StdQty*/(SELECT
STDEV(OurShipQty) * .5 AS StdQty
FROM
dbo.v_InvoiceRegisterForTurns AS IR
WHERE
IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
)) AND
(BuyToOrder <> 'TRUE')
GROUP BY
PartNum,
Plant

@cbnumbers

Copy link
Copy Markdown

SELECT
Register.PartNum,
CONVERT(DECIMAL(16, 4), (SUM(OurShipQty) * .5)) AS QuarterTotal,
CONVERT(DECIMAL(16, 4), (SUM(OurShipQty) / 3) * .5) AS MonthAvg,
CONVERT(DECIMAL(16, 4), (COUNT(Register.PartNum)) * .5) AS Hits,
COUNT(DISTINCT CustID) AS Customers, MAX(Plant) AS Plant, AVG(AvgQty) as AvgQty, AVG(StdQty) as StdQty
FROM
EpicorReports.dbo.v_InvoiceRegisterForTurns AS Register Left Join
(SELECT
AVG(OurShipQty) * .5 AS AvgQty, PartNum
FROM
EpicorReports.dbo.v_InvoiceRegisterForTurns AS IR
WHERE
--IR.PartNum = Register.PartNum AND
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(BuyToOrder <> 'TRUE')
GROUP BY PartNum
) AS AvgQty on Register.PartNum = AvgQty.PartNum Left Join

(SELECT 
	STDEV(OurShipQty) * .5 AS StdQty, PartNum 
	FROM  
		EpicorReports.dbo.v_InvoiceRegisterForTurns AS IR 
	WHERE 
		--IR.PartNum = Register.PartNum AND 
		(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND  
		(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND  
		(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND  
		(NetExtPrice > 0) AND 
		(BuyToOrder <> 'TRUE') 
	GROUP BY PartNum
) AS StdQty on Register.PartNum = StdQty.PartNum

WHERE
(InvoiceDate > DATEADD(month, - 12, GETDATE())) AND
(InvoiceDate <= DATEADD(month, - 9, GETDATE())) AND
(InvoiceTerms NOT IN ('16', '26', '14', 'PORD', '50%D', '50%S')) AND
(NetExtPrice > 0) AND
(OurShipQty) < (AvgQty + StdQty) AND
(OurShipQty) > (AvgQty - StdQty) AND
(BuyToOrder <> 'TRUE')
GROUP BY
Register.PartNum,
Plant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment