我需要根据父表搜索条件获取子表记录,但它们需要是不同的,输出应如下所示:
表A,有三行。第一行用于通用规则,第二行用于特定类别,第三行用于特定分支,类别和子类别。 现在,我的输出应该包含特定于泛型的规则。 以下是输出规则:
现在,
我尝试过以下查询,但没有提供所需的输出:
SELECT DISTINCT RULE,
value
FROM siebel.b rxm
WHERE par_row_id IN (SELECT row_id
FROM siebel.a
WHERE ( branch = 'Civil'
AND category = 'C.M.> (Civil)'
AND sub_category IS NULL )
OR ( branch = 'Civil'
AND category = 'C.M. (Civil)'
AND sub_category = 'Pauper' )
OR ( branch = 'Civil'
AND category IS NULL
AND sub_category IS NULL ))
我使用Oracle作为RDBMS。
架构声明:
Create Table A (ROW_ID int, BRANCH varchar(50), CATEGORY varchar(50), SUB_CATEGORY varchar(50))
Create Table B (PAR_ROW_ID int, RULE varchar(50), Value varchar(50))
INSERT INTO A (ROW_ID, BRANCH)
VALUES (1,'Civil')
INSERT INTO A (ROW_ID, BRANCH, CATEGORY)
VALUES (2,'Civil','C.M. (Civil)')
INSERT INTO A (ROW_ID, BRANCH, CATEGORY, SUB_CATEGORY)
VALUES (3,'Civil','C.M. (Civil)','Pauper')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (1,'Pay','10')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (1','Days','25')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (1,'Discount','20')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (2,'Pairs','5')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (2,'Pay','30')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (3,'Pay','15')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (3,'Discount','20')
INSERT INTO B (PAR_ROW_ID, RULE, VALUE)
VALUES (3,'items','30')
答案 0 :(得分:0)
SELECT MAX( par_row_id ) AS par_row_id,
rule,
MAX( value ) KEEP ( DENSE_RANK LAST OVER ORDER BY par_row_id ) AS value
FROM table_b
GROUP BY rule
或者:
SELECT par_row_id,
rule,
value
FROM (
SELECT b.*,
ROW_NUMBER() OVER ( PARTITION BY rule ORDER BY par_row_id DESC ) AS rn
FROM table_b b
)
WHERE rn = 1;