I am adding a commission percent column to an existing query. However, the commission data sits in a table unrelated to the main table (A) used within the query. However, these two tables have common columns/values with Table B.
I have three tables, A, B and C below
Table A
Reference Value_Name Renewal_Code
1 A N
2 A R
3 B N
4 A R
4 A N
Table B
Reference Value_Name Prod_Code
1 A 0016
2 A 0027
4 A 0032
4 A 0032
Table C
A_Prod_Code A_Tans_Code **Commission_Percent**
0016 Renewal 5
0027 Renewal 5
0032 New 10
0032 Renewal 5
I need to get the Commission_Percent from Table C relating to the corresponding Renewal_Code from Table A. This is the same as A_Tans_Code from Table C except that Table C spells out Renewal or New and Table A only uses R or N.
I have been able to pull through the Commission_Percent column into the output by using Table B for common values, but all values show as NULL.
I have also tried using a decode statement in order to link the Renewal_code/A_Trans_Code columns from Tables A and C.
(
SELECT
distinct c.commision_percent
FROM
TableA a
JOIN TableB b ON a.reference = b.reference
AND b.value_name = 'A'
JOIN TableC c ON b.prod_code = c.a_prod_code
AND b.value_name = 'A'
JOIN TableC c ON a.renewal_code = decode(c.a_trans_code, 'Rewnal','R','New','N')
) Commission_Percent
I need the correct commission_percent for Renewal and New business to come through for each reference. So far, I am only getting NULLs as I am having a hard time linking Tables A and C's Renewal_code and A_prod_code columns.
Any help is greatly appreciated!
This creates reproducible testing by cleaning up temp tables and re-inserting the data.
And could be a model for other solutions.
The SQL 'Select...' joins to a single TableC that has two parts to the ON condition-- prod_code and renewal_code (instead of two joins). Just remove the "a.*," to use it in your sql. (the decode function was changed to use a subscript of the first char of the A_Trans_Code).
IF OBJECT_ID('tempdb..#TableA') IS NOT NULL DROP TABLE #TableA
GO
CREATE TABLE #TableA
( Reference INTEGER
, Value_Name VARCHAR(10)
, Renewal_Code VARCHAR(10) )
INSERT INTO #TableA VALUES( 1, 'A', 'N' );
INSERT INTO #TableA VALUES( 2, 'A', 'R' );
INSERT INTO #TableA VALUES( 3, 'B', 'N' );
INSERT INTO #TableA VALUES( 4, 'A', 'R' );
INSERT INTO #TableA VALUES( 4, 'A', 'N' );
IF OBJECT_ID('tempdb..#TableB') IS NOT NULL DROP TABLE #TableB
GO
CREATE TABLE #TableB
( Reference INTEGER
, Value_Name VARCHAR(10)
, Prod_Code VARCHAR(10) )
INSERT INTO #TableB VALUES( 1, 'A', '0016' );
INSERT INTO #TableB VALUES( 2, 'A', '0027' );
INSERT INTO #TableB VALUES( 4, 'A', '0032' );
INSERT INTO #TableB VALUES( 4, 'A', '0032' );
IF OBJECT_ID('tempdb..#TableC') IS NOT NULL DROP TABLE #TableC
GO
CREATE TABLE #TableC
( Prod_Code VARCHAR(10)
, A_Trans_Code VARCHAR(10)
, Commission_Percent INTEGER )
INSERT INTO #TableC VALUES( '0016', 'Renewal', 5 );
INSERT INTO #TableC VALUES( '0027', 'Renewal', 5 );
INSERT INTO #TableC VALUES( '0032', 'New', 10 );
INSERT INTO #TableC VALUES( '0032', 'Renewal', 5 );
SELECT distinct a.*, c.commission_percent
FROM #TableA a
JOIN #TableB b ON a.reference = b.reference
AND a.value_name = b.value_name
JOIN #TableC c ON b.prod_code = c.prod_code
AND a.renewal_code = SUBSTRING(c.a_trans_code,1,1)
Results are--
Reference Value_Name Renewal_Code commission_percent
2 A R 5
4 A N 10
4 A R 5
Code to put in your sql
( SELECT distinct c.commission_percent
FROM TableA a
JOIN TableB b ON a.reference = b.reference
AND a.value_name = b.value_name
JOIN TableC c ON b.prod_code = c.prod_code
AND a.renewal_code = SUBSTRING(c.a_trans_code,1,1)
) Commission_Percent
WITH table_a AS (
SELECT 1 AS reference, 'A' AS value_name, 'N' AS renewal_code FROM DUAL UNION ALL
SELECT 2 AS reference, 'A' AS value_name, 'R' AS renewal_code FROM DUAL UNION ALL
SELECT 3 AS reference, 'B' AS value_name, 'N' AS renewal_code FROM DUAL UNION ALL
SELECT 4 AS reference, 'A' AS value_name, 'R' AS renewal_code FROM DUAL UNION ALL
SELECT 4 AS reference, 'A' AS value_name, 'N' AS renewal_code FROM DUAL
),
table_b AS (
SELECT 1 AS reference, 'A' AS value_name, '0016' AS prod_code FROM DUAL UNION ALL
SELECT 2 AS reference, 'A' AS value_name, '0027' AS prod_code FROM DUAL UNION ALL
/* SELECT 4 AS reference, 'A' AS value_name, '0032' AS prod_code FROM DUAL UNION ALL duplicate row excluded */
SELECT 4 AS reference, 'A' AS value_name, '0032' AS prod_code FROM DUAL
),
table_c AS (
SELECT '0016' AS a_prod_code, 'Renewal' AS a_tans_code, 5 AS commission_percent FROM DUAL UNION ALL
SELECT '0027' AS a_prod_code, 'Renewal' AS a_tans_code, 5 AS commission_percent FROM DUAL UNION ALL
SELECT '0032' AS a_prod_code, 'New' AS a_tans_code, 10 AS commission_percent FROM DUAL UNION ALL
SELECT '0032' AS a_prod_code, 'Renewal' AS a_tans_code, 5 AS commission_percent FROM DUAL
)
SELECT a.reference AS ref_a,
a.value_name AS value_name_a,
b.reference AS ref_b,
b.value_name AS value_name_b,
a.renewal_code,
b.prod_code,
c.commission_percent
FROM table_a a
LEFT OUTER JOIN table_b b
ON a.value_name = b.value_name
AND a.reference = b.reference
LEFT OUTER JOIN table_c c
ON SUBSTR(c.a_tans_code, 1, 1) = a.renewal_code
AND c.a_prod_code = b.prod_code
;
The common table expression (CTE) is just to create the same values you posted (CTE is the part using the WITH construct).
Results:
REF_A VALUE_NAME_A REF_B VALUE_NAME_B RENEWAL_CODE PROD_CODE COMMISSION_PERCENT
---------- --------------- ---------- --------------- --------------- ---------- --------------------
2 A 2 A R 0027 5
4 A 4 A N 0032 10
4 A 4 A R 0032 5
1 A 1 A N 0016
3 B N
You may have to check the values in the rows you posted that do not result in a join. This also assumes there are no codes:code_name relationships that violate the rule of first letter of code name = code.
Related
table1
Column A
Column B
A
New York
B
Istanbul
B
London
table2
Column A
Column B
A
New York
B
Istanbul
C
London
SELECT Column A From Table1
minus
SELECT Column A From Table2
RESULT -> C
I want to see result row so not only columnA
RESULT -> C LONDON
How can i handle it?
The way you put it:
Sample data:
SQL> with
2 table1 (cola, colb) as
3 (select 'A', 'New York' from dual union all
4 select 'B', 'Istanbul' from dual union all
5 select 'B', 'London' from dual
6 ),
7 table2 (cola, colb) as
8 (select 'A', 'New York' from dual union all
9 select 'B', 'Istanbul' from dual union all
10 select 'C', 'London' from dual
11 )
Query begins here:
12 select b.*
13 from table2 b
14 where not exists (select null
15 from table1 a
16 where a.cola = b.cola
17 );
C COLB
- --------
C London
SQL>
Move that select to the where clause of your query, as follows:
SELECT Column B FROM table
WHERE Column A IN (
SELECT Column A From Table
minus
SELECT Column A From Table
)
This is the most optimal query for it :
have proper indexes on columnA either btree(high selectivity) or bitmap(low selectivity)
select b.* from
table2 b left join table1 a
on b.ColumnA=a.ColumnA
where a.ColumnA is null;
in will be very slow if the tables cardinality is very large so Koen Lostrie answer is not optimal.
Littlefoot's answer is based on subquery which is also slower !!
Is there any better way achieve the below result other than using union all?
The table has millions of records, so looking for a better option where the result set is fetched once.
create table test_tab (
rec_id number(3),
p_code varchar2(5),
q_code varchar2(5),
r_code varchar2(5),
p_amt number(8),
q_amt number(8),
r_amt number(8)
);
delete from test_tab;
insert into test_tab (rec_id, p_code, q_code,r_code , p_amt,q_amt,r_amt)
values (1, 'p1','q1','r1',18,9,9);
insert into test_tab (rec_id, p_code, q_code,r_code , p_amt,q_amt,r_amt)
values (2, 'p2','q2','r2',28,6,4);
insert into test_tab (rec_id, p_code, q_code,r_code , p_amt,q_amt,r_amt)
values (3, 'p1',null,null,18,null,null);
insert into test_tab (rec_id, p_code, q_code,r_code , p_amt,q_amt,r_amt)
values (4, null,'q3','r3',null,9,9);
commit;
select rec_id, p_code,p_amt from test_tab where p_code is not null
union all
select rec_id, q_code,q_amt from test_tab where q_code is not null
union all
select rec_id, r_code,r_amt from test_tab where r_code is not null;
Result:
REC_ID
P_CODE
P_AMT
1
q1
9
1
p1
18
1
r1
9
2
p2
28
2
q2
6
2
r2
4
3
p1
18
4
q3
9
4
r3
9
This is a basic application of the unpivot operator, available since Oracle 11.1.
select rec_id, code, amt
from test_tab
unpivot ((code, amt) for ord in
((p_code, p_amt) as 1, (q_code, q_amt) as 2, (r_code, r_amt) as 3))
order by rec_id, ord -- if needed
;
REC_ID CODE AMT
---------- ----- ----------
1 p1 18
1 q1 9
1 r1 9
2 p2 28
2 q2 6
2 r2 4
3 p1 18
4 q3 9
4 r3 9
9 rows selected.
Notice a few things. I call the output columns code and amt - it makes no sense to have the prefix p_ in the output column names. Also, "exclude nulls" is the default in unpivot, so I didn't need to mention it explicitly (although it wouldn't hurt anything). Finally, while perhaps not critical, I also created a column ord to reflect column order, and ordered the rows in the output in the same order as you had the columns in the input.
You can use Hiearchy query and cross join as follows:
select * from (select rec_id,
case lvl when 1 then p_code when 2 then q_code else r_code end as p_code,
case lvl when 1 then p_amt when 2 then q_amt else r_amt end as p_amount
from test_tab
cross join (select level as lvl from dual connect by level <= 3) )
where p_code is not null
You can use a lateral join:
select x.*
from test_tab t cross join lateral
(select t.rec_id, t.p_code as code, t.p_amount as amount from dual union all
select t.rec_id, t.q_code, t.q_amount from dual union all
select t.rec_id, t.r_code, t.r_amount from dual
) x
where code is not null;
Technical, this still has a union all, but it is only scanning the original table once.
I am trying to combine the data of three tables but running into a minor issue.
Let's say we have 3 tables
Table A
ID | ID2 | ID3 | Name | Age
1 2x 4y John 23
2 7j Mike 27
3 1S1 6HH Steve 67
4 45 O8 Carol 56
Table B
| ID2 | ID3 | Price
2x 4y 23
7j 8uj 27
x4 Q6 56
Table C
|ID | Weight|
1 145
1 210
1 240
2 234
2 110
3 260
3 210
4 82
I want to get every record from table A of everyone who weighs 200 or more but they cannot be in table B. Table A and C are joined by ID. Table A and B are joined by either ID2 or ID3. ID2 and ID3 don't both have to necessarily be populated but at least 1 will. Either can be present or both and they will be unique. So expected result is
3 | 1S1 | 6HH | Steve| 67
Note that a person can have multiple weights but as long as at least one record is 200 or above they get pulled.
What I have so far
Select *
From tableA x
Where
x.id in (Select distinct y.id
From tableA y, tableC z
Where y.id = z.id
And z.weight >= '200'
And y.id not in (Select distinct h.id
From tableA h, tableB k
Where (h.id2 = k.id2 or h.id3 = k.id3)))
When I do this it seems to ignore the check on tableB and I get John, Mike and Steve. Any ideas? Sorry it's convoluted, this is what I have to work with. I am doing this in oracle by the way.
This sounds like exists and not exists. So a direct translation is:
select a.*
from tableA a
where exists (select 1 from tableC c where c.id = a.id and c.weight >= 200) and
not exists (select 1 from tableB b where b.id2 = a.id2 or b.id3 = a.id3);
Splitting the or into two separate subqueries can often improve performance:
select a.*
from tableA a
where exists (select 1 from tableC c where c.id = a.id and c.weight >= 200) and
not exists (select 1 from tableB b where b.id2 = a.id2) and
not exists (select 1 from tableB b where b.id3 = a.id3);
Here's what I came up with.
SELECT DISTINCT
A.ID,
A.ID2,
A.ID3,
A.Name,
A.Age
FROM
A
LEFT OUTER JOIN C ON C.ID = A.ID
LEFT OUTER JOIN B ON
B.ID2 = A.ID2
OR B.ID3 = A.ID3
WHERE
C.Weight >= 200
AND B.Price IS NULL
BELOW is test data
CREATE TABLE A
(
ID INT,
ID2 VARCHAR(3),
ID3 VARCHAR(3),
Name VARCHAR(10),
Age INT
);
INSERT INTO A VALUES (1, '2x', '4y', 'John', 23);
INSERT INTO A VALUES (2, '7j', NULL , 'Mike', 27);
INSERT INTO A VALUES (3, '1S1', '6HH', 'Steve', 67);
INSERT INTO A VALUES (4, '45', 'O8', 'Carol', 56);
CREATE TABLE B
(
ID2 VARCHAR(3),
ID3 VARCHAR(3),
Price INT
);
INSERT INTO B VALUES ('2x', '4y', 23);
INSERT INTO B VALUES ('7j', '8uj', 27);
INSERT INTO B VALUES ('x4', 'Q6', 56);
CREATE TABLE C
(
ID INT,
Weight INT
);
INSERT INTO C VALUES (1, 145);
INSERT INTO C VALUES (1, 210);
INSERT INTO C VALUES (1, 240);
INSERT INTO C VALUES (2, 234);
INSERT INTO C VALUES (2, 110);
INSERT INTO C VALUES (3, 260);
INSERT INTO C VALUES (3, 210);
INSERT INTO C VALUES (4, 82);
Select a.id, a.id2, a.id3
From table_a a
Left join table_c c on a.id = c.id
Where c.weight >=200
And not exists
(Select 1
From table_b b
Where a.id = b.id2
Or a.id = b.id3
);
I was beating to the answers, but I used INNER JOIN on tables a and c and a NOT EXISTS on table b.
--This first section is creating the test data
with Table_A (id, id2, id3, Name, age) as
(select 1, '2x', '4y', 'John', 23 from dual union all
select 2, '7j', null, 'Mike', 27 from dual union all
select 3, '1S1', '6HH', 'Steve', 67 from dual union all
select 4, '45', 'O8', 'Carol', 56 from dual),
Table_B(id2, id3, price) as
(select '2x', '4y', 23 from dual union all
select '7j', '8uj', 27 from dual union all
select 'x4', 'Q6', 56 from dual),
Table_C(id, weight) as
(select 1, 145 from dual union all
select 1, 210 from dual union all
select 1, 240 from dual union all
select 2, 234 from dual union all
select 2, 110 from dual union all
select 3, 260 from dual union all
select 3, 210 from dual union all
select 4, 82 from dual)
--Actual query starts here
select distinct a.*
from table_a a
--join to table c, include the weight filter
inner join table_c c on (a.id = c.id and c.weight >= 200)
where not exists -- The rest is the NOT EXISTS to exclude the values in table b
(select 1 from table_b b
where a.id2 = b.id2
or a.id3 = b.id3);
I have following table structure. I would like to get sum at each level from TAB2.
TAB1 stores hierarchy in level columns.
TAB1
----- ----- ---- ----
KEY L1 L2 L3
---- ----- ----- ----
A A
B A B
C A B C
D A B D
TAB2
-----
KEY TC
---- ----
A 10
B 11
C 6
D 12
X 11
Expected Output:
KEY SUM
---- ----
A 39
B 29
C 6
D 12
X 11
Here is SQLFiddle Link: LINK TO FIDDLE
Oracle Setup
Create table TAB1 (pKey varchar2(10),level1 varchar2(10),level2 varchar2(10),level3 varchar2(10),level4 varchar2(10));
insert into TAB1(pKey,level1) values('A','A');
insert into TAB1(pKey,level1,level2) values('B','A','B');
insert into TAB1(pKey,level1,level2,level3) values('C','A','B','C');
insert into TAB1(pKey,level1,level2,level3) values('D','A','B','D');
Create table TAB2 (pKey varchar(10), tc integer);
insert into TAB2(pKey,tc) values('A',10);
insert into TAB2(pKey,tc) values('B',11);
insert into TAB2(pKey,tc) values('C',6);
insert into TAB2(pKey,tc) values('D',12);
insert into TAB2(pKey,tc) values('X',11);
Query:
SELECT t2.pKey,
SUM( COALESCE( t4.TC, t2.tc ) ) AS tc
FROM tab2 t2
LEFT OUTER JOIN
tab1 t1
ON ( t2.pKey = t1.pKey )
LEFT OUTER JOIN
tab1 t3
ON ( t1.level1 = t3.level1
AND ( t1.level2 IS NULL OR t1.level2 = t3.level2 )
AND ( t1.level3 IS NULL OR t1.level3 = t3.level3 )
AND ( t1.level4 IS NULL OR t1.level4 = t3.level4 ) )
LEFT OUTER JOIN
tab2 t4
ON ( t3.pKey = t4.pKey )
GROUP BY t2.pKey;
Output:
PKEY TC
---------- ----------
D 12
A 39
B 29
C 6
X 11
In the solution provided below (including the input data as factored subqueries), first I show how to use unpivot and additional operations to normalize tab1 (the result is the factored subquery n for "normalized"). Then, if you had the data in normal form, the output could be obtained by a direct application of standard hierarchical querying as shown at the bottom of my code.
with
tab1 (key, L1, L2, L3) as (
select 'A', 'A', null, null from dual union all
select 'B', 'A', 'B' , null from dual union all
select 'C', 'A', 'B' , 'C' from dual union all
select 'D', 'A', 'B' , 'D' from dual
),
tab2 (key, TC) as (
select 'A', 10 from dual union all
select 'B', 11 from dual union all
select 'C', 6 from dual union all
select 'D', 12 from dual union all
select 'X', 11 from dual
),
unpiv (key, l, ancestor) as (
select key, to_number(substr(lv, 2)), ancestor from tab1
unpivot (ancestor for lv in (L1, L2, L3))
),
d (key, depth) as (
select key, max(l)
from unpiv
group by key
),
n (child, parent, TC) as (
select d.key, u.ancestor, tab2.TC
from unpiv u
right outer join d
on u.key = d.key and u.l = d.depth - 1
left outer join tab2
on d.key = tab2.key
)
SELECT key, sum(TC) as sum_TC
from (
select connect_by_root child as key, TC
from n
connect by prior child = parent
)
group by key
order by key;
Along the way, in unpiv, I already had all the parent-child relationships, so I could have joined that directly with tab2 on unpiv.key = tab2.key and summed TC grouping by ancestor (similar to MT0's solution). Instead, I wanted to demonstrate two separate steps: (1) normalizing tab1 and (2) how easy it is to use hierarchical queries on normalized tables.
Output:
KEY SUM_TC
--- ----------
A 39
B 29
C 6
D 12
Using Oracle 10g. I have two tables:
User Parent
-------------
1 (null)
2 1
3 1
4 3
Permission User_ID
-------------------
A 1
B 3
The values in the permissions table get inherited down to the children. I would like to write a single query that could return me something like this:
User Permission
------------------
1 A
2 A
3 A
3 A
3 B
4 A
4 B
Is it possible to formulate such a query using 10g connect .. by syntax to pull in rows from previous levels?
you can achieve the desired result with a connect by (and the function CONNECT_BY_ROOT that returns the column value of the root node):
SQL> WITH users AS (
2 SELECT 1 user_id, (null) PARENT FROM dual
3 UNION ALL SELECT 2, 1 FROM dual
4 UNION ALL SELECT 3, 1 FROM dual
5 UNION ALL SELECT 4, 3 FROM dual
6 ), permissions AS (
7 SELECT 'A' permission, 1 user_id FROM dual
8 UNION ALL SELECT 'B', 3 FROM dual
9 )
10 SELECT lpad('*', 2 * (LEVEL-1), '*')||u.user_id u,
11 u.user_id, connect_by_root(permission) permission
12 FROM users u
13 LEFT JOIN permissions p ON u.user_id = p.user_id
14 CONNECT BY u.PARENT = PRIOR u.user_id
15 START WITH p.permission IS NOT NULL
16 ORDER SIBLINGS BY user_id;
U USER_ID PERMISSION
--------- ------- ----------
3 3 B
**4 4 B
1 1 A
**2 2 A
**3 3 A
****4 4 A
You could take a look at http://www.adp-gmbh.ch/ora/sql/connect_by.html
Kind of black magic, but you can use table-cast-multiset to reference one table from another in WHERE clause:
create table t1(
usr number,
parent number
);
create table t2(
usr number,
perm char(1)
);
insert into t1 values (1,null);
insert into t1 values (2,1);
insert into t1 values (3,1);
insert into t1 values (4,3);
insert into t2 values (1,'A');
insert into t2 values (3,'B');
select t1.usr
, t2.perm
from t1
, table(cast(multiset(
select t.usr
from t1 t
connect by t.usr = prior t.parent
start with t.usr = t1.usr
) as sys.odcinumberlist)) x
, t2
where t2.usr = x.column_value
;
In the subquery x I construct a table of all parents for the given user from t1 (including itself), then join it with permissions for these parents.
Here is a example for just one user id. you can use proc to loop all.
CREATE TABLE a_lnk
(user_id VARCHAR2(5),
parent_id VARCHAR2(5));
CREATE TABLE b_perm
(perm VARCHAR2(5),
user_id VARCHAR2(5));
INSERT INTO a_lnk
SELECT 1, NULL
FROM DUAL;
INSERT INTO a_lnk
SELECT 2, 1
FROM DUAL;
INSERT INTO a_lnk
SELECT 3, 1
FROM DUAL;
INSERT INTO a_lnk
SELECT 4, 3
FROM DUAL;
INSERT INTO b_perm
SELECT 'A', 1
FROM DUAL;
INSERT INTO b_perm
SELECT 'B', 3
FROM DUAL;
-- example for just for user id = 1
--
SELECT c.user_id, c.perm
FROM b_perm c,
(SELECT parent_id, user_id
FROM a_lnk
START WITH parent_id = 1
CONNECT BY PRIOR user_id = parent_id
UNION
SELECT parent_id, user_id
FROM a_lnk
START WITH parent_id IS NULL
CONNECT BY PRIOR user_id = parent_id) d
WHERE c.user_id = d.user_id
UNION
SELECT d.user_id, c.perm
FROM b_perm c,
(SELECT parent_id, user_id
FROM a_lnk
START WITH parent_id = 1
CONNECT BY PRIOR user_id = parent_id
UNION
SELECT parent_id, user_id
FROM a_lnk
START WITH parent_id IS NULL
CONNECT BY PRIOR user_id = parent_id) d
WHERE c.user_id = d.parent_id;