如何通过组合两个 CTE 来获得所需的结果? [英] How to get desired result by combining two CTE's?

查看:21
本文介绍了如何通过组合两个 CTE 来获得所需的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 CTE,首先是

I have two CTE's,firstly

  ;WITH CTE AS (SELECT A.* 
                    , Row_NUMBER() Over (Partition by ID order by Date asc) RN 
             FROM TABLE A)
    SELECT Sum(Weight) as IN_WT
    FROM CTE 
    WHERE RN = 1 and name='dev1'

然后

 ;WITH CTE AS (SELECT B.* 
                    , Row_NUMBER() Over (Partition by ID order by Date desc) RN1 
             FROM TABLE B)
    SELECT Sum(Weight) AS out_wt
    FROM CTE 
    WHERE RN1 = 1 and name='dev1'

现在我需要合并输出并获得 in_wt、out_wt.我尝试合并两个 CTE,但没有得到想要的结果.我们该怎么做?

Now I had a reuiremnt that the output should be combined and get in_wt,out_wt.I tried combining both the CTE's but didn't get the desired result.How can we do that?

推荐答案

这是我找到的简单解决方案,

This was the simple Solution I Found,

一个'快速&脏"方法是将您的查询包装在另外两个 CTE 的 SELECT (SELECT IN_WT FROM CTE1), (SELECT OUT_WT FROM CTE2)

A 'quick & dirty' approach would be two wrap your queries in two other CTE's SELECT (SELECT IN_WT FROM CTE1), (SELECT OUT_WT FROM CTE2)

这篇关于如何通过组合两个 CTE 来获得所需的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆