简体   繁体   English

SAP HANA中的SQL查询

[英]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 我有一个数据库,有2个称为DWFR和DWFR3的架构,我需要做的是比较2个架构,并获取匹配的表名以及DWFR架构中存在的表,该表在DWFR3上不可用,并且具有以下条件

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. 这将获得匹配的表名,但我也希望DWFR中有可用的表名,而不是DWFR3上的表名。

You can use a LEFT OUTER JOIN for selecting all data rows from a set and matchings from a second set. 您可以使用LEFT OUTER JOIN从一组中选择所有数据行,并从第二组中选择匹配项。

I used a SQL CTE Common Table Expression in the script, you can think of it as a sub-select with additional uses 我在脚本中使用了SQL CTE通用表表达式,您可以将其视为具有其他用途的子选择

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 并且您还需要在上面的脚本中附加其他要求或WHERE子句

For the reason that I used CTE can be explained as follows. 由于我使用CTE的原因,可以解释如下。 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 不幸的是,HANA似乎不通过JOIN的ON子句支持其他过滤条件

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 由于这个原因,我必须在应用ON,join条件之前过滤JOIN的LEFT部分

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: 要在DWFR中找到表名,而在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%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM