Getting the result with date from yesterday - sql-server

I am trying to tally all Invoices without Sales Order and show it as an SQL query. The date should be the Invoices entered yesterday. The challenge is it seems that the result is static and won't change
SELECT TOP (100) PERCENT CONVERT(varchar, DATEADD(dd, - 1, GETDATE()), 103) AS Date,
'Invoices w/o SO' AS Type,
COUNT(dbo.Invoice.InvoiceID) AS Orders,
COUNT(dbo.Invoice.InvoiceID) AS Chairs,
ISNULL(ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]
FROM dbo.Invoice INNER JOIN dbo.Customers
ON dbo.Invoice.CustomerID = dbo.Customers.CustomerID
INNER JOIN dbo.InvoiceDetails
ON dbo.Invoice.InvoiceID = dbo.InvoiceDetails.InvoiceID
WHERE (dbo.InvoiceDetails.ItemNo = 'TRIAL CHAIR')
OR
(dbo.InvoiceDetails.ItemNo = 'STORAGE')
OR
(dbo.InvoiceDetails.ItemNo = 'RSTF01')
OR
(dbo.InvoiceDetails.ItemNo = 'FRDMC01')
AND
(dbo.Invoice.CredInvoiceNo IS NULL)
AND
(dbo.Invoice.EntryDate >= CONVERT(char(8), DATEADD(dd, - 1, GETDATE()), 112))
AND
(dbo.Customers.CustomerID <> 187)
Screenshot

Thanks everyone.
I changed the query and it worked!
SELECT
CONVERT(varchar, DATEADD(dd, -1, GETDATE()), 103) AS Date,
'Invoices w/o SO' AS Type,
COUNT(dbo.Invoice.InvoiceID) AS Orders,
COUNT(dbo.Invoice.InvoiceID) AS Chairs,
ISNULL(ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]
FROM dbo.Invoice
INNER JOIN dbo.Customers
ON dbo.Invoice.CustomerID = dbo.Customers.CustomerID
INNER JOIN dbo.InvoiceDetails
ON dbo.Invoice.InvoiceID = dbo.InvoiceDetails.InvoiceID
WHERE (dbo.InvoiceDetails.ItemNo IN ('TRIAL CHAIR', 'STORAGE', 'RSTF01', 'FRDMC01'))
AND (dbo.Invoice.CredInvoiceNo IS NULL)
AND (dbo.Customers.CustomerID <> 187)
AND (DATEDIFF(DAY, dbo.Invoice.EntryDate, GETDATE()) = 1)

Related

Too many Group By dilutes query results

I need some assistance on a SQL Query. I hope i titled it correctly as I do not want to mislead anyone.
I am attempting to generate a query which will eventually be placed into a view and be used in the Microsoft Power BI tool that will return a count for a given event in the EVENT field/column, which are:
6 – Out of Spec Above
7 – Out of Spec Below
…that occurred within a given time frame along with the total number of measurements tested.
I am successful if I only group by Date, Item Code, Plant Code, and Property using the query below.
SELECT
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) AS [Date],
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1 AS Plant_Code,
dbo.OC_VDATA.UDL5 as Property,
SUM(CASE WHEN OC_VDATA.EVENT = 6 THEN 1 WHEN OC_VDATA.EVENT = 7 THEN 1 ELSE 0 END) AS Out_of_Spec,
COUNT(*) AS Total_Measured
FROM
dbo.OC_VDATA INNER JOIN
dbo.OC_VDAT_AUX ON dbo.OC_VDATA.PARTNO = dbo.OC_VDAT_AUX.PARTNOAUX
AND dbo.OC_VDATA.DATETIME = dbo.OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(dbo.OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(dbo.OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) = '08/21/2019'
and stagingPLM.dbo.ITEM_CODES.ITEM_CODE = '20SL'
AND dbo.OC_VDATA.UDL1 = '1011'
AND dbo.OC_VDATA.UDL5 = 'WEIGHT'
--(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-13, 0))
GROUP BY
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1,
dbo.OC_VDATA.UDL5
First Query Results:
If I add another grouping level, the EVENT, my Total_Measured column gets diluted. I expected this to happen.
SELECT
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) AS [Date],
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1 AS Plant_Code,
dbo.OC_VDATA.UDL5 as Property,
dbo.OC_VDATA.EVENT,
SUM(CASE WHEN OC_VDATA.EVENT = 6 THEN 1 WHEN OC_VDATA.EVENT = 7 THEN 1 ELSE 0 END) AS Out_of_Spec,
COUNT(*) AS Total_Measured
FROM
dbo.OC_VDATA INNER JOIN
dbo.OC_VDAT_AUX ON dbo.OC_VDATA.PARTNO = dbo.OC_VDAT_AUX.PARTNOAUX
AND dbo.OC_VDATA.DATETIME = dbo.OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(dbo.OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(dbo.OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) = '08/21/2019'
and stagingPLM.dbo.ITEM_CODES.ITEM_CODE = '20SL'
AND dbo.OC_VDATA.UDL1 = '1011'
AND dbo.OC_VDATA.UDL5 = 'WEIGHT'
--(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-13, 0))
GROUP BY
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1,
dbo.OC_VDATA.UDL5,
dbo.OC_VDATA.EVENT
Second Query Results:
With all that said, what I really want is the results from the second query and merge in the Total_Measured column from the first query.
Results I want:
I thought using a CTE was the answer but that takes a while and eventually generates an error.
WITH CTE1 AS
(
SELECT
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101) AS Date,
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1 AS Plant_Code,
OC_VDATA.UDL5 AS Property,
--SUM(CASE WHEN OC_VDATA.EVENT = 6 THEN 1 WHEN OC_VDATA.EVENT = 7 THEN 1 ELSE 0 END) AS Out_of_Spec,
COUNT(OC_VDATA.EVENT) AS Total_Measured
FROM
OC_VDATA INNER JOIN
OC_VDAT_AUX ON OC_VDATA.PARTNO = OC_VDAT_AUX.PARTNOAUX
AND OC_VDATA.DATETIME = OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(month, - 13, GETDATE()))
GROUP BY
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1,
OC_VDATA.UDL5
),
CTE2 AS
(
SELECT
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101) AS Date,
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1 AS Plant_Code,
OC_VDATA.UDL5 AS Property,
OC_VDATA.EVENT,
COUNT(*) AS Out_of_Spec
FROM
OC_VDATA INNER JOIN
OC_VDAT_AUX ON OC_VDATA.PARTNO = OC_VDAT_AUX.PARTNOAUX
AND OC_VDATA.DATETIME = OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(month, - 13, GETDATE()))
GROUP BY
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1,
OC_VDATA.UDL5,
OC_VDATA.EVENT
)
SELECT
cte1.Date,
cte1.ITEM_CODE,
cte1.Plant_Code,
cte1.PROPERTY,
EVENT,
Out_of_Spec,
Total_Measured
FROM
cte1 INNER JOIN cte2 on cte1.Date = cte2.date
and cte1.ITEM_CODE = cte2.ITEM_CODE
and cte1.Plant_Code = CTE2.Plant_Code
and cte1.Property = cte2.Property
How to achieve what I want? Does anyone have any tips/tricks? Am I even on the right track? Frustration is starting to set in.
DECLARE #TargetDate varchar(10) = '08/21/2019'
;WITH PlainList AS
(
SELECT
[Date] = CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101)
, Item_Code = stagingPLM.dbo.ITEM_CODES.ITEM_CODE
, Plant_Code = dbo.OC_VDATA.UDL1
, Property = dbo.OC_VDATA.UDL5
, [Event] = dbo.OC_VDATA.[EVENT]
, Out_of_Spec = CASE OC_VDATA.[EVENT]
WHEN 6 THEN 1
WHEN 7 THEN 1
ELSE 0
END
FROM dbo.OC_VDATA
INNER JOIN dbo.OC_VDAT_AUX ON dbo.OC_VDAT_AUX.PARTNOAUX = dbo.OC_VDATA.PARTNO
AND dbo.OC_VDAT_AUX.DATETIMEAUX = dbo.OC_VDATA.[DATETIME]
INNER JOIN stagingPLM.dbo.ITEM_CODES ON stagingPLM.dbo.ITEM_CODES.SPEC_NO = LEFT(dbo.OC_VDATA.PARTNO , 12)
AND stagingPLM.dbo.ITEM_CODES.SPEC_NO = LEFT(dbo.OC_VDAT_AUX.PARTNOAUX , 12)
WHERE 1=1
AND stagingPLM.dbo.ITEM_CODES.ITEM_CODE = '20SL'
AND dbo.OC_VDATA.UDL1 = '1011'
AND dbo.OC_VDATA.UDL5 = 'WEIGHT'
)
SELECT
[Date]
, Item_Code
, Plant_Code
, Property
, [Event]
, Out_of_Spec = SUM(Out_of_Spec)
, Total_Measured = SUM(1)
FROM PlainList P
GROUP BY
[Date]
, Item_Code
, Plant_Code
, Property
, [Event]
HAVING [Date] = #TargetDate

Percentage Difference Between Two Temp Columns in SQL

I am trying to add a column to this query that will calculate the percentage difference between c.PopulationForecast and c.PopulationActualMAX and/or c.PopulationActualMin. It would be nice if it was just one column but I will settle for two.
SELECT c.DepartmentName, c.DateandTime, c.ExecutionDate, c.PopulationForecast, c.PopulationActualMAX, c.PopulationActualMIN
FROM
(SELECT DepartmentName, DateandTime, ExecutionDate FROM ForecastTable WHERE DateandTime >= CAST(GETDATE() AS DATE)) d
CROSS APPLY
(SELECT *,
(SELECT MAX(ACT.PopulationActual) FROM ActualsTable ACT JOIN DepartmentTable DTD ON DTD.DepartmentTypeID = ACT.DepartmentTypeId JOIN FacilityTable FD ON FD.FacilityID = ACT.FacilityID WHERE (ACT.TargetDateTime >= DATEADD(DAY, -28, d.DateandTime)) and (DATEPART(WEEKDAY, ACT.TargetDateTime) = DATEPART(WEEKDAY, d.DateandTime)) and (CONCAT(FD.FacilityKey,'.All',DTD.DepartmentTypeKey) = DepartmentName))
as PopulationActualMAX,
(SELECT MIN(ACT.PopulationActual) FROM ActualsTable ACT JOIN DepartmentTable DTD ON DTD.DepartmentTypeID = ACT.DepartmentTypeId JOIN FacilityTable FD ON FD.FacilityID = ACT.FacilityID WHERE (ACT.TargetDateTime >= DATEADD(DAY, -28, d.DateandTime)) and (DATEPART(WEEKDAY, ACT.TargetDateTime) = DATEPART(WEEKDAY, d.DateandTime)) and (CONCAT(FD.FacilityKey,'.All',DTD.DepartmentTypeKey) = DepartmentName))
as PopulationActualMIN
FROM ForecastTable
WHERE
DepartmentName = d.DepartmentName
and DateandTime = d.DateandTime
and (
(ROUND (PopulationForecast, 0) >
(
SELECT MAX(ACT.PopulationActual)
FROM ActualsTable ACT
JOIN DepartmentTable DTD ON DTD.DepartmentTypeID = ACT.DepartmentTypeId
JOIN FacilityTable FD ON FD.FacilityID = ACT.FacilityID
WHERE (ACT.TargetDateTime >= DATEADD(DAY, -28, d.DateandTime))
and (DATEPART(WEEKDAY, ACT.TargetDateTime) = DATEPART(WEEKDAY, d.DateandTime))
and (CONCAT(FD.FacilityKey,'.All',DTD.DepartmentTypeKey) = d.DepartmentName)
)
)
or
(ROUND (PopulationForecast, 0) <
(
SELECT MIN(ACT.PopulationActual)
FROM ActualsTable ACT
JOIN DepartmentTable DTD ON DTD.DepartmentTypeID = ACT.DepartmentTypeId
JOIN FacilityTable FD ON FD.FacilityID = ACT.FacilityID
WHERE (ACT.TargetDateTime >= DATEADD(DAY, -28, d.DateandTime))
and (DATEPART(WEEKDAY, ACT.TargetDateTime) = DATEPART(WEEKDAY, d.DateandTime))
and (CONCAT(FD.FacilityKey,'.All',DTD.DepartmentTypeKey) = d.DepartmentName)
)
)
)
) c
Order By d.DepartmentName, d.DateandTime
Is there anything stopping you putting the difference in your outer select (first line)?
For example:
(c.PopulationForecast-c.PopulationActualMIN)/c.PopulationForecast ForecastVsMin
, (c.PopulationForecast-c.PopulationActualMAX)/c.PopulationForecast ForecastVsMax
From your query it looks like you already have all three fields you're looking for.

Incorrect syntax near the keyword 'DECLARE' in view

Can someone please confirm how to get rid of the following error.
DECLARE #StartDate DATETIME, #EndDate DATETIME
SET #StartDate = DATEADD(mm, DATEDIFF(mm, 0, getdate()) - 1, 0)
SET #EndDate = DATEADD(mm, 1, #StartDate)
SELECT
dbo.General_Ledger_Detail.Accounting_ID,
dbo.General_Ledger_Detail.Cost_Centre,
dbo.General_Ledger_Detail.Product_ID,
dbo.General_Ledger_Detail.Accounted_Amount AS Amount,
dbo.General_Ledger_Detail.Account_Name,
dbo.General_Ledger_Detail.Accounting_Date,
dbo.Account_Codes_Sales_OPEX$.[Opex Type],
dbo.LogSolOpexCC.Logistic_Solutions_Type
FROM
dbo.General_Ledger_Detail
INNER JOIN dbo.Account_Codes_Sales_OPEX$
ON dbo.General_Ledger_Detail.Accounting_ID =
dbo.Account_Codes_Sales_OPEX$.[Account Code]
INNER JOIN dbo.LogSolOpexCC
ON dbo.General_Ledger_Detail.Cost_Centre = dbo.LogSolOpexCC.Cost_Centre
GROUP BY dbo.General_Ledger_Detail.Accounting_ID,
dbo.General_Ledger_Detail.Cost_Centre,
dbo.General_Ledger_Detail.Product_ID,
dbo.General_Ledger_Detail.Accounted_Amount,
dbo.General_Ledger_Detail.Account_Name,
dbo.General_Ledger_Detail.Accounting_Date,
dbo.Account_Codes_Sales_OPEX$.[Opex Type],
dbo.LogSolOpexCC.Logistic_Solutions_Type
HAVING (dbo.General_Ledger_Detail.Accounting_Date BETWEEN #startdate AND #enddate)
You cannot pass parameters to SQL Server views. https://www.mssqltips.com/sqlservertip/5147/limitations-when-working-with-sql-server-views/
SELECT dbo.General_Ledger_Detail.Accounting_ID, dbo.General_Ledger_Detail.Cost_Centre, dbo.General_Ledger_Detail.Product_ID,
dbo.General_Ledger_Detail.Accounted_Amount AS Amount, dbo.General_Ledger_Detail.Account_Name, dbo.General_Ledger_Detail.Accounting_Date,
dbo.Account_Codes_Sales_OPEX$.[Opex Type], dbo.LogSolOpexCC.Logistic_Solutions_Type
FROM dbo.General_Ledger_Detail INNER JOIN
dbo.Account_Codes_Sales_OPEX$ ON dbo.General_Ledger_Detail.Accounting_ID = dbo.Account_Codes_Sales_OPEX$.[Account Code] INNER JOIN
dbo.LogSolOpexCC ON dbo.General_Ledger_Detail.Cost_Centre = dbo.LogSolOpexCC.Cost_Centre
GROUP BY dbo.General_Ledger_Detail.Accounting_ID, dbo.General_Ledger_Detail.Cost_Centre, dbo.General_Ledger_Detail.Product_ID,
dbo.General_Ledger_Detail.Accounted_Amount, dbo.General_Ledger_Detail.Account_Name, dbo.General_Ledger_Detail.Accounting_Date,
dbo.Account_Codes_Sales_OPEX$.[Opex Type], dbo.LogSolOpexCC.Logistic_Solutions_Type
HAVING (dbo.General_Ledger_Detail.Accounting_Date BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(mm, 1, GETDATE()))

How to display monthly balance for month with no balance SQL Server

I have the following block of code which I am using to query a SQL Server database that summarizes all balances for each of the previous 13 months. It works pretty good, but there are a few months when there were no balances to report. These months are not displaying which I do need. At this point, I am at a loss for what to try next.
DECLARE #StartDate DATE, #EndDate DATE;
SELECT
#StartDate = CONVERT(VARCHAR(11), DATEADD(month, -13, GETDATE())),
#EndDate = CONVERT(VARCHAR(11), DATEADD(month, 0, GETDATE()));
;WITH d(d) AS
(
SELECT
DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0))
FROM
(SELECT TOP (DATEDIFF(MONTH, #StartDate, #EndDate) + 1)
n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
FROM
sys.all_objects ORDER BY [object_id]) AS n
)
SELECT
FORMAT(d.d, 'MMM yy') AS Purchase_Date,
CAST(ROUND(SUM(lm.BALANCE), 0) AS FLOAT) AS Balance
FROM
d
LEFT OUTER JOIN
dbo.purchases AS lm ON lm.purchase_date >= d.d
AND lm.purchase_date < DATEADD(MONTH, 1, d.d)
WHERE
lm.Buyer_code = 'FirstTime'
AND lm.PROGRAM_ID = 'NewBuyers'
GROUP BY
d.d
ORDER BY
d.d
You are overriding the LEFT JOIN by the where clause, which requires that every row have certain values from the left joined table. In effect it is equivalent to an inner join. You need to allow rows from d to survive into the result, which you can do by using the wanted conditions directly in the LEFT JOIN:
SELECT
FORMAT( d.d, 'MMM yy' ) AS Purchase_Date
, CAST( ROUND( SUM( lm.BALANCE ), 0 ) AS float ) AS Balance
FROM d
LEFT OUTER JOIN DBO.purchases AS lm ON lm.purchase_date >= d.d
AND lm.purchase_date < DATEADD( MONTH, 1, d.d )
AND lm.Buyer_code = 'FirstTime'
AND lm.PROGRAM_ID = 'NewBuyers
GROUP BY
d.d
ORDER BY
d.d

How to set date clause for prevous month in sql?

I want to get last values for prevous month (universal, not just for january :))
How can I set up where condition? Now it works well for current month, but i want result for prevous.
SELECT MAX(v.timestamp) AS Date,
MAX(v.value) AS Stanje,
v.tag_id, t.prik_sifr, t.tag_name
FROM dbo.tag_values AS v INNER JOIN
dbo.typ_tag AS t ON v.tag_id = t.id
WHERE (t.prik_sifr IS NOT NULL) AND (t.unit = 'M3') AND
(DATEPART(YEAR, v.timestamp) = DATEPART(YEAR, SYSDATETIME())) AND
(DATEPART(MONTH, v.timestamp)= DATEPART(MONTH, SYSDATETIME())) GROUP BY v.tag_id, t.prik_sifr, t.tag_name)
You need to subtruct one month from the current date:
SELECT MAX(v.timestamp) AS Date,
MAX(v.value) AS Stanje,
v.tag_id, t.prik_sifr, t.tag_name
FROM dbo.tag_values AS v INNER JOIN
dbo.typ_tag AS t ON v.tag_id = t.id
WHERE (t.prik_sifr IS NOT NULL) AND (t.unit = 'M3') AND
(DATEPART(YEAR, v.timestamp) = DATEPART(YEAR, DATEADD(MONTH, -1 SYSDATETIME()))) AND
(DATEPART(MONTH, v.timestamp)= DATEPART(MONTH, DATEADD(MONTH, -1 SYSDATETIME())))
GROUP BY v.tag_id, t.prik_sifr, t.tag_name)

Resources