开发者

Big ugly SQL query problem

开发者 https://www.devze.com 2023-01-30 06:52 出处:网络
I have two tables: Status(status_id, desc1, desc2, desc3, desc4) Status_Level(status_level_id, desc, levelD, levelS)

I have two tables: 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. I need to select status_id, levelD and levelS where levelD and levelS are highest levels of all four 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. 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


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.

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.


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;


I'm not sure if this would work in Access as well, but I'm pretty sure this should give the correct results:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消