昨天的SQL Server

时间:2018-10-27 03:42:07

标签: sql sql-server

我对SQL的了解很少,因此被要求修复现有代码中的问题。代码是PHP,但是导致问题的SQL是:

$sql = "INSERT INTO Intranet.dbo.DailyBilling (Date, JobCode, SubJob, TotalTTC, TotalATTC, CompletedBillableHours, WIP, CurrencyCode, ContractValue, Invoiced, BillableTotal, BillableToday)
            SELECT
                Date = CONVERT(VARCHAR(10), dateadd(day,-1, getdate()), 111),
                JobCode,
                vwJobValueVsInvoiced.SubJob,
                TotalTTC,
                TotalATTC = ATTC,
                CompletedBillableHours,
                WIP,
                vwJobValueVsInvoiced.CurrencyCode,
                ContractValue,
                vwJobValueVsInvoiced.Invoiced,
                BillableTotal =  IIF(TotalTTC <> 0,((CompletedBillableHours/TotalTTC)*ContractValue), 0),
                BillableToday = IIF(TotalTTC <> 0,
                                     IIF(Yesterday.InvoicedYesterday <> vwJobValueVsInvoiced.Invoiced,
                                                      ((CompletedBillableHours/TotalTTC)*ContractValue)-vwJobValueVsInvoiced.Invoiced ,
                                                     (((CompletedBillableHours/TotalTTC)*ContractValue)-vwJobValueVsInvoiced.Invoiced)- (Yesterday.BillableTotal-Yesterday.InvoicedYesterday)), 
                                  0)
            FROM
                Intranet.dbo.vwJobValueVsInvoiced
            LEFT JOIN
                Intranet.dbo.vwCurrentRate ON vwJobValueVsInvoiced.CurrencyCode = vwCurrentRate.CurrencyCode
            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
            WHERE 
                vwJobValueVsInvoiced.Status <> 'Complete' AND IIF(TotalTTC <> 0,((CompletedBillableHours/TotalTTC)*ContractValue)-vwJobValueVsInvoiced.Invoiced, 0) <> 0";

表格如下:

dbo.dailybilling

vwJobValueVSInvoice

vwCurrencyRate

SQL返回此错误:

Array ( [0] => Array ( [0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'WIP'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'WIP'. ) ) 

在处理此问题时,我遇到的第一个问题是昨天是昨天。它不是我可以找到的表,也不在外部PHP中。

第二个问题,为什么WIP存在问题?它按顺序显示。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

Yesterday是给子查询的别名,InvoicedYesterday是给子查询的列的别名。

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

现在将通过该别名引用该较大查询中该子查询的所有结果,因此该子查询的所有列都将变为

Yesterday.SubJob
Yesterday.BillableTotal
Yesterday.BillableToday
Yesterday.InvoicedYesterday

如果通过该查询向后跟踪Yesterday.InvoicedYesterday,则该查询源自表Intranet.dbo.DailyBilling和该表的列[Invoiced],但是子查询正在使用{{1}筛选数据}

如果您运行此

WHERE Date = CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 111)

您会看到这是“ 2天前”或“ 前天”,例如如果今天是2018年10月27日,则查询返回“ 2018/10/25”(作为字符串,从字面上是该格式)


通常,我不建议在查询中使用日期样式111(两次使用)。相反,我建议改用样式号112(它没有定界符,是最安全的格式)。

答案 1 :(得分:0)

就像在上一个答案中提到的那样,“昨天”是子查询的别名,“发票”是其子列别名之一。

我认为WIP列的问题在于它属于DailyBilling表,但该列不存在于以Yesterday别名命名的左联接的select子句中。