簡體   English   中英

大丑的SQL查詢問題

[英]Big ugly SQL query problem

我有兩個表:Status(status_id,desc1,desc2,desc3,desc4)Status_Level(status_level_id,desc,levelD,levelS)

連接位於狀態中的desc1..4文件和Status_Level中的desc。 我需要選擇status_id,levelD和levelS,其中levelD和levelS是所有四個descs的最高級別。

我試過這個:

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

我的SQL非常生疏,所以任何幫助都會非常感激。 謝謝。

編輯:示例:

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
-------------

如果您可以創建這些功能,那么您可以:

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;

請注意,我假設desc1-4中不允許null

怎么樣:

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

首先,使用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; 

這是整個測試腳本和輸出

    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.

我不確定這是否也適用於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

這將有效(從我下面的模擬示例):

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

以下是完整的模擬,您可以將其粘貼到“查詢編輯器”窗口中:

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