简体   繁体   English

MySQL的枢轴使用列和行号

[英]mysql pivot using column and row numbers

I am stuck in this situation where I need to use Row Number and Column Number values from table's columns to derive the output mentioned below. 我被困在这种情况下,我需要使用表列的行号和列号值来导出下面提到的输出。 I have tried everything - if/else, case when/then but not helping. 我已经尝试了一切-如果/其他情况下,何时/然后,但没有帮助。

Any help/suggestions are really appreciated! 任何帮助/建议都非常感谢!

Here is a mocked up sample data present in db table - 这是db表中存在的模拟样本数据-

+--------+--------+--------+----------+-------------+ 
| Record | ColNbr | RowNbr | ColTitle | CellContent | 
+--------+--------+--------+----------+-------------+
|      1 | 1      | 1      | Unit     | sqf         |
|      1 | 1      | 2      | Unit     | cm          |
|      1 | 2      | 1      | Desc     | roof        |
|      1 | 2      | 2      | Desc     | rod         |
|      1 | 3      | 1      | Material | concrete    |
|      1 | 3      | 2      | Material | steel       |
|      1 | 4      | 1      | Quantity | 100         |
|      1 | 4      | 2      | Quantity | 12          |
|      1 | 1      | 1      | Unit     | liter       |
|      1 | 1      | 2      | Unit     | ml          |
|      1 | 2      | 1      | Desc     | bowl        |
|      1 | 2      | 2      | Desc     | plate       |
|      1 | 3      | 1      | Material | plastic     |
|      1 | 3      | 2      | Material | glass       |
|      1 | 4      | 1      | Quantity | 2           |
|      1 | 4      | 2      | Quantity | 250         |
+--------+--------+--------+----------+-------------+

Expected Output - 预期产出-

+--------+--------+--------+----------+-------------+ 
| Record | Unit   | Desc   | Material | Quantity    | 
+--------+--------+--------+----------+-------------+
|      1 | sqf    | roof   | concrete | 100         |
|      1 | cm     | rod    | steel    | 12          |
|      2 | liter  | bowl   | plastic  | 2           |
|      2 | ml     | plate  | glass    | 250         |
+--------+--------+--------+----------+-------------+

If your actual data is like that, I suggest that you consider to separate the data to; 如果您的实际数据是这样,我建议您考虑将数据分离到; for example, 4 different tables (unit,description,material & a table to store all that ids+quantity). 例如,有4个不同的表格(单位,说明,物料和一个用于存储所有ID +数量的表格)。 The former 3 tables will store the prerequisite info that get minor updates throughout time and the last table will store all the quantity records. 前3个表将存储先决条件信息,这些信息将在整个时间内进行较小的更新,而最后一个表将存储所有数量记录。 Let's say your tables will look something like this: 假设您的表格如下所示:

CREATE TABLE `Unit` (
unit_id INT,
unit_name VARCHAR(50));

+---------+-----------+
| unit_id | unit_name |
+---------+-----------+
|    1    | sqf       |
|    2    | cm        |
|    3    | liter     |
|    4    | ml        |
+---------+-----------+

CREATE TABLE `Description` (
desc_id INT,
desc_name VARCHAR(50));

+---------+-----------+
| desc_id | desc_name |
+---------+-----------+
|    1    | roof      |
|    2    | rod       |
|    3    | bowl      |
|    4    | plate     |
+---------+-----------+

CREATE TABLE `Material` (
mat_id INT,
mat_name VARCHAR(50));

+--------+----------+
| mat_id | mat_name |
+--------+----------+
|    1   | concrete |
|    2   | steel    |
|    3   | plastic  |
|    4   | glass    |
+--------+----------+

CREATE TABLE `Records` (
unit_id INT,
desc_id INT,
mat_id INT,
quantity DECIMAL(14,4));

+---------+---------+--------+----------+ 
| unit_id | desc_id | mat_id | Quantity | 
+---------+---------+--------+----------+
|    1    |    1    |    1   | 100      |
|    2    |    2    |    2   | 12       |
|    3    |    3    |    3   | 2        |
|    4    |    4    |    4   | 250      |
+---------+---------+--------+----------+

Then you insert the data accordingly. 然后,您相应地插入数据。

Anyhow, for your existing data example, it could be done but there are some concern over whether the unit+desc+material+quantity matching are correct. 无论如何,对于您现有的数据示例,可以完成此操作,但是对于单位+说明+材料+数量匹配是否正确存在一些担忧。 The only way I can maybe at least think that it's correctly matched is by giving all of the query a similar ORDER BY clause. 我可能至少认为它正确匹配的唯一方法是为所有查询提供相似的ORDER BY子句。 Hence, the following: 因此,以下内容:

SELECT A.record,A.unit,B.Desc,C.Material,D.Quantity FROM
     (SELECT @rn:=@rn+1 AS record,CASE WHEN coltitle='unit' THEN cellcontent END AS Unit 
        FROM yourtable, (SELECT @rn :=0 ) v 
      HAVING unit IS NOT NULL
      ORDER BY colnbr) A LEFT JOIN
     (SELECT @rn1:=@rn1+1 AS record,CASE WHEN coltitle='Desc' THEN cellcontent END AS `Desc`
        FROM yourtable, (SELECT @rn1 :=0 ) v 
      HAVING `Desc` IS NOT NULL
      ORDER BY colnbr) B ON a.record=b.record LEFT JOIN 
     (SELECT @rn2:=@rn2+1 AS record,CASE WHEN coltitle='material' THEN cellcontent END AS Material 
        FROM yourtable, (SELECT @rn2:=0 ) v 
      HAVING Material IS NOT NULL
      ORDER BY colnbr) C ON a.record=c.record LEFT JOIN 
     (SELECT @rn3:=@rn3+1 AS record,CASE WHEN coltitle='Quantity' THEN cellcontent END AS Quantity 
        FROM yourtable, (SELECT @rn3:=0 ) v 
      HAVING Quantity IS NOT NULL
      ORDER BY colnbr) D ON a.record=d.record;

The idea here is to make a sub-query based on COLTITLE then assign a numbering/ranking ( @rn,@rn1,@rn2,@rn3 ) variable to each of the sub-query and join them up using LEFT JOIN . 这里的想法是基于COLTITLE创建一个子查询,然后为每个子查询分配一个编号/排名( @rn,@rn1,@rn2,@rn3 )变量,然后使用LEFT JOIN将它们LEFT JOIN Now, this experiment works to exactly return the output that you need but its not a definite answer because there are some part that is questionable especially on matching the combination correctly. 现在,此实验可以准确返回所需的输出,但它不是一个确定的答案,因为有些部分存在问题,尤其是在正确匹配组合时。 Hopefully, this will give you some idea. 希望这会给您一些想法。

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

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