Teradata 中的模式 SQL - 从范围中排除一个值,并使用多个表

MODE in Teradata SQL - excluding a value from the range, and using multiple tables

我正在比较 3 个 table 的数据以获得相同的需求,并希望创建一个 table 来显示两个 table 的模式(以便就正确的值可能是什么提出建议)。我可能需要连接,因为我正在寻找具有相同 ID 和名称的所有行的模式。

Table 1:

+----------+----------+------+
| DemandNo | Forename | Size |
+----------+----------+------+
|        1 | Richard  |   42 |
|        2 | Richard  |   42 |
|        3 | Richard  |   42 |
|        4 | Richard  |   36 |
|        5 | Richard  |   36 |
|        6 | Richard  |   36 |
|        7 | Richard  |   36 |
|        8 | Luke     |   14 |
|        9 | Luke     |   14 |
|       10 | Luke     |   14 |
|       11 | Luke     |   14 |
|       12 | Luke     |   14 |
|       13 | Luke     |   25 |
|       14 | Luke     |   25 |
|       15 | Luke     |   25 |
+----------+----------+------+

Table 2:

+----------------+-----------------+
| List1_DemandNo | List1_PenColour |
+----------------+-----------------+
|              1 | White           |
|              2 | Black           |
|              3 | Black           |
|              4 | Red             |
|              5 | ?               |
|              6 | Red             |
|              7 | Red             |
|              8 | Yellow          |
|              9 | Yellow          |
|             10 | Yellow          |
|             11 | Green           |
|             12 | Yellow          |
|             13 | Green           |
|             14 | ?               |
|             15 | ?               |
+----------------+-----------------+

Table 3:

+----------------+-----------------+
| List2_DemandNo | List2_PenColour |
+----------------+-----------------+
|              1 | White           |
|              2 | Green           |
|              3 | Green           |
|              4 | Red             |
|              5 | ?               |
|              6 | Red             |
|              7 | Red             |
|              8 | Pink            |
|              9 | Pink            |
|             10 | Yellow          |
|             11 | Green           |
|             12 | Pink            |
|             13 | Orange          |
|             14 | Orange          |
|             15 | Orange          |
+----------------+-----------------+

因此我需要为每个具有相同姓名和大小的人生成推荐。建议应该是 table1 中所有行的模式,其中该人具有相同的名字和大小(我需要连接名字和大小吗?)

另一个要求是所有问号“?”应从 MODE/recommendation.

中排除

所以我的结果 table 应该是这样的:

+----------+----------+-----+------------+------------+
| DemandNo | Forename | Size | List1_MODE | List2_MODE |
+----------+----------+-----+------------+------------+
|        1 | Richard  |  42 | Black      | Green      |
|        2 | Richard  |  42 | Black      | Green      |
|        3 | Richard  |  42 | Black      | Green      |
|        4 | Richard  |  36 | Red        | Red        |
|        5 | Richard  |  36 | Red        | Red        |
|        6 | Richard  |  36 | Red        | Red        |
|        7 | Richard  |  36 | Red        | Red        |
|        8 | Luke     |  14 | Yellow     | Pink       |
|        9 | Luke     |  14 | Yellow     | Pink       |
|       10 | Luke     |  14 | Yellow     | Pink       |
|       11 | Luke     |  14 | Yellow     | Pink       |
|       12 | Luke     |  14 | Yellow     | Pink       |
|       13 | Luke     |  25 | Green      | Orange     |
|       14 | Luke     |  25 | Green      | Orange     |
|       15 | Luke     |  25 | Green      | Orange     |
+----------+----------+-----+------------+------------+

我知道 MODE 函数在 Teradata 中不起作用,我需要执行计数,但是使用 3 tables 并排除 ?遗憾的是超出了我的 SQL 技能 - 任何帮助将不胜感激!

非常感谢 理查德

您需要为每个模式编写单独的 select 并加入其中:

select t1.*, List1_PenColour, List2_PenColour 
from table1 as t1
left join 
  (
    select Forename, Size, List1_PenColour
    from table1 as t1
    join table2 as t2
    on t1.DemandNo = t2.List1_DemandNo
    and List1_PenColour <> '?'
    group by Forename, Size, List1_PenColour
    -- return only the row with the highest count
    -- random row if multiple rows with the same count exist
    qualify row_number()
            over (partiton by Forename, Size, List1_PenColour 
                  order by count(*) desc) = 1 
  ) as list1
on t1.Forename = list1.Forename
and t1.Size = list1.Size
left join 
  (
    select Forename, Size, List2_PenColour
    from table1 as t1
    join table3 as t3
    on t1.DemandNo = t3.List2_DemandNo
    and List2_PenColour <> '?'
    group by Forename, Size, List2_PenColour
    qualify row_number() 
            over (partiton by Forename, Size, List2_PenColour 
                  order by count(*) desc) = 1 
  ) as list2
on t1.Forename = list2.Forename
and t1.Size = list2.Size