简体   繁体   English

SQL脚本未加入结果

[英]SQL Script Not Joining Results

I am having an issue getting this SQL Script to join the two tables together, I can execute it and it will show the results of the first portion (AccountID, AuditDate ..etc) but it will not join to the inner select statement. 我在使该SQL脚本将两个表连接在一起时遇到问题,我可以执行它,并且它将显示第一部分(AccountID,AuditDate ..etc)的结果,但不会连接到内部选择语句。

I have verified by spot checking that there is data that matches in the database. 我已经通过抽查确认数据库中是否存在匹配的数据。

What portion did i mess up? 我把哪一部分弄乱了?

Select AccountID, AuditDate, SourceVersion
From Audit
Left join (
    Select A.PrinterAuditID, A.AuditID, A.SerialNr, A.PageCountTotal,a.PageCountColor, A.PageCountMono
    from  InvalidPrinterAudit A
    where A.DeviceID = 90757
) InvalidPrinterAudit on InvalidPrinterAudit.AuditID = Audit.AuditID

There's a couple of things happening here. 这里发生了几件事。

One : In order to have fields in your result set, they must be included in the SELECT portion of your query: 一个 :为了在您的结果集中包含字段,必须将它们包含在查询的SELECT部分中:

Select AccountID, AuditDate, SourceVersion, InvalidPrinterAudit.PrinterAuditID, InvalidPrinterAudit.AuditID, InvalidPrinterAudit.SerialNr, InvalidPrinterAudit.PageCountTotal,InvalidPrinterAudit.PageCountColor, InvalidPrinterAudit.PageCountMono
    From Audit
    Left join (
        Select A.PrinterAuditID, A.AuditID, A.SerialNr, A.PageCountTotal,a.PageCountColor, A.PageCountMono
        from  InvalidPrinterAudit A
        where A.DeviceID = 90757
    ) InvalidPrinterAudit on InvalidPrinterAudit.AuditID = Audit.AuditID

Two : You don't need to have a subquery here. :您在这里不需要子查询。 Subqueries are great if you need to aggregate the results from a seperate table or something, but here you can just go with a LEFT OUTER JOIN and be done with it. 如果您需要汇总来自单独表或其他内容的结果,则子查询非常有用,但是在这里,您只需使用LEFT OUTER JOIN即可完成。

Select AccountID, AuditDate, SourceVersion, InvalidPrinterAudit.PrinterAuditID, InvalidPrinterAudit.AuditID, InvalidPrinterAudit.SerialNr, InvalidPrinterAudit.PageCountTotal,InvalidPrinterAudit.PageCountColor, InvalidPrinterAudit.PageCountMono
    From Audit
    Left OUTER JOIN InvalidPrinterAudit 
        ON InvalidPrinterAudit.AuditID = Audit.AuditID 
           AND InvalidPrinterAudit.DeviceID = 90757

This will apply that DeviceID = 90757 filter to your InvalidPrinterAudit before the join is applied, so you'll still get all of your Audit records, and then only the InvalidPrinterAudit records for that DeviceID that matches. 这将在应用DeviceID = 90757之前将DeviceID = 90757过滤器应用于InvalidPrinterAudit ,因此您仍将获得所有的Audit记录,然后仅获得与该DeviceID相匹配的InvalidPrinterAudit记录。

You are selecting less columns in the outer query than in the inner query. 您在外部查询中选择的列少于在内部查询中选择的列。

If you want them to be returned in the resultset, include them in the outer: 如果希望将它们返回到结果集中,请将它们包括在外部:

Select AccountID, AuditDate, SourceVersion, PrinterAuditID, SerialNr
From Audit
Left join (
    Select A.PrinterAuditID, A.AuditID, A.SerialNr, A.PageCountTotal,a.PageCountColor, A.PageCountMono
    from  InvalidPrinterAudit A
    where A.DeviceID = 90757
) InvalidPrinterAudit on InvalidPrinterAudit.AuditID = Audit.AuditID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM