加入两个过滤的CTE

时间:2017-02-28 17:59:21

标签: sql-server common-table-expression

我将此作为我的第一个CTE:

WITH CTA (DateB, YrMnB, NAVB, Rank)
AS
(
SELECT Date, YrMn, NAV, RANK () OVER (PARTITION BY YrMn ORDER BY Date) AS Rank
FROM Portfolio.DailyStats
)  
SELECT DateB, YrMnB, NAVB, Rank
FROM CTA
WHERE Rank =1 
ORDER BY DateB DESC

这给了我下表:

+----------+---------+----------+------+
|  DateB   |  YrMnB  |   NAVB   | Rank |
+----------+---------+----------+------+
| 2/1/2017 | 2017-02 | 33270551 |    1 |
| 1/3/2017 | 2017-01 | 35085249 |    1 |
+----------+---------+----------+------+

这是我的第二次CTE:

WITH
CTB (DPDate, YrMn, PNL)
AS
(
SELECT Date, CONVERT(varchar(7),Date) AS YrMn, SUM (GLPeriod) AS PNL
FROM Portfolio.DailyPortfolio
GROUP BY Date
)
SELECT ctb.DPDate, ctb.YrMn, ctb.PNL, ds. SP500, ds.NetExp
FROM CTB ctb
JOIN Portfolio.DailyStats ds
ON ctb.DPDate = ds.Date
WHERE ds.SP500 < 0
ORDER BY ctb.DPDate DESC
GO

这给了我第二张表:

+-----------+---------+---------+---------+--------+
|  DPDate   |  YrMn   |   PNL   |  SP500  | NetExp |
+-----------+---------+---------+---------+--------+
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 |
| 2/16/2017 | 2017-02 | -111896 | -0.0009 | 0.6608 |
| 2/6/2017  | 2017-02 | -469190 | -0.0021 | 0.6087 |
| 1/31/2017 | 2017-01 |   61991 | -0.0009 | 0.5007 |
| 1/30/2017 | 2017-01 |   41431 | -0.006  | 0.6032 |
| 1/27/2017 | 2017-01 | -925463 | -0.0009 | 0.5879 |
| 1/26/2017 | 2017-01 |  -75744 | -0.0007 | 0.6266 |
| 1/23/2017 | 2017-01 | -277345 | -0.0027 | 0.6648 |
| 1/19/2017 | 2017-01 |   35323 | -0.0036 | 0.6575 |
| 1/17/2017 | 2017-01 | -242976 | -0.003  | 0.6602 |
| 1/12/2017 | 2017-01 | -351322 | -0.0021 | 0.5656 |
| 1/9/2017  | 2017-01 | -424598 | -0.0035 | 0.6165 |
| 1/5/2017  | 2017-01 | -696164 | -0.0008 | 0.5793 |
+-----------+---------+---------+---------+--------+

我想加入这两个表,以便我的最终结果如下:

+-----------+---------+---------+---------+--------+----------+
|  DPDate   |  YrMn   |   PNL   |  SP500  | NetExp |   NAVB   |
+-----------+---------+---------+---------+--------+----------+
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 33270551 |
| 2/16/2017 | 2017-02 | -111896 | -0.0009 | 0.6608 | 33270551 |
| 2/6/2017  | 2017-02 | -469190 | -0.0021 | 0.6087 | 33270551 |
| 1/31/2017 | 2017-01 |   61991 | -0.0009 | 0.5007 | 35085249 |
| 1/30/2017 | 2017-01 |   41431 | -0.006  | 0.6032 | 35085249 |
| 1/27/2017 | 2017-01 | -925463 | -0.0009 | 0.5879 | 35085249 |
| 1/26/2017 | 2017-01 |  -75744 | -0.0007 | 0.6266 | 35085249 |
| 1/23/2017 | 2017-01 | -277345 | -0.0027 | 0.6648 | 35085249 |
| 1/19/2017 | 2017-01 |   35323 | -0.0036 | 0.6575 | 35085249 |
| 1/17/2017 | 2017-01 | -242976 | -0.003  | 0.6602 | 35085249 |
| 1/12/2017 | 2017-01 | -351322 | -0.0021 | 0.5656 | 35085249 |
| 1/9/2017  | 2017-01 | -424598 | -0.0035 | 0.6165 | 35085249 |
| 1/5/2017  | 2017-01 | -696164 | -0.0008 | 0.5793 | 35085249 |
+-----------+---------+---------+---------+--------+----------+

这是我写的加入两个表的代码:

 WITH CTA (DateB, YrMnB, NAVB, Rank)
 AS
 (
 SELECT Date, YrMn, NAV, RANK () OVER (PARTITION BY YrMn ORDER BY Date) AS  Rank
FROM Portfolio.DailyStats
),

CTB (DPDate, YrMn, PNL)
AS
(
SELECT Date, CONVERT(varchar(7),Date) AS YrMn, SUM (GLPeriod) AS PNL
FROM Portfolio.DailyPortfolio
GROUP BY Date
)
SELECT ctb.DPDate, ctb.YrMn, ctb.PNL, ds. SP500, ds.NetExp, cta.NAVB
FROM CTB ctb
FULL JOIN CTA
N Cta.YrMnB = ctb.YrMn
JOIN Portfolio.DailyStats ds
ON ctb.DPDate = ds.Date
WHERE ds.SP500 < 0 OR cta.Rank=1
ORDER BY ctb.DPDate desc

我的结果最终会像这样出现:

+-----------+---------+---------+---------+--------+----------+
|  DPDate   |  YrMn   |   PNL   |  SP500  | NetExp |   NAVB   |
+-----------+---------+---------+---------+--------+----------+
| 2/27/2017 | 2017-02 |  330524 | 0.001   | 0.6197 | 33270551 |
| 2/24/2017 | 2017-02 |  435853 | 0.0015  | 0.6403 | 33270551 |
| 2/23/2017 | 2017-02 | -307945 | 0.0004  | 0.6486 | 33270551 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 33270551 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 33219634 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 34050018 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 33685689 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 33669105 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 33608570 |
| 2/22/2017 | 2017-02 | -333191 | -0.0011 | 0.6739 | 34544129 |
+-----------+---------+---------+---------+--------+----------+

1 个答案:

答案 0 :(得分:1)

你可以将它们分开并在最后进行加入:

WITH
CTB (DPDate, YrMn, PNL) AS (
    SELECT Date, CONVERT(varchar(7),Date) AS YrMn, SUM (GLPeriod) AS PNL
    FROM Portfolio.DailyPortfolio
    GROUP BY Date
),
CTB2 as (
    SELECT ctb.DPDate, ctb.YrMn, ctb.PNL, ds. SP500, ds.NetExp
    FROM CTB ctb
    JOIN Portfolio.DailyStats ds
    ON ctb.DPDate = ds.Date
    WHERE ds.SP500 < 0
    ORDER BY ctb.DPDate DESC
),
CTA (DateB, YrMn, NAVB, Rank) AS (
    SELECT Date, YrMn, NAV, RANK () OVER (PARTITION BY YrMn ORDER BY Date) AS Rank
    FROM Portfolio.DailyStats
), 
CTA2 as (
    SELECT DateB, YrMn, NAVB, Rank
    FROM CTA
    WHERE Rank =1 
    ORDER BY DateB DESC
)
select b.*, a.navb 
from CTB2 b
join CTA2 a on b.YrMn = a.YrMn