Dilip Deenadayalan
Dilip Deenadayalan

Reputation: 17

SQL Query in sap hana

I have a database and have 2 schema called DWFR and DWFR3 , what i need to do is compare 2 schmea and take the matching table name and also the table present in DWFR schema which is not available on DWFR3 and also with the below condition

SELECT schema_name, table_name 
FROM tables 
WHERE schema_name = 'DWFR' 
    AND table_name NOT LIKE 'RA%' 
    AND table_name NOT LIKE 'ZZ%' 
    AND table_name NOT LIKE 'DELETE%' 
    AND table_name NOT LIKE '%TEST%' 
    AND table_name NOT LIKE 'XX%' 
    AND table_name NOT LIKE 'ROB%' 
    AND table_name IN (
            SELECT table_name 
            FROM tables 
            WHERE schema_name = 'DWFR3' 
                AND table_name NOT LIKE 'RA%'
                AND table_name NOT LIKE 'ZZ%'
                AND table_name NOT LIKE 'DELETE%' 
                AND table_name NOT LIKE '%TEST%' 
                AND table_name NOT LIKE 'XX%'
                AND table_name NOT LIKE 'ROB%'
        )
ORDER BY schema_name, table_name;

This will get the matched table names but i also want table name available in DWFR which is not their on DWFR3.

Upvotes: 0

Views: 2360

Answers (2)

Eralper
Eralper

Reputation: 6622

You can use a LEFT OUTER JOIN for selecting all data rows from a set and matchings from a second set.

I used a SQL CTE Common Table Expression in the script, you can think of it as a sub-select with additional uses

Here is how you can use for your case

with t2 as (
    select table_name
    from tables
    where schema_name = 'DWFR3'
)
select t1.table_name, t2.table_name
from tables t1
left outer join t2
    on t1.table_name = t2.table_name
where t1.schema_name = 'DWFR'

And you also need the append the additional requirements or WHERE clauses into above script

For the reason that I used CTE can be explained as follows. In fact, the actual solution should be as simple as following. Unfortunately, HANA seems to not supporting additional filtering criteria with ON clause on JOINs

select t1.table_name, t2.table_name
from tables t1
left outer join tables t2
    on t1.table_name = t2.table_name and t2.schema_name = 'DWFR3
where t1.schema_name = 'DWFR'

Because of this reason, I had to filter the LEFT part of the JOIN earlier than applying the ON, join condition

Upvotes: 1

Ron Ballard
Ron Ballard

Reputation: 701

To find matched table names in both schemas:

select
    'DWFR' as schema_name,
    table_name
from
    (
        select table_name from tables where schema_name = 'DWFR'
        intersect
        select table_name from tables where schema_name = 'DWFR3'
    ) x
where
    table_name not like 'RA%' and
    table_name not like 'ZZ%' and
    table_name not like 'DELETE%' and
    table_name not like '%TEST%' and
    table_name not like 'XX%' and
    table_name not like 'ROB%';

To find table names in DWFR but not in DWFR3:

select
    'DWFR' as schema_name,
    table_name
from
    (
        select table_name from tables where schema_name = 'DWFR'
        except
        select table_name from tables where schema_name = 'DWFR3'
    ) x
where
    table_name not like 'RA%' and
    table_name not like 'ZZ%' and
    table_name not like 'DELETE%' and
    table_name not like '%TEST%' and
    table_name not like 'XX%' and
    table_name not like 'ROB%';

Upvotes: 0

Related Questions