Teradata wildcard join with modifying table instead of conditions - sql

I have tablea and tableb, that I need to join.
It can happen that in b.col2, b.col3 can be value '%', which should be something like wildcard, meaninng, that in this case we can join value of b.col2 on any value of a.col2 or value b.col3 on any value a.col3.
One solution would look like this:
select a.*, b.col4, b.col5
from tablea a
left join (select col1, col2, col3, col4, col5 tableb) b
on b.col1=a.col1 and
(b.col2 = a.col2 or b.col2 = '%') and
(b.col3 = a.col3 or b.col3 = '%')
qualify 1 = row_number() over (partition by a.id order by (case when b.col2 = '%' then 2 else 1 end), (case when b.col3 = '%' then 2, else 1 end))
My problem is that because of later use in different app, I can only use simple join conditions like:
b.col1 = a.col1 and
b.col2 = a.col2 and
b.col3 = a.col3
My question is, if there is a way, how to achieve the same result as in the first solution, but using 'simple' join conditions (a.col2=b.col2) and just making changes in selection of tableb?

Related

Update table based on multiple conditions without using CASE

I need to update a table based on multiple conditions and the update needs to be done in one update statement. In addition, the restriction is that I CANNOT use the following construct due to performance issues since there are about 18 CASE expressions in my update:
UPDATE A
SET A.col1 = CASE WHEN B.col = someValue THEN B.Col2 END,
B.Col2 = CASE WHEN b.col = someOtherValue THEN B.Col2 END,
.
.
--18th CASE stmt
B.Col18 = CASE WHEN b.col = YetAnotherValue THEN B.Col2 END
FROM
tableA A
INNER JOIN
tableB B ON A.someColumn = B.someColumn
Any suggestions will be appreciated .
I suspect that you actually want to aggregate before updating:
UPDATE A
SET A.col1 = B.col1,
B.Col2 = B.col2,
. . .
FROM tableA A JOIN
(SELECT B.someColumn,
MAX(CASE WHEN B.col = someValue THEN B.Col2 END) as col1,
MAX(CASE WHEN b.col = someOtherValue THEN B.Col2 END) as col2,
. . .
FROM tableB B
GROUP BY B.someColumn
) B
ON A.someColumn = B.someColumn

How to avoid DISTINCT in a query that joins multiple tables?

I want to avoid using DISTINCT and produce the same result for queries that join multiple tables.
Without DISTINCT, it produces the same row multiple times.
I already tried looking up how to avoid DISTINCT, but nothing seems to work for me, seemingly because my table is more complicated and joining multiple tables at the same time.
SELECT DISTINCT C.COL3, B.COL1, A.COL2, A.COL4, B.COL5 FROM C
INNER JOIN B
ON B.COL1 = C.COL1
INNER JOIN A
ON B.COL2 = A.COL2
ORDER BY C.COL3 ASC;
I know I have to use GROUP BY somehow, but I just can't wrap my head around it...
You can just group by all the columns (without having ay aggregation):
SELECT
C.COL3, B.COL1, A.COL2, A.COL4, B.COL5
FROM C
JOIN B ON B.COL1 = C.COL1
JOIN A ON B.COL2 = A.COL2
GROUP BY C.COL3, B.COL1, A.COL2, A.COL4, B.COL5 -- group by all selected columns
ORDER BY C.COL3 ASC
If you then wanted to aggregate over the de-duped rows of the above query, use it as a subquery. For example, to SUM(B.COL5) of the de-duped rows:
SELECT
COL3, COL1, COL2, COL4, SUM(COL5)
FROM (
SELECT
C.COL3, B.COL1, A.COL2, A.COL4, B.COL5
FROM C
JOIN B ON B.COL1 = C.COL1
JOIN A ON B.COL2 = A.COL2
GROUP BY C.COL3, B.COL1, A.COL2, A.COL4, B.COL5
) deduped
GROUP BY COL3, COL1, COL2, COL4
ORDER BY COL3 ASC
Are you getting multiple duplicate rows of the same data if you do not use DISTINCT? If so, this query worked for me when I was joining multiple asp net tables in order to show the user info, plus the roles within the site they are assigned to. Hopefully this can help you.
SELECT AspNetUsers.Id, AspNetRoles.Name as SiteRole,
AspNetRoles.ID as RoleID, AspNetUsers.UserName,
AspNetUsers.Email FROM AspNetUserRoles INNER JOIN
AspNetUsers ON AspNetUserRoles.UserId = AspNetUsers.Id INNER JOIN
AspNetRoles ON AspNetUserRoles.RoleId = AspNetRoles.Id
You can use row_number() partition by [column you want to be distinct].
select *
from (select c.col3, b.col1, a.col2, a.col4, b.col5
, row_number() over (partition by c.col1 order by c.col3) as rn
from c
inner join b on b.col1 = c.col1
inner join a on a.col2 = b.col2) t1
where t1.rn = 1
order by t1.col3
SELECT COL3, COL1, SUM(COL5)
FROM
(
SELECT DISTINCT C.COL3, B.COL1, A.COL2, A.COL4, B.COL5 FROM C
INNER JOIN B
ON B.COL1 = C.COL1
INNER JOIN A
ON B.COL2 = A.COL2
) X
GROUP BY COL3, COL1
ORDER BY COL3, COL1

Teradata wildcard in join definition

I have joined tables like bellow:
select a.*, b.col4, b.col5 from table a
inner join table b
on a.col2=b.col2
and a.col3=b.col3
It can happen that in b.col2, b.col3 can be value '*', which should be something like wildcard, meaninng, that in this case we can join value of b.col2 on any value of a.col2 or value b.col3 on any value a.col3.
Would you please help me define it?
It sounds like you have a default. One method is multiple comparison:
select a.*,
coalesce(b.col4, bdef3.col4, bdef2.col4, bdef.col4) as col4, b.col5
coalesce(b.col5, bdef3.col5, bdef2.col5, bdef.col5) as col5
from tablea a left join
tableb b
on b.col2 = a.col2 and b.col3 = a.col3 left join
tableb bdef3
on b.col2 = a.col2 and b.col3 = '*' left join
tableb bdef2
on b.col2 = '*' and b.col3 = a.col3 left join
tableb bdef
on b.col2 = '*' and b.col3 = '*';
You may want a where clause if you want to guarantee some match:
where (b.col2 is not null or bdef3.col2 is not null or bdef2.col2 is not null or bdef.col2 is not null)
I think the above is more efficient, but you can express this more succinctly as:
select a.*, b.col4, b.col5
from tablea a left join
tableb b
on (b.col2 = a.col2 or b.col2 = '*') and
(b.col3 = a.col3 or b.col3 = '*')
qualify 1 = row_number() over (partition by a.id order by (case when b.col2 = '*' then 2 else 1 end), (case when b.col3 = '*' then 2, else 1 end))

Oracle try second condition only if first one gave nothing in join

I'm looking in Oracle for a way to do something like if first condition has no match, do the second one. Let's get to an example.
I have this JOIN in a query :
SELECT A.COL1, A.COL2, B.COL1, B.COL2
FROM A
FULL JOIN (SELECT COL1, COL2 FROM B)
ON B.COL1 = A.COL1
OR B.COL2 = A.COL2
And you see, with this OR condition I can have two created joins for the same B object. And I don't want that. What I would like is something like :
if no result with B.COL1 = A.COL1
then let's try a join with B.COL2 = A.COL2
You need to include the negation of the condition B.COL1 = A.COL1 in the second filter. Naively, this is B.COL1 <> A.COL1 but this does not handle NULL values so you need to use B.COL1 <> A.COL1 OR B.COL1 IS NULL OR A.COL1 IS NULL like this:
SELECT A.COL1, A.COL2, B.COL1, B.COL2
FROM A
FULL JOIN (SELECT COL1, COL2 FROM B)
ON ( B.COL1 = A.COL1
OR ( ( B.COL1 <> A.COL1 OR B.COL1 IS NULL OR A.COL1 IS NULL )
AND B.COL2 = A.COL2
)
)
If B.COL1 = A.COL1 then return row. Or, if B.COL1 <> A.COL1 but B.COL2 = A.COL2 also return row.
SELECT A.COL1, A.COL2, B.COL1, B.COL2
FROM A
FULL JOIN (SELECT COL1, COL2 FROM B) B
ON (B.COL1 = A.COL1)
OR (B.COL1 <> A.COL1 and B.COL2 = A.COL2)

Dynamic SQL in WHERE clause

SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM TABLE A, TABLE B
WHERE B.COL1 = A.COL1
AND B.COL2 = A.COL2
AND B.COL3 - A.COL3
AND B.COL4 = A.COL4
Now I want to tune the SQL query, that whenever any of the Columns in Table B has field value 'ALL' the where clause will not come into picture.
i.e. When it has a distinct value it will match with both the tables, when the field value is 'ALL' then to exclude from the where clause.
Alternatively,
I Need B.COL1= A.COL1 (When B.COL1 <> 'ALL')
Else NO WHERE clause with B.Col1 = A.Col1 (When B.COL1 = 'ALL')
Use OR wisely:
SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM TABLE A, TABLE B
WHERE (B.COL1 = A.COL1 OR B.COL1='ALL')
AND (B.COL2 = A.COL2 OR B.COL2='ALL')
...
I would also suggest learning JOIN syntax.
Hi, You can use case statement to have a condition in where clause,
SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM TABLE A, TABLE B
WHERE B.COL1 =
CASE
WHEN B.COL1 <> 'ALL' THEN A.COL1
ELSE NULL
END
AND B.COL2 = A.COL2
AND B.COL3 - A.COL3
AND B.COL4 = A.COL4
You can achieve this with just IN:
SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM TABLE A, TABLE B
WHERE B.COL1 IN (A.COL1, 'ALL')
AND B.COL2 IN (A.COL2, 'ALL')
AND B.COL3 IN (A.COL3, 'ALL')
AND B.COL4 IN (A.COL4, 'ALL')
What is actually going on may be more clear with a more verbose version using AND/OR, but the logic is exactly the same
SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM TABLE A, TABLE B
WHERE (B.COL1 = A.COL1 OR B.COL1 = 'ALL')
AND (B.COL2 = A.COL2 OR B.COL2 = 'ALL')
AND (B.COL3 = A.COL3 OR B.COL3 = 'ALL')
AND (B.COL4 = A.COL4 OR B.COL4 = 'ALL')
Simple solution:
SELECT DISTINCT A.COL1, A.COL2, A.COL3, A.COL4
FROM TABLE A
INNER JOIN TABLE B ON
(B.COL1 = A.COL1 AND B.COL2 = A.COL2 AND B.COL3 = A.COL3 AND B.COL4 = A.COL4)
OR
('ALL' IN (B.COL1, B.COL2, B.COL3, B.COL4))
but, if you work with large tables that complex filtering could slow down very much the execution, so I suggest to use a different syntax for complex JOINs
SELECT DISTINCT *
FROM (
SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM A
INNER JOIN B
ON (B.COL1 = A.COL1 AND B.COL2 = A.COL2 AND B.COL3 = A.COL3 AND B.COL4 = A.COL4)
) J1
UNION -- ALL ?
SELECT DISTINCT *
FROM (
SELECT A.COL1, A.COL2, A.COL3, A.COL4
FROM A
INNER JOIN B ON ('ALL' IN (B.COL1, B.COL2, B.COL3, B.COL4))
) J2
This one should be much faster than previous one.
Also, I wonder about row duplicates.. with that syntax each row of table A will be added to result as many times as many rows in table B contains 'ALL'
I have added the DISTINCT clause to the SELECT to avoid duplicates (same problem affects UNION operator), so if you need duplicates, remove DISTINCT and use UNION ALL instead of UNION