1

这是我目前的查询:

1

SELECT FilteredInvoice.accountidname, 
       FilteredInvoice.createdon, 
       FilteredInvoice.createdon AS sort_date, 
       FilteredInvoice.duedate, 
       FilteredInvoice.invoicenumber, 
       FilteredInvoice.statecodename, 
       FilteredInvoice.totalamount_base, 
       CONVERT(datetime, NULL) AS mag_paymentdate, 
       0 AS mag_amount_base, 
       GETDATE() AS Today
  FROM FilteredAccount AS CRMAF_FilteredAccount 
  JOIN FilteredInvoice ON FilteredInvoice.accountid = CRMAF_FilteredAccount.accountid 
  JOIN FilteredMag_Payment ON FilteredInvoice.invoiceid = FilteredMag_Payment.mag_invoiceid
 WHERE (FilteredInvoice.statecodename <> 'Canceled')

2

   SELECT FilteredInvoice_1.accountidname, 
          FilteredInvoice_1.createdon, 
          FilteredInvoice_1.createdon AS sort_date, 
          FilteredInvoice_1.duedate, 
          FilteredInvoice_1.invoicenumber, 
          FilteredInvoice_1.statecodename, 
          FilteredInvoice_1.totalamount_base, 
          FilteredMag_Payment.mag_paymentdate, 
          FilteredMag_Payment.mag_amount_base, 
          GETDATE() AS Today
     FROM FilteredAccount AS CRMAF_FilteredAccount 
LEFT JOIN FilteredInvoice AS FilteredInvoice_1 ON FilteredInvoice_1.accountid = CRMAF_FilteredAccount.accountid 
     JOIN FilteredMag_Payment ON FilteredInvoice_1.invoiceid = FilteredMag_Payment.mag_invoiceid
    WHERE (FilteredInvoice_1.statecodename <> 'Canceled')

仅这些就完全符合我的要求,但是一旦我尝试使用“UNION”或“子查询”加入它们,第二个查询总是会中断并显示错误的信息。

我只是金发碧眼,无法解决这个问题,还是我实际上做错了什么。

感谢所有帮助。

非常感谢西蒙。

编辑: 我所说的“错误信息”是指第二个查询返回所有值,而不是遵循 CRMAF_ 前缀并仅返回运行它的帐户中的值。

4

2 回答 2

2

很难猜出“错误信息”是什么意思,但我相信你想要UNION ALL而不是UNION.

UNION删除重复项,因此如果第二个查询中的记录先前由第一个查询返回,则不会返回它们。此外,一个查询中可能存在的重复项也将被消除。

a 中的记录数UNION可以少于两个查询中的记录总数。

如果您只想连接两个记录集,请使用UNION ALL

SELECT FilteredInvoice.accountidname, 
       FilteredInvoice.createdon, 
       FilteredInvoice.createdon AS sort_date, 
       FilteredInvoice.duedate, 
       FilteredInvoice.invoicenumber, 
       FilteredInvoice.statecodename, 
       FilteredInvoice.totalamount_base, 
       CONVERT(datetime, NULL) AS mag_paymentdate, 
       0 AS mag_amount_base, 
       GETDATE() AS Today
  FROM FilteredAccount AS CRMAF_FilteredAccount 
  JOIN FilteredInvoice ON FilteredInvoice.accountid = CRMAF_FilteredAccount.accountid 
  JOIN FilteredMag_Payment ON FilteredInvoice.invoiceid = FilteredMag_Payment.mag_invoiceid
 WHERE (FilteredInvoice.statecodename <> 'Canceled')
UNION ALL
   SELECT FilteredInvoice_1.accountidname, 
          FilteredInvoice_1.createdon, 
          FilteredInvoice_1.createdon AS sort_date, 
          FilteredInvoice_1.duedate, 
          FilteredInvoice_1.invoicenumber, 
          FilteredInvoice_1.statecodename, 
          FilteredInvoice_1.totalamount_base, 
          FilteredMag_Payment.mag_paymentdate, 
          FilteredMag_Payment.mag_amount_base, 
          GETDATE() AS Today
     FROM FilteredAccount AS CRMAF_FilteredAccount 
LEFT JOIN FilteredInvoice AS FilteredInvoice_1 ON FilteredInvoice_1.accountid = CRMAF_FilteredAccount.accountid 
     JOIN FilteredMag_Payment ON FilteredInvoice_1.invoiceid = FilteredMag_Payment.mag_invoiceid
    WHERE (FilteredInvoice_1.statecodename <> 'Canceled')
于 2010-09-16T22:20:04.897 回答
1

在我看来,您应该能够获得与 UNIONed 查询相同的结果,如下所示:

   SELECT FilteredInvoice_1.accountidname, 
          FilteredInvoice_1.createdon, 
          FilteredInvoice_1.createdon AS sort_date, 
          FilteredInvoice_1.duedate, 
          FilteredInvoice_1.invoicenumber, 
          FilteredInvoice_1.statecodename, 
          FilteredInvoice_1.totalamount_base, 
          CASE PF.pay_flag 
              WHEN 0.0 THEN CONVERT(datetime, NULL)
              ELSE FilteredMag_Payment.mag_paymentdate
          END AS mag_paymentdate, 
          FilteredMag_Payment.mag_amount_base * PF.pay_flag AS mag_amount_base, 
          GETDATE() AS Today
     FROM FilteredAccount AS CRMAF_FilteredAccount 
     CROSS JOIN (SELECT 1.0 pay_flag UNION SELECT 0.0) AS PF
     JOIN FilteredInvoice AS FilteredInvoice_1 ON FilteredInvoice_1.accountid = CRMAF_FilteredAccount.accountid 
     LEFT JOIN FilteredMag_Payment ON FilteredInvoice_1.invoiceid = FilteredMag_Payment.mag_invoiceid
    WHERE (FilteredInvoice_1.statecodename <> 'Canceled') AND
          (PF.pay_flag = 0 OR FilteredMag_Payment.mag_invoiceid IS NOT NULL)

编辑:左加入 FilteredMag_Payment

进一步编辑:在 WHERE 子句中添加了带括号的最终 OR 条件。

于 2010-09-17T13:00:54.330 回答