SQL:在动态键上加入行

发布于 2025-02-12 12:00:22 字数 2410 浏览 0 评论 0原文

我有一个具有可以通过三个键Refirefjrefk的复合键识别的产品的表。 refi不能为null,refj和refk可能为null。 钥匙有可信赖的命令。因此,REFI具有最低的推力功率,REFJ培养基和REFK最高的可信赖性能力。

产品P可以随着时间的推移升级其可信赖性关键,因此可以通过Refi和另一个时间点通过Refi和Refk来识别它。这段时间将表示为扫描产品的时间 - 列 t

规则1 :目标是通过将至少一个键中的一个钥匙相对应的时间来识别独特的产品 - 这是具有最高推力可达功率的非空键。

规则2 :另一个规则是,在某种意义上,如果产品随着时间的推移降低了其可信赖性关键,则该顺序不应在这种情况下进行。

规则3 :最后一个和最终的规则 - 当2种产品P1和P2被确定为与第三产品P3的潜在通信时,它是具有具有最高可信度能力的通用密钥的产品与P3相对应。

景象

示例

2

1景象1说明
规则以下
一些编号

​将这三行视为1个独特产品,因为它们都具有相同的非零reci1。这也是规则数3的一个典范,其中2种产品识别为与第三个的潜在通信也对应于彼此。

示例2

tRefiRefjRefj
T1Refi1 refj1refj1null
T2Refi2 Refi2Refj1null

在这个景象中,我希望能够将这两个行算作1行,因为它们具有共同的RefJ,并且RefJ具有较高的值得信赖的能力比重新提供的能力。因此,即使Refi键不同,REFJ也很重要。

示例3

tRefiRefjRefj
T1Refi1 Refj1refj1refk1
T2Refi2 Refi2Refj1null

在此典范中,我想说明规则编号2。在此CAS中,我想将这2行视为两个单独的产品不可能具有较低信任力的钥匙,因此在这种情况下,我们认为这是另一种产品。

示例4

tRefiRefjRefk
T1refi1nullnull
T2Refi2refj1null
T3Refi1 Refi1Refj1 Refk1Refk1

在这个景象中,我想说明规则编号3。 在这种情况下,第一行是3D行中产品的潜在对应。 基于Refi1和第二行产品的第一组基于REFJ1。 由于RefJ具有比Refi更高的可信赖能力,因此在这种情况下,第一行被确定为1个产品,最后2行是同一产品。

目标:基于这些规则,我需要找到以一种或另一种方式处理这些情况的SQL查询 - 它可以直接在单独的列中计算独特的产品或将独特的键关联。

任何帮助,算法的想法或如何治疗不同案例的建议将得到赞赏!

I have a table with products that can be identified via a composite key of three keys refi, refj and refk.
refi can not be null, refj and refk can be null.
The keys have an order in trustworthiness power. Therefore refi has the lowest thrustworthiness power, refj medium and refk the highest trustworthiness power.

A product p can upgrade its trustworthiness key through time and thus at one point it can be identified via refi and at another point in time via refi and refk for exemple. This time will be represented as time when the product was scaned - column t.

Rule 1 : The goal is to identify a unique product through time by corresponding at least one of the keys in the table - that is the non null key that has the highest thrustworthiness power.

Rule 2: Another rule is that the order matters in the sense that such correspondance should'nt be made in the case if a product downgrades its trustworthiness key through time.

Rule 3: And last and final rule - when 2 products p1 and p2 are identified as a potential correspondance to a third product p3, it is the product with the common key with the highest trustworthiness power that is finally corresponded with p3.

Here are some exemples:

Exemple 1 and exemple 2 illustrate rule number 1.

Exemple 1:

trefirefjrefk
t1refi1NULLNULL
t2refi1refj1NULL
t3refi1refj1refk

In this exemple I want to be able to count these three rows as 1 unique product because they all have the same non null refi1. It is also an exemple of rule number 3 where the 2 products identified as a potential correspondance to a third, are also corresponding to eachother.

Exemple 2:

trefirefjrefk
t1refi1refj1NULL
t2refi2refj1NULL

In this exemple I want to be able to count these two rows as 1 unique product because they have a common refj and refj has a higher trustworthiness power than refi. So even if the refi keys are different, it is the refj that counts.

Exemple 3:

trefirefjrefk
t1refi1refj1refk1
t2refi2refj1NULL

In this exemple I want to illustrate rule number 2. In this cas I want to count these 2 rows as two separate products because downgrading to a key with less trustworthiness power is not possible so in this case we suppose it's another product.

Exemple 4:

trefirefjrefk
t1refi1NULLNULL
t2refi2refj1NULL
t3refi1refj1refk1

In this exemple I want to illustrate rule number 3.
In this case 1st and 2nd row are potential correspondances for the product in 3d row.
The 1st based on refi1 and the 2nd row product based on refj1.
As refj has a higher trustworthiness power than refi, in this case the 1st row is identified as 1 product and the last 2 rows as the same product.

Goal : Based on these rules i need to find a SQL query that treats these cases in one way or another - it can be counting the unique products or associating unique keys in a separate column directly.

Any help, idea of an algorithm or a suggestion of how to treat the distinct cases will be appreciated!

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

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

发布评论

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

评论(1

胡大本事 2025-02-19 12:00:22

您可以通过在相同的“ ref ”值上应用自我加入,在每个“ ref ”上分开工作,然后将Union应用于结果,尽管根据帖子中指向的一个(k> j> i)固定优先级。

WITH 
    cte_refk AS (
        SELECT t1.t AS t1_t, t2.t AS t2_t
        FROM       tab t1
        INNER JOIN tab t2 ON t1.refk = t2.refk
                         AND t1.t    > t2.t    ),
    cte_refj AS (
        SELECT t1.t AS t1_t, t2.t AS t2_t
        FROM       tab t1
        INNER JOIN tab t2 ON t1.refj = t2.refj
                         AND t1.t    > t2.t    ),
    cte_refi AS (
        SELECT t1.t AS t1_t, t2.t AS t2_t
        FROM       tab t1
        INNER JOIN tab t2 ON t1.refi = t2.refi
                         AND t1.t    > t2.t    )
SELECT * 
FROM cte_refk

UNION ALL

SELECT * 
FROM cte_refj 
WHERE t1_t NOT IN (SELECT t1_t FROM cte_refk)

UNION ALL

SELECT * 
FROM cte_refi
WHERE t1_t NOT IN (SELECT t1_t FROM cte_refk)
  AND t1_t NOT IN (SELECT t1_t FROM cte_refj)

此查询将为您提供匹配的行。

此提小提琴

You can work separately on each "ref" by applying self joins on the same "ref" value, then apply a UNION to the results, though fixing priority according to the one pointed in the post (k > j > i).

WITH 
    cte_refk AS (
        SELECT t1.t AS t1_t, t2.t AS t2_t
        FROM       tab t1
        INNER JOIN tab t2 ON t1.refk = t2.refk
                         AND t1.t    > t2.t    ),
    cte_refj AS (
        SELECT t1.t AS t1_t, t2.t AS t2_t
        FROM       tab t1
        INNER JOIN tab t2 ON t1.refj = t2.refj
                         AND t1.t    > t2.t    ),
    cte_refi AS (
        SELECT t1.t AS t1_t, t2.t AS t2_t
        FROM       tab t1
        INNER JOIN tab t2 ON t1.refi = t2.refi
                         AND t1.t    > t2.t    )
SELECT * 
FROM cte_refk

UNION ALL

SELECT * 
FROM cte_refj 
WHERE t1_t NOT IN (SELECT t1_t FROM cte_refk)

UNION ALL

SELECT * 
FROM cte_refi
WHERE t1_t NOT IN (SELECT t1_t FROM cte_refk)
  AND t1_t NOT IN (SELECT t1_t FROM cte_refj)

This query will get you the matching rows.

Feel free to play with it using your examples at this fiddle.

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