首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我有两个SQL查询。我需要将它们连接到一个SQL查询中。

问我有两个SQL查询。我需要将它们连接到一个SQL查询中。
EN

Stack Overflow用户
提问于 2022-01-03 12:33:36
回答 3查看 72关注 0票数 1

我有两个sql查询,需要合并成一个查询。当我试图将这两个查询合并成一个查询时,我没有得到正确的结果。请有人帮助将这两个查询合并成一个查询。

第一个SQL查询:

代码语言:javascript
复制
SELECT distinct order_no,status,status_cd,
case when order_qty - fulfilled_qty > 0 then 'inventory-unavailable' END
from t1 a, t2 b
where 
a.inventory_stockout='TRUE'
AND a.flag_column='TRUE' 
and status in '(fulfilled','pending')
GROUP BY order_no,status,status_cd;

第二个SQL查询:

代码语言:javascript
复制
SELECT distinct order_no,status,status_cd,
case when order_qty - fulfilled_qty > 0 then 'inventory-unavailable' END
from t1 a, t2 b
where 
a.inventory_stockout='TRUE'
AND a.flag_column='TRUE' 
and a.subscription_enabled='TRUE'
GROUP BY order_no,status,status_cd;
EN

回答 3

Stack Overflow用户

发布于 2022-01-03 12:38:42

如果它们--分别返回您想要的结果(我对此表示怀疑;您使用的是t1和t2之间的交叉连接),那么一个简单的选项是对它们进行union (移除distinct,删除group by (因为您没有聚合任何内容,但distinct做了工作),因为union在本例中替换了两者):

代码语言:javascript
复制
SELECT order_no,
       status,
       status_cd,
       CASE
          WHEN order_qty - fulfilled_qty > 0 THEN 'inventory-unavailable'
       END
  FROM t1 a, t2 b
 WHERE     a.inventory_stockout = 'TRUE'
       AND a.flag_column = 'TRUE'
       AND status IN ('fulfilled', 'pending')
UNION
SELECT order_no,
       status,
       status_cd,
       CASE
          WHEN order_qty - fulfilled_qty > 0 THEN 'inventory-unavailable'
       END
  FROM t1 a, t2 b
 WHERE     a.inventory_stockout = 'TRUE'
       AND a.flag_column = 'TRUE'
       AND a.subscription_enabled = 'TRUE';
票数 1
EN

Stack Overflow用户

发布于 2022-01-03 15:02:33

一个问题:我看到了t1和t2以及同义词,但没有看到连接条件或将t1和t2连接在一起的关键逻辑。您知道这些表之间的PK/FK关系吗?

建议:在大多数相同的条件和相同的案例逻辑的情况下,您都会碰到相同的表。因此,如果将其合并到一个查询中,那么您将对可能比UNION更快的表进行一次遍历。如果将一些()添加到和逻辑中,将使您能够这样做。你能试试这种款式,看看它是否管用吗?

-联合代码模型

代码语言:javascript
复制
SELECT distinct order_no,status,status_cd,
  CASE when order_qty - fulfilled_qty > 0 then 'inventory-unavailable' END
  FROM t1 a, t2 b
 WHERE t1.PK = t2.FK (Add...)
   AND a.inventory_stockout='TRUE'
   AND a.flag_column='TRUE' 
   AND (status in '(fulfilled','pending' OR (AND a.flag_column='TRUE' 
        AND a.subscription_enabled='TRUE'))
GROUP BY order_no,status,status_cd;
票数 1
EN

Stack Overflow用户

发布于 2022-01-15 05:53:06

即使您知道order_qty来自哪个表,如果您已经对两个表a & b进行了别名化,最好始终使用别名,以与您的意思相同。

而且,如果inventory_stockout & flag_column确实是字符串编码的布尔值,那么它们的性能也会很差。

同样不同的是一个组BY,但是如果您有一个组,您不需要逐个地声明组,因为所有的变量都是考虑的。另外,作为雪花注释,this_should_have_a_name在组中没有命名,但是它也不需要被命名,因为它是组中事物集合的导数值。

我假设您希望将结果集合在一起,您可以像littlefoot所指出的那样将它们连接起来( 友联市 ),但是这将在这两者之间运行一个不同的集合,这就是我假设您想要的结果。如果您知道这两个结果集中有零个重复项,那么UNION ALL会告诉DB将结果连接在一起,这样速度要快得多。

接下来您可以做的是一个CTE并将公共部分选择到一个块中,然后合并子结果如下:

代码语言:javascript
复制
WITH cte AS (
    SELECT DISTINCT 
        order_no, 
        status,
        status_cd,
        IFF( order_qty - fulfilled_qty > 0, 'inventory-unavailable', NULL) AS this_should_have_a_name,
        a.subscription_enabled
    FROM t1 AS a, t2 AS b
    WHERE a.inventory_stockout = 'TRUE'
        AND a.flag_column = 'TRUE' 
)
SELECT order_no, status, status_cd, this_should_have_a_name
FROM cte
WHERE status in '(fulfilled','pending')

UNION 

SELECT order_no, status, status_cd, this_should_have_a_name
FROM cte
WHERE subscription_enabled='TRUE'
;

现在我们看到它是这样写的,我们可以和CTE一起在里面放一个OR

代码语言:javascript
复制
WITH cte AS (
    SELECT DISTINCT 
        order_no, 
        status,
        status_cd,
        IFF( order_qty - fulfilled_qty > 0, 'inventory-unavailable', NULL) AS this_should_have_a_name,
        a.subscription_enabled
    FROM t1 AS a, t2 AS b
    WHERE a.inventory_stockout = 'TRUE'
        AND a.flag_column = 'TRUE' 
)
SELECT order_no, status, status_cd, this_should_have_a_name
FROM cte
WHERE ( status in '(fulfilled','pending')
    OR  subscription_enabled='TRUE' )
;

或者没有CTE

代码语言:javascript
复制
SELECT DISTINCT 
        order_no, 
        status,
        status_cd,
        IFF( order_qty - fulfilled_qty > 0, 'inventory-unavailable', NULL) AS this_should_have_a_name
    FROM t1 AS a, t2 AS b
    WHERE a.inventory_stockout = 'TRUE'
        AND a.flag_column = 'TRUE' 
        AND ( status in '(fulfilled','pending') 
            OR a.subscription_enabled='TRUE' )
;

或者更慢,现在逻辑被合并了--联盟所需的逻辑--都可以看作是

代码语言:javascript
复制
SELECT order_no, status, status_cd, this_should_have_a_name
FROM cte
WHERE status in '(fulfilled','pending')
    AND subscription_enabled != 'TRUE'

UNION ALL 

SELECT order_no, status, status_cd, this_should_have_a_name
FROM cte
WHERE subscription_enabled = 'TRUE'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70565841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档