SQL UNION 查询不起作用

发布于 2024-09-19 06:41:48 字数 1746 浏览 7 评论 0原文

这是我当前的查询:

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_ 前缀并仅返回其运行帐户中的值。

here is my current queries:

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

These alone do exactly what i am wanting them to but as soon as i try and join them using a "UNION" or "Sub-query" the second query always breaks and displays the wrong information.

Am I just being blond not being able to work this out or am I actually doing something wrong.

All help is appreciated.

Many thanks Simon.

EDIT:
What I mean by "Wrong information" is that the 2nd query is returning all values rather then following the CRMAF_ prefix and returning only values from the account it is run on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2024-09-26 06:41:48

很难猜测“错误信息”是什么意思,但我相信您想要 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')

It's hard to guess what do you mean by "wrong information" but I believe you want UNION ALL rather than UNION.

UNION removes duplicates so the records from the second query won't be returned if they were previously returned by the first query. In addition, the possible duplicates within one query will be eliminated too.

The number of records in a UNION can be less than the total count of records in two queries.

If you just want to concatenate two recordsets, use 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')
时光暖心i 2024-09-26 06:41:48

在我看来,您应该能够获得与 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 子句。

It looks to me as though you should be able to get the same results as you would from the UNIONed query, with the following:

   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)

EDIT: LEFT JOIN FilteredMag_Payment

FURTHER EDIT: added final parenthesised OR condition to WHERE clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文