简体   繁体   English

大丑的SQL查询问题

[英]Big ugly SQL query problem

I have two tables: Status(status_id, desc1, desc2, desc3, desc4) Status_Level(status_level_id, desc, levelD, levelS) 我有两个表:Status(status_id,desc1,desc2,desc3,desc4)Status_Level(status_level_id,desc,levelD,levelS)

The connection is on the desc1..4 fileds in Status and desc in Status_Level. 连接位于状态中的desc1..4文件和Status_Level中的desc。 I need to select status_id, levelD and levelS where levelD and levelS are highest levels of all four descs. 我需要选择status_id,levelD和levelS,其中levelD和levelS是所有四个descs的最高级别。

I tried this: 我试过这个:

SELECT Status.status_id, MAX(levelS) AS ds_column, MAX(levelD) AS ss_column 
FROM Status, Status_level WHERE
Status_level.desc=Status.desc1 OR 
Status_level.desc=Status.desc2 OR 
Status_level.desc=Status.desc3 OR 
Status_level.desc=Status.desc4 
GROUP BY Status.status_id

My SQL is very rusty so any help would be greatly appreciated. 我的SQL非常生疏,所以任何帮助都会非常感激。 Thanks. 谢谢。

edit: example: 编辑:示例:

Status:
-----------------------------------------------------------
| 1 | ABC_LEVEL_1 | DEF_LEVEL_5| CBA_LEVEL_2 | ABC_LEVEL_4|
-----------------------------------------------------------
| 2 | ABC_LEVEL_1 | DEF_LEVEL_1| CBA_LEVEL_2 | ABC_LEVEL_1|
-----------------------------------------------------------
| 3 | ABC_LEVEL_4 | DEF_LEVEL_1| CBA_LEVEL_2 | ABC_LEVEL_4|
-----------------------------------------------------------

Status_Level:
---------------------------
| 1 | ABC_LEVEL_1 | 1 | 7 |
---------------------------
| 1 | DEF_LEVEL_5 | 5 | 6 |
---------------------------
| 1 | CBA_LEVEL_2 | 2 | 3 |
---------------------------
| 1 | ABC_LEVEL_4 | 4 | 5 |
---------------------------
| 1 | DEF_LEVEL_1 | 1 | 2 |

Desired output:

-------------
| 1 | 5 | 7 | <- 5 from DEF_LEVEL_5 and 7 from ABC_LEVEL_1
------------- 
| 2 | 2 | 7 | <- 2 from CBA_LEVEL_2 and 7 from ABC_LEVEL_1
-------------
| 3 | 4 | 5 | <- 4 from ABC_LEVEL_4 and 5 from ABC_LEVEL_4
-------------

If you can create these functions then you can do: 如果您可以创建这些功能,那么您可以:

SELECT Status.status_id, 
       MaxOfList(l1.levelD,l2.levelD,l3.levelD,l4.levelD) AS ds_column, 
       MaxOfList(l1.levelS,l2.levelS,l3.levelS,l4.levelS) AS ss_column
FROM Status s, Status_level l1, Status_level l2, Status_level l3, Status_level l4
WHERE l1.desc=s.desc1 AND l2.desc=s.desc2 
      AND l3.desc=s.desc3 AND l4.desc=s.desc4;

Note that I am assuming that null is not allowed in desc1-4 请注意,我假设desc1-4中不允许null

How about: 怎么样:

SELECT t.status_id, Max(Status_Level.levelD) AS MaxOflevelD, 
       Max(Status_Level.levelS) AS MaxOflevelS
FROM Status_Level INNER JOIN (SELECT s.status_id, s.desc1 As [Desc]
FROM status s
UNION ALL
SELECT s.status_id, s.desc2 As [Desc]
FROM status s
UNION ALL
SELECT s.status_id, s.desc3 As [Desc]
FROM status s
UNION ALL
SELECT s.status_id, s.desc4 As [Desc]
FROM status s)  AS t ON Status_Level.desc = t.Desc
GROUP BY t.status_id;
SELECT Status.status_id, MAX(levelS) AS ds_column, MAX(levelD) AS ss_column 
FROM Status, Status_level WHERE
Status_level.desc in (Status.desc1, Status.desc2, Status.desc3, Status.desc4)
GROUP BY Status.status_id

First you normalize that status table with a UNION and then you do the group by with the max values. 首先,使用UNION标准化该状态表,然后使用最大值执行group by。

select status_id, max(levelid), max(levels) from ( 
select * from status_level, status where desc = desc1 
union 
select * from status_level, status where desc = desc2 
union 
select * from status_level, status where desc = desc3 
union 
select * from status_level, status where desc = desc4) 
group by status_id; 

Here is the whole test script and output 这是整个测试脚本和输出

    create table status (status_id number, desc1 varchar2(50), desc2 varchar2(50), desc3 varchar2(50), desc4 varchar2(50) );

    create table status_level (status_level_id number, descx varchar2(50), levelid number, levels number);

    insert into status values (1,'ABC_LEVEL_1','DEF_LEVEL_5','CBA_LEVEL_2','ABC_LEVEL_4');
    insert into status values (2,'ABC_LEVEL_1','DEF_LEVEL_1','CBA_LEVEL_2','ABC_LEVEL_1');
    insert into status values (3,'ABC_LEVEL_4','DEF_LEVEL_1','CBA_LEVEL_2','ABC_LEVEL_4');

    insert into status_level values (1,'ABC_LEVEL_1',1,7);
    insert into status_level values (1,'DEF_LEVEL_5',5,6);
    insert into status_level values (1,'CBA_LEVEL_2',2,3);
    insert into status_level values (1,'ABC_LEVEL_4',4,5);
    insert into status_level values (1,'DEF_LEVEL_1',1,2);

    commit;

    select * from status;

    select * from status_level;

    select * from status, status_level where descx = desc1
    union
    select * from status, status_level where descx = desc2
    union
    select * from status, status_level where descx = desc3
    union
    select * from status, status_level where descx = desc4;


    select status_id, max(levelid), max(levels) from (
    select * from status_level, status where descx = desc1
    union
    select * from status_level, status where descx = desc2
    union
    select * from status_level, status where descx = desc3
    union
    select * from status_level, status where descx = desc4)
    group by status_id;

    Table created.
    Table created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    Commit complete.

     STATUS_ID DESC1                                              DESC2                                              DESC3                                              DESC4                                             
    ---------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
             1 ABC_LEVEL_1                                        DEF_LEVEL_5                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                       
             2 ABC_LEVEL_1                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_1                                       
             3 ABC_LEVEL_4                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                       


    3 rows selected.

    STATUS_LEVEL_ID DESCX                                                 LEVELID     LEVELS
    --------------- -------------------------------------------------- ---------- ----------
                  1 ABC_LEVEL_1                                                 1          7
                  1 DEF_LEVEL_5                                                 5          6
                  1 CBA_LEVEL_2                                                 2          3
                  1 ABC_LEVEL_4                                                 4          5
                  1 DEF_LEVEL_1                                                 1          2


    5 rows selected.

     STATUS_ID DESC1                                              DESC2                                              DESC3                                              DESC4                                              STATUS_LEVEL_ID DESCX                                                 LEVELID     LEVELS
    ---------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------- -------------------------------------------------- ---------- ----------
             1 ABC_LEVEL_1                                        DEF_LEVEL_5                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 ABC_LEVEL_1                                                 1          7
             1 ABC_LEVEL_1                                        DEF_LEVEL_5                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 ABC_LEVEL_4                                                 4          5
             1 ABC_LEVEL_1                                        DEF_LEVEL_5                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 CBA_LEVEL_2                                                 2          3
             1 ABC_LEVEL_1                                        DEF_LEVEL_5                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 DEF_LEVEL_5                                                 5          6
             2 ABC_LEVEL_1                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_1                                                      1 ABC_LEVEL_1                                                 1          7
             2 ABC_LEVEL_1                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_1                                                      1 CBA_LEVEL_2                                                 2          3
             2 ABC_LEVEL_1                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_1                                                      1 DEF_LEVEL_1                                                 1          2
             3 ABC_LEVEL_4                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 ABC_LEVEL_4                                                 4          5
             3 ABC_LEVEL_4                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 CBA_LEVEL_2                                                 2          3
             3 ABC_LEVEL_4                                        DEF_LEVEL_1                                        CBA_LEVEL_2                                        ABC_LEVEL_4                                                      1 DEF_LEVEL_1                                                 1          2


    10 rows selected.

     STATUS_ID MAX(LEVELID) MAX(LEVELS)
    ---------- ------------ -----------
             1            5           7
             2            2           7
             3            4           5


    3 rows selected.

I'm not sure if this would work in Access as well, but I'm pretty sure this should give the correct results: 我不确定这是否也适用于Access,但我很确定这应该给出正确的结果:

SELECT status_id, max(levelS) as ds_column, max(levelD) as ss_column
FROM (
    SELECT s.status_id, sl.levelS, sl.levelD
    FROM Status s
    INNER JOIN Status_level sl ON s.desc1 = sl.desc
UNION ALL
    SELECT s.status_id, sl.levelS, sl.levelD
    FROM Status s
    INNER JOIN Status_level sl ON s.desc2 = sl.desc
UNION ALL
    SELECT s.status_id, sl.levelS, sl.levelD
    FROM Status s
    INNER JOIN Status_level sl ON s.desc3 = sl.desc
UNION ALL
    SELECT s.status_id, sl.levelS, sl.levelD
    FROM Status s
    INNER JOIN Status_level sl ON s.desc4 = sl.desc
) ssl
GROUP BY status_id

This will work (from my mocked up example below): 这将有效(从我下面的模拟示例):

select S.status_id, max(levelD), max(levelS)
from @status s
inner join @Status_Level sl1 on sl1.[desc] in (s.desc1,s.desc2,s.desc3,s.desc4)
group by status_id

Here is the complete mock up which you can paste into your Query Editor window: 以下是完整的模拟,您可以将其粘贴到“查询编辑器”窗口中:

declare @status table (status_id int, desc1 varchar(20), desc2 varchar(20), desc3 varchar(20), desc4 varchar(20)) 
declare @Status_Level table (status_level_id int, [desc]varchar(20), levelD int, levelS int)

insert into @status values(1, 'ABC_LEVEL_1','DEF_LEVEL_5','CBA_LEVEL_2','ABC_LEVEL_4')
insert into @status values(2, 'ABC_LEVEL_1','DEF_LEVEL_1','CBA_LEVEL_2','ABC_LEVEL_1')
insert into @status values(3, 'ABC_LEVEL_4','DEF_LEVEL_1','CBA_LEVEL_2','ABC_LEVEL_4')

insert into @Status_Level values(1, 'ABC_LEVEL_1',1,7)
insert into @Status_Level values(1, 'DEF_LEVEL_5',5,6)
insert into @Status_Level values(1, 'CBA_LEVEL_2',2,3)
insert into @Status_Level values(1, 'ABC_LEVEL_4',4,5)
insert into @Status_Level values(1, 'DEF_LEVEL_1',1,2)

--The select statement
select S.status_id, max(levelD), max(levelS)
from @status s
inner join @Status_Level sl1 on sl1.[desc] in (s.desc1,s.desc2,s.desc3,s.desc4)
group by status_id

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

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