在存在条件的情况下旋转

J86

我在MySQL 5.7.35 中工作,我有下表:

结构

create table Table1 (
    Id int not null auto_increment,
    Name varchar(255) not null,
    primary key(Id)
);

create table Table2 (
    Id int not null auto_increment,
    Name varchar(255) not null,
    Table1_Id int not null,
    primary key(Id),
    foreign key(Table1_Id) references Table1(Id)  
);


create table Table3 (
    Id int not null auto_increment,
    Type varchar(255) not null,
    Name varchar(255) not null,
    Result varchar(255) not null,
    Table2_Id int not null,
    primary key(Id),
    foreign key(Table2_Id) references Table2(Id)
);

数据

在里面,我有以下数据:

| Id  | Name       |
| --- | ---------- |
| 1   | Computer A |

---

| Id  | Name       | Table1_Id |
| --- | ---------- | --------- |
| 1   | Test Run 1 | 1         |

---

| Id  | Type      | Name      | Result  | Table2_Id |
| --- | --------- | --------- | ------- | --------- |
| 1   | Processor | MMX       | Pass    | 1         |
| 2   | Processor | SSE       | Pass    | 1         |
| 3   | Processor | SSE 2     | Pass    | 1         |
| 4   | Display   | Red       | Pass    | 1         |
| 5   | Display   | Green     | Pass    | 1         |
| 6   | Keyboard  | General   | Pass    | 1         |
| 7   | Keyboard  | Lights    | Skipped | 1         |
| 8   | Network   | Ethernet  | Pass    | 1         |
| 9   | Network   | Wireless  | Skipped | 1         |
| 10  | Network   | Bluetooth | Fail    | 1         |

所需查询

我的需求发生了变化,现在,每个Type值都应该变成一列,并使用以下逻辑计算该值:

对于 中的任何给定值Type

  • 如果全部通过,则结果为Pass
  • 如果有任何失败,则结果是Fail
  • 如果有任何被跳过(前提是检查了前两点),则结果为Skipped

所以对于当前数据,输出将是:

| table1_name | processor_test | display_test | keyboard_test | Network |
| ----------- | ---------------|--------------|---------------|---------|
| Computer A  | Pass           | Pass         | Skipped       | Fail    |

当前查询

当列/值位于两级子表中时,我正在努力进行条件透视。我目前的查询是:

select t1.Name as 'table1_name'
-- pivoting columns
from Table1 t1
inner join Table2 t2 on t1.Id = t2.Table1_Id
inner join Table3 t3 on t2.Id = t3.Table2_Id;

我创建了一个db-fiddle来让事情变得更容易。

塞尔格

创建一个表格来确定结果的优先级

create table Priority (
  Id int not null ,
  Result varchar(255) not null
);

insert into Priority(id, result)
values 
(1,'Fail'),
(2,'Skipped'),
(3,'Pass')

select grp.table1_name, grp.Type, p.Result
from (    
    select t1.Name as table1_name,
    t3.Type, min(p.id) mp
    from Table1 t1
    inner join Table2 t2 on t1.Id = t2.Table1_Id
    inner join Table3 t3 on t2.Id = t3.Table2_Id
    inner join Priority p on p.Result = t3.Result
    group by t1.Name, t3.Type) grp
join Priority p on p.Id = grp.mp;

然后可以根据需要旋转结果。

编辑

您还可以使用 CASE 来编码/解码优先级

select table1_name, 
    MAX(
        CASE 
            WHEN Type='Processor' 
            THEN Result
            ELSE NULL 
        END
    ) AS 'processor_test',
    MAX(
        CASE 
            WHEN Type='Display' 
            THEN Result
            ELSE NULL 
        END
    ) AS 'display_test',
    MAX(
        CASE 
            WHEN Type='Network' 
            THEN Result
            ELSE NULL 
        END
    ) AS 'network_test',
    MAX(
        CASE 
            WHEN Type='Keyboard'
            THEN Result
            ELSE NULL 
        END
    ) AS 'keyboard_test' 
  from (    
    select t1.Name as table1_name,
        t3.Type, 
        case min(
            case t3.Result 
              when'Fail' then 1
              when'Skipped' then 2
              when'Pass' then 3
            end) 
          when 1 then 'Fail' 
          when 2 then 'Skipped'     
          when 3 then 'Pass'
        end Result
    from Table1 t1
    inner join Table2 t2 on t1.Id = t2.Table1_Id
    inner join Table3 t3 on t2.Id = t3.Table2_Id
    group by t1.Name, t3.Type
    ) t
 group by table1_name;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有外部功能的情况下旋转图像

如何在不使用JavaScript的情况下旋转glyphicon?

在没有动画的情况下旋转ImageButton onCreate

在不使用 ImageURI/ContentResolver 的情况下旋转位图?

在不丢失高度的情况下旋转 ImageView

如何在不旋转图像的情况下旋转剪切路径?

iText-如何在不旋转页面的情况下旋转pdf内容

如何在没有动画的情况下旋转素材图标?

如何在不更改布局的情况下旋转方向改变的视图?

如何在不与整个网站互动的情况下旋转背景图片?

如何在不影响画布中其他元素的情况下旋转特定元素?

在不裁剪的情况下旋转cv :: Mat后,将cv :: RotatedRect移动到相同位置

Pygame:如何在不对图像进行两次闪电闪烁的情况下旋转表面区域?

如何在不首先将其元素转换为int的情况下旋转python中的bytearray键?

ExoPlayer在全屏模式下旋转

在背景混合模式下旋转图像

Selenium Webdriver (JAVA) 代码在不存在 NoSuchElementException 元素的情况下以“If 条件”退出。

MySQL仅在不存在但有更多条件的情况下插入

在存在NA的情况下取消过滤条件会产生与直觉相反的结果

离子范围 如果为 true,则按下旋钮时会显示一个带有整数值的引脚。我可以在没有按下旋钮的情况下默认显示吗?

Javascript,如果在&&存在的情况下条件在没有&&逻辑运算符的情况下不起作用,则它不起作用

仅在存在的情况下包括

Pygame枪在左侧问题上将视线向下旋转

Postgres在竞争条件下不存在的情况下选择插入的任何重复机会

在存在某些条件的情况下,如何根据该数据帧的列值随机选择该数据帧的记录?

如何在不在 bash 脚本中创建竞争条件的情况下检查文件是否存在?

在加入条件的情况下如何工作

在某些情况下消除部分条件

MongoDB在不存在字段的情况下查找所有文档,如果存在则应用字段运算符($ max)条件