如何从表格行中推导筛选条件

huangapple go评论84阅读模式
英文:

how to derive filter condition from table rows

问题

如何根据以下类似的行来推导BTEQ中的过滤条件?

表B:

COLA COLB COLC
1    CODE  AAA
1    DESC  BBB
1    TYPE  CCC

这是一些代码:

SELECT * FROM TABLE A WHERE CODE IN ('AAA') AND DESC IN ('BBB') AND TYPE IN ('CCC')
英文:

How do i derive the filter condition in bteq based on rows like given below?

TABLE B:

COLA COLB COLC
1    CODE  AAA
1    DESC  BBB
1    TYPE  CCC

Here is some code:

SELECT * FROM TABLE A WHERE CODE IN ('AAA') AND DESC IN ('BBB') AND TYPE IN ('CCC')

答案1

得分: 2

SELECT
COLA
FROM yourTable
GROUP BY
COLA
HAVING
COUNT(CASE WHEN COLB = 'CODE' AND COLC = 'AAA' THEN 1 END) > 0 AND
COUNT(CASE WHEN COLB = 'DESC' AND COLC = 'BBB' THEN 1 END) > 0 AND
COUNT(CASE WHEN COLB = 'TYPE' AND COLC = 'CCC' THEN 1 END) > 0;

英文:

One approach here would be to aggregate by COLA, and then add three assertions for the key/values you expect:

SELECT
    COLA
FROM yourTable
GROUP BY
    COLA
HAVING
    COUNT(CASE WHEN COLB = 'CODE' AND COLC = 'AAA' THEN 1 END) > 0 AND
    COUNT(CASE WHEN COLB = 'DESC' AND COLC = 'BBB' THEN 1 END) > 0 AND
    COUNT(CASE WHEN COLB = 'TYPE' AND COLC = 'CCC' THEN 1 END) > 0;

答案2

得分: 0

你可以使用通用表达式重写查询。

或者使用派生表。

SELECT *
FROM
(
SELECT COLA, CASE WHEN COLB = 'Code' THEN COLC ELSE NULL END AS Code
           , CASE WHEN COLB = 'DESC' THEN COLC ELSE NULL END AS DESC
           , CASE WHEN COLB = 'TYPE' THEN COLC ELSE NULL END AS TYPE
FROM TABLEB
) AS t
WHERE  CODE IN ('AAA') AND DESC IN ('BBB') AND TYPE IN ('CCC');
英文:

You can re-write query using common table expression.

WITH CTE_TableB(ColA, Code, DESC,TYPE) AS
(
SELECT COLA, CASE WHEN COLB = 'Code' THEN COLC ELSE NULL AS Code
           , CASE WHEN COLB = 'DESC' THEN COLC ELSE NULL AS DESC
           , CASE WHEN COLB = 'TYPE' THEN COLC ELSE NULL AS TYPE
FROM TABLEB
)
SELECT *
FROM CTE_TableB
WHERE  CODE IN ('AAA') AND DESC IN ('BBB') AND TYPE IN ('CCC');

Or using derived table

SELECT *
FROM
(
SELECT COLA, CASE WHEN COLB = 'Code' THEN COLC ELSE NULL AS Code
           , CASE WHEN COLB = 'DESC' THEN COLC ELSE NULL AS DESC
           , CASE WHEN COLB = 'TYPE' THEN COLC ELSE NULL AS TYPE
FROM TABLEB
) AS t
WHERE  CODE IN ('AAA') AND DESC IN ('BBB') AND TYPE IN ('CCC');

答案3

得分: 0

使用表B中的值来过滤表A,并假设表B中没有值的列表示不对该列进行过滤,您可以这样做:

SELECT *
  FROM A
 WHERE ( NOT EXISTS ( SELECT * FROM B WHERE B.COLB = 'CODE' )
      OR A.CODE IN ( SELECT B.COLC FROM B WHERE B.COLB = 'CODE' ) )
   AND ( NOT EXISTS ( SELECT * FROM B WHERE B.COLB = 'DESC' )
      OR A.DESC IN ( SELECT B.COLC FROM B WHERE B.COLB = 'DESC' ) )
   AND ( NOT EXISTS ( SELECT * FROM B WHERE B.COLB = 'TYPE' )
      OR A.TYPE IN ( SELECT B.COLC FROM B WHERE B.COLB = 'TYPE' ) )
英文:

To filter table A using the values in table B, and assuming that a column without values in table B means unfiltered on that column, you can do it like this:

SELECT *
  FROM A
 WHERE ( NOT EXISTS ( SELECT * FROM B WHERE B.COLB = 'CODE' )
      OR A.CODE IN ( SELECT B.COLC FROM B WHERE B.COLB = 'CODE' ) )
   AND ( NOT EXISTS ( SELECT * FROM B WHERE B.COLB = 'DESC' )
      OR A.DESC IN ( SELECT B.COLC FROM B WHERE B.COLB = 'DESC' ) )
   AND ( NOT EXISTS ( SELECT * FROM B WHERE B.COLB = 'TYPE' )
      OR A.TYPE IN ( SELECT B.COLC FROM B WHERE B.COLB = 'TYPE' ) )

答案4

得分: 0

您可以使用inexists来实现此目的:

select a.*
from a 
where a.code in (select b.colc from b where b.colb = 'CODE') and
      a.desc in (select b.colc from b where b.colb = 'DESC') and
      a.type in (select b.colc from b where b.colb = 'TYPE');

唯一的注意事项是,这假定表b中至少有一个每个值。

您可以处理缺失的值。一种方法是使用额外的子查询:

select a.*
from a 
where (a.code in (select b.colc from b where b.colb = 'CODE') or
       not exists (select 1 from b where b.colb = 'CODE')
      ) and
      (a.desc in (select b.colc from b where b.colb = 'DESC') or
       not exists (select 1 from b where b.colb = 'DESC')
      ) and
      (a.type in (select b.colc from b where b.colb = 'TYPE') or
       not exists (select 1 from b where b.colb = 'TYPE')
      );
英文:

You can use in or exists for this purpose:

select a.*
from a 
where a.code in (select b.colc from b where b.colb = 'CODE') and
      a.desc in (select b.colc from b where b.colb = 'DESC') and
      a.type in (select b.colc from b where b.colb = 'TYPE');

The one caveat is that this assumes that there is at least one of each value in table b.

You can handle missing values. One method uses additional subqueries:

select a.*
from a 
where (a.code in (select b.colc from b where b.colb = 'CODE') or
       not exists (select 1 from b where b.colb = 'CODE')
      ) and
      (a.desc in (select b.colc from b where b.colb = 'DESC') or
       not exists (select 1 from b where b.colb = 'DESC')
      ) and
      (a.type in (select b.colc from b where b.colb = 'TYPE') or
       not exists (select 1 from b where b.colb = 'TYPE')
      );

huangapple
  • 本文由 发表于 2020年1月3日 16:05:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定