Skip to content
Advertisement

Yesterday SQL Server

I know very little SQL and have been asked to fix a problem in existing code. The code is PHP but the SQL causing the problem is:

The tables look like this:

dbo.dailybilling

vwJobValueVSInvoice

vwCurrencyRate

SQL returns this error:

In working through it the first question I have is What is Yesterday.InvoicedYesterday? It isn’t a table I can find and is not in the outer PHP.

Second questions, why the problem with WIP? It appears in order.

Assistance much appreciated.

Advertisement

Answer

Yesterday is an alias given to a subquery, and InvoicedYesterday is an alias given to a column in that subquery.

LEFT JOIN (
    SELECT
        SubJob
      , BillableTotal
      , BillableToday
      ,     InvoicedYesterday    = Invoiced
    FROM Intranet.dbo.DailyBilling
    WHERE Date = CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 111)
    )   Yesterday    ON vwJobValueVsInvoiced.SubJob = Yesterday.SubJob

All results of that subquery in the larger query will now be referenced through that alias, so all the columns of the subquery become

If you trace Yesterday.InvoicedYesterday backwards through that query it is sourced from the table Intranet.dbo.DailyBilling and from the column [Invoiced] in that table, but the subquery is filteing data using WHERE Date = CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 111)

If you run this

you will see that this is “2 days ago” or “the day before yesterday” e.g. if today is 2018-10-27 the query returns ‘2018/10/25’ (as a string, literally in that format)


I generally would not recommend use of date style 111 in your query (it is used twice). Instead I would recommend using style number 112 instead (this has no delimiter and is the safest format to use).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement