MS Access:转置数据库表以避免冗余

时间:2015-07-05 00:08:31

标签: sql ms-access

我有一个包含2列的数据库表,如下所示:

IMPORT_ID   EXPORT_ID
SI1313721   SI1440839
SI1313721   SI1440997
SI1313722   SI1440672
SI1313722   SI1440776
SI1313722   SI1441313

IMPORT_ID正在重复(SI1313721 - 2次,SI1313722 - 3次)。我想把这个表转换成

SI1313721   SI1440839   SI1440997
SI1313722   SI1440672   SI1440776  SI1441313

我如何在MS Access中执行此操作?

2 个答案:

答案 0 :(得分:1)

转换不支持它,也没有分析功能可能很难做到。不确定,为什么你真的需要它转换到第一位。为了识别有问题的记录,您可以继续工作:

CREATE VIEW DuplicateImportID AS
  SELECT
    import_id
  FROM SomeTable
  GROUP BY import_id
  HAVING COUNT(export_id) > 1
;

CREATE VIEW ExportIDForDuplicateImportID AS
  SELECT
    D.import_id
    , S.export_id
  FROM DuplicateImportID D
  JOIN SomeTable S
    ON D.import_id = S.import_id
;

SELECT
  *
FROM ExportIDForDuplicateImportID
ORDER BY import_id, export_id
;

抱歉 - 我手头没有MS Access来验证/接近 看到它的实际应用:SQL Fiddle

如果您确实需要转置它并且卡在MS Access中,您可能需要查看:

CREATE VIEW DuplicateImportID AS
  SELECT
    import_id
  FROM SomeTable
  GROUP BY import_id
  HAVING COUNT(export_id) > 1
;

CREATE VIEW ExportIDForDuplicateImportID AS
  SELECT
    D.import_id
    , S.export_id
  FROM DuplicateImportID D
  JOIN SomeTable S
    ON D.import_id = S.import_id
;

CREATE VIEW ExportIDForDuplicateImportID1 AS
  SELECT
    import_id
    , MIN(export_id) export_id
    , 1 level
  FROM ExportIDForDuplicateImportID
  GROUP BY import_id
;

CREATE VIEW ExportIDForDuplicateImportID2 AS
  SELECT
    E.import_id
    , MIN(E.export_id) export_id
    , 2 level
  FROM ExportIDForDuplicateImportID E
  LEFT JOIN ExportIDForDuplicateImportID1 E1
    ON E.import_id = E1.import_id
    AND E.export_id = E1.export_id
  WHERE E1.export_id IS NULL
  GROUP BY E.import_id
;

CREATE VIEW ExportIDForDuplicateImportID3 AS
  SELECT
    E.import_id
    , MIN(E.export_id) export_id
    , 3 level
  FROM ExportIDForDuplicateImportID E
  LEFT JOIN ExportIDForDuplicateImportID1 E1
    ON E.import_id = E1.import_id
    AND E.export_id = E1.export_id
  LEFT JOIN ExportIDForDuplicateImportID2 E2
    ON E.import_id = E2.import_id
    AND E.export_id = E2.export_id
  WHERE E1.export_id IS NULL
    AND E2.export_id IS NULL
  GROUP BY E.import_id
;

CREATE VIEW Transposed AS
  SELECT
    D.import_id
    , (SELECT export_id FROM ExportIDForDuplicateImportID1 WHERE import_id = D.import_id) export_id1
    , (SELECT export_id FROM ExportIDForDuplicateImportID2 WHERE import_id = D.import_id) export_id2
    , (SELECT export_id FROM ExportIDForDuplicateImportID3 WHERE import_id = D.import_id) export_id3
  FROM DuplicateImportID D
;

SQL Fiddle中所示 不确定,MS Access将支持这一点--MySQL确实:SQL Fiddle

如果需要调整/进一步详细说明,请发表评论。

答案 1 :(得分:1)

要转换到您的需求需要一些细微差别,包括编号重复次数,然后使用子查询:

SomeQuery:

SELECT t1.IMPORT_ID, t1.EXPORT_ID 
   (SELECT COUNT(*) 
    FROM TableName AS t2
    WHERE t1.EXPORT_ID >= t2.EXPORT_ID 
     AND t1.IMPORT_ID = t2.EXPORT_ID) AS IDCount
FROM TableName AS t1;

TransposedQuery:

SELECT DISTINCT main.IMPORT_ID,
  (SELECT q1.EXPORT_ID FROM SomeQuery AS q1 
   WHERE q1.IMPORT_ID = main.IMPORT_ID AND q1.IDCount= 1) AS EXPORT1,
  (SELECT q2.EXPORT_ID FROM SomeQuery AS q2 
   WHERE q2.IMPORT_ID = main.IMPORT_ID AND q2.IDCount= 2) AS EXPORT2,
  (SELECT q3.EXPORT_ID FROM SomeQuery AS q3 
   WHERE q3.IMPORT_ID = main.IMPORT_ID AND q3.IDCount= 3) AS EXPORT3
FROM SomeQuery AS main;