简体   繁体   English

将没有重复(不同)的不同列加入/联合到一个新表中

[英]Join/Union different columns without duplicates (Distinct) into a new table

trying since several hours but were not able to find a working solution.尝试了几个小时,但无法找到有效的解决方案。 I am using MariaDB.我正在使用 MariaDB。 I have a table with several millions of rows (MySourceTable) where I want to get the unique cell values from specific columns and want copy them into a new table.我有一个包含数百万行 (MySourceTable) 的表,我想在其中从特定列中获取唯一的单元格值并将它们复制到新表中。

COL_A  COL_B  COL_C  COL_D  COL_E

  1      11     a      ab     a1  
  2      12     a      cd     a2  
  3      13     a      ab     a3  
  1      14     b      ab     a2  
  2      12     c      ef     a5  
  4      15     d      gh     a1

The content of the new should be like this:新的内容应该是这样的:

COL_A   COL_B  COL_C  COL_D  COL_E

  1      11     a      ab     a1  
  2      12     b      cd     a2  
  3      13     c      ef     a3  
  4      14     d      gh     a5  
         15 

     

Yep - the columns does not have any relation anymore after joining.是的 - 加入后列不再有任何关系。 Need the unique rows of this specific columns in a new target table for using them afterwards with DataTables SearchPanes filter .需要新目标表中此特定列的唯一行,以便随后与DataTables SearchPanes 过滤器一起使用

Edit: This is how datatables searchpanes expect the values for the filter.编辑:是数据表搜索窗格期望过滤器值的方式。 If I dont distinct the column values into a new unique table, searchPanes has to go on each page refresh to several hundert thousand rows, to get the values.如果我不将列值区分到一个新的唯一表中,searchPanes 必须在每个页面刷新到几十万行,以获取值。

What I tried so far:到目前为止我尝试过的:

DROP TABLE IF EXISTS col_names;
CREATE Table col_names 
(
    /*ID MEDIUMINT NOT NULL AUTO_INCREMENT, */
    ID MEDIUMINT NOT NULL Auto_Increment, 
    COL_A TINYTEXT, 
    COL_B TINYTEXT, 
    COL_C TINYTEXT, 
    COL_D TINYTEXT, 
    COL_E TINYTEXT
    PRIMARY KEY (ID)
);

INSERT INTO col_names (COL_A)
    Select Distinct Source_A AS COL_A FROM MySourceTable;
INSERT INTO col_names (COL_B)
    Select Distinct Source_B AS COL_B FROM MySourceTable;
INSERT INTO col_names (COL_C)
    Select Distinct Source_C AS COL_C FROM MySourceTable;
INSERT INTO col_names (COL_D)
    Select Distinct Source_D AS COL_D FROM MySourceTable;
INSERT INTO col_names (COL_E)
    Select Distinct Source_E AS COL_E FROM MySourceTable;

SELECT * FROM col_names
    ORDER BY COL_A, COL_B Desc; 

Result is not that what I am expecting.结果不是我所期望的。 I think, I have to work with Cross Apply, Join, Union etc. Tried different things, but failed so many times.我想,我必须使用 Cross Apply、Join、Union 等。尝试过不同的事情,但失败了很多次。

You can do this with row_number() , union all and aggregation:您可以使用row_number()union all和聚合来做到这一点:

select max(col_a), max(col_b), . . .
from ((select row_number() over (order by col_a) as seqnum,
              col_a, null as col_b, null as col_c, null as col_d, null as col_e
       from t
       group by col_a
      ) union all
      (select row_number() over (order by col_b) as seqnum,
              null as col_a, col_b, null as col_c, null as col_d, null as col_e
       from t
       group by col_b
      ) union all
      . . .
     ) x
group by seqnum;

Change your requirements to a normalised output;将您的要求更改为标准化输出; two columns - column_name and column_value.两列 - column_name 和 column_value。 Then, each cell in your expected results becomes a row in the normalised structure.然后,预期结果中的每个单元格都成为规范化结构中的一行。 This is much more in keeping with sql, much more flexible, doesn't have strange behaviour if one column has many more values than the rest, etc, etc.这更符合 sql,更灵活,如果一列的值比其余列多得多,则不会有奇怪的行为,等等。

SELECT
  DISTINCT
  lookup.*
FROM
  MySourceTable
CROSS APPLY
(
  SELECT 'COL_A', MySourceTable.Source_A
  UNION ALL
  SELECT 'COL_B', MySourceTable.Source_B
  UNION ALL
  etc, etc
)
  AS lookup(column_name, column_value)

Or perhaps...也许...

SELECT DISTINCT 'COL_A' AS column_name, Source_A AS column_value FROM MySourceTable
UNION ALL
SELECT DISTINCT 'COL_B' AS column_name, Source_B AS column_value FROM MySourceTable
UNION ALL
etc, etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM