SQL UNION查询无法正常工作

时间:2010-09-16 22:07:36

标签: sql sql-server dynamics-crm crm

这是我当前的查询:

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_前缀,只返回运行它的帐户中的值。

2 个答案:

答案 0 :(得分:2)

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

UNION删除重复项,因此如果第一个查询先前返回了第二个查询中的记录,则不会返回这些记录。此外,一个查询中可能的重复项也将被删除。

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')

答案 1 :(得分: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)

编辑:LEFT JOIN FilteredMag_Payment

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