我有两张桌子: 状态(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
-------------
答案 0 :(得分:3)
如果您可以创建these功能,那么您可以执行以下操作:
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
答案 1 :(得分:2)
怎么样:
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;
答案 2 :(得分:2)
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
答案 3 :(得分:2)
首先使用UNION对该状态表进行规范化,然后使用最大值进行分组。
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.
答案 4 :(得分:1)
我不确定这是否也适用于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
答案 5 :(得分:1)
这将起作用(来自我下面的模拟示例):
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