外连接或动态查询,哪个是最好的方法?

发布于 2024-08-02 02:39:55 字数 1951 浏览 3 评论 0原文

我有下表(我简化了表中包含的数据)。

RateTable
- rate_table_id [int]
- rate_table_name [nvarchar(50)]

RateTableUsed
- rate_used_id [int]
- rate_table_id [int] (key to RateTable.rate_table_id)
- customer_id [int] 

RateTableExtra
- rate_table_extra_id [int]
- rate_ extra_id [int] (key to RateExtra.rate_table_id)
- rate_used_id [int] (key to RateTableUsed.rate_used_id)

RateExtra
- rate_ extra_id [int]
- rate_extra_name [nvarchar(50)]


select rate_table_name, rate_table_extra_id, rate_extra_name from RateTableUsed rtu 
    innerjoin RateTable rt on rtu.rate_table_id = rt.rate_table_id 
    innerjoin RateTableExtra rte on rte.rate_table_id = rt.rate_table_id
    innerjoing RateExtr re on rte.rate_extra_id = re.rate_extra_id

RateExtra 仅包含 3 个值,键为 1、2 和 1。 3、汽油附加费、管理费和消费税。

这对于当前的目的来说工作得很好。 正在显示包含匹配记录的值列表,并且仅针对rate_extra_name 查询RateExtra。

因此,我可能有以下结果:

  • Ratetable1, 1, PetrolSurcharge
  • Ratetable1, 2, ManagementFee
  • Ratetable2, 3, PetrolSurcharge
  • Ratetable4, 4, GST
  • Ratetable6, 5, PetrolSurcharge

我被要求修改此内容,以便现在返回的每条记录都包含记录对于 RateExtra 表中的每个值。 如果没有匹配的记录,那么我的 RateTableExtra 表中的数据应返回为 NULL。 所以我的数据应该返回为:

  • Ratetable1, 1, PetrolSurcharge
  • Ratetable1, 2, ManagementFee
  • Ratetable1, NULL, GST
  • Ratetable2, 3, PetrolSurcharge
  • Ratetable2, NULL, ManagementFee
  • Ratetable2, NULL, GST
  • Ratetable4, NULL, PetrolSurcharge
  • Ratetable4, NULL, ManagementFee
  • Ratetable4, 4, GST
  • Ratetable6, 5, PetrolSurcharge
  • Ratetable6, NULL, ManagementFee
  • Ratetable6, NULL, GST

我已经尝试过 OUTER 连接,但我假设它们似乎不起作用,因为 RateExtra 数据链接到 RateTableExtra ,它将返回 null 。 我现在正在考虑创建一个动态查询,该查询将获取我的原始结果集,对其进行迭代检查rate_extra_id,如果它不在结果集中,则在我需要的地方使用 NULL 数据将新行附加到结果中。 我假设这会起作用,但我有一种感觉,这将是性能的杀手。

有没有更好的方法来做到这一点? 希望有人能提供帮助,我们将不胜感激。

I have the following tables (I've simplified the data contained in the tables).

RateTable
- rate_table_id [int]
- rate_table_name [nvarchar(50)]

RateTableUsed
- rate_used_id [int]
- rate_table_id [int] (key to RateTable.rate_table_id)
- customer_id [int] 

RateTableExtra
- rate_table_extra_id [int]
- rate_ extra_id [int] (key to RateExtra.rate_table_id)
- rate_used_id [int] (key to RateTableUsed.rate_used_id)

RateExtra
- rate_ extra_id [int]
- rate_extra_name [nvarchar(50)]


select rate_table_name, rate_table_extra_id, rate_extra_name from RateTableUsed rtu 
    innerjoin RateTable rt on rtu.rate_table_id = rt.rate_table_id 
    innerjoin RateTableExtra rte on rte.rate_table_id = rt.rate_table_id
    innerjoing RateExtr re on rte.rate_extra_id = re.rate_extra_id

The RateExtra contains only 3 values with key's 1, 2 & 3 and names' petrol surcharge, management fee and GST.

This is working fine for it's current purpose. A list of values is being displayed with matching records and the RateExtra is queried only for the rate_extra_name.

So I may have the following results:

  • Ratetable1, 1, PetrolSurcharge
  • Ratetable1, 2, ManagementFee
  • Ratetable2, 3, PetrolSurcharge
  • Ratetable4, 4, GST
  • Ratetable6, 5, PetrolSurcharge

I've been asked to modify this so that each record that returns now includes records for each value in the RateExtra table. If there are no matching records then data from my RateTableExtra table should come back as NULL. So my data should come back as:

  • Ratetable1, 1, PetrolSurcharge
  • Ratetable1, 2, ManagementFee
  • Ratetable1, NULL, GST
  • Ratetable2, 3, PetrolSurcharge
  • Ratetable2, NULL, ManagementFee
  • Ratetable2, NULL, GST
  • Ratetable4, NULL, PetrolSurcharge
  • Ratetable4, NULL, ManagementFee
  • Ratetable4, 4, GST
  • Ratetable6, 5, PetrolSurcharge
  • Ratetable6, NULL, ManagementFee
  • Ratetable6, NULL, GST

I've tried OUTER joins but they don't seem to be working I'm assuming because the RateExtra data is linked to the RateTableExtra which would return null. I'm now considering creating a dynamic query that will get my original result set, iterate over it checking for rate_extra_id and, if it's not already in the resultset, appending a new row to the results with NULL data where I need it. I'm assuming this would work but I've got a feeling it'd be a killer on performance.

Is there any better way to do this? Hope someone can help, it'd be really appreciated.

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

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

发布评论

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

评论(1

雪若未夕 2024-08-09 02:39:55

试试这个:

select 
    rate_table_name, 
    rate_table_extra_id, 
    rate_extra
from 
    RateTableUsed rtu
    inner join RateTable rt on
        rtu.rate_table_id = rt.rate_table_id
    cross join RateExtra re
    left outer join RateTableExtra rte on 
        rte.rate_table_id = rt.rate_table_id
        and rte.rate_extra_id = re.rate_extra_id
order by rt.rate_table_name, re.rate_extra_id

您通过此查询得到的本质上是 RateTableRateExtra 之间的笛卡尔联接,并使用 left join 来查找与这些对匹配的 RateTableExtra 值。 本质上,此查询返回 RateTableRateExtra所有可能组合,并显示 RateTableExtra< 中包含哪些组合。 /code> 快速表。

小心笛卡尔连接。 如果桌子大小合理,他们很快就会失控!
享受!

Try this:

select 
    rate_table_name, 
    rate_table_extra_id, 
    rate_extra
from 
    RateTableUsed rtu
    inner join RateTable rt on
        rtu.rate_table_id = rt.rate_table_id
    cross join RateExtra re
    left outer join RateTableExtra rte on 
        rte.rate_table_id = rt.rate_table_id
        and rte.rate_extra_id = re.rate_extra_id
order by rt.rate_table_name, re.rate_extra_id

What you're getting with this query is, essentially, a Cartesian Join between RateTable and RateExtra, with a left join to find the values of RateTableExtra that match those pairs. Essentially, this query is bringing back all possible combinations of RateTable and RateExtra, and showing you which ones you have in your RateTableExtra table quickly.

Be careful with Cartesian Joins. They can get out of hand very quickly with reasonably sized tables!
Enjoy!

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