2017-09-14 71 views
1

我有这个声明返回2个不同的计数,我想加入这些计数关于这个月。下面是我目前的说法,我知道我的问题是COUNT(SecondColl。*)作为第二张表的STRAFT。我可以做这样的事吗?sql加入计数

WITH cte as(
    SELECT * FROM K1 
    UNION ALL 
    SELECT * FROM K2 
    UNION ALL 
    SELECT * FROM K3 
    UNION ALL 
    SELECT * FROM K4 
), FirstColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
      from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'STR' 
), SecondColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
      from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'NCD' 
) 
SELECT MONTH(ED) as STRMnth, COUNT(*) as STRRFT , COUNT(SecondColl.*) as STRAFT 
FROM FirstColl 
inner join SecondColl where MONTH(ED) = SecondColl.MONTH(ED) 
group by MONTH(ED) 
order by STRMnth asc 
+0

添加一些样品台DAT a和预期结果 - 作为格式化文本。 (即没有图像) – jarlh

回答

0

试试这个:

WITH cte as(
    SELECT * FROM K1 
    UNION ALL 
    SELECT * FROM K2 
    UNION ALL 
    SELECT * FROM K3 
    UNION ALL 
    SELECT * FROM K4 
), FirstColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
      from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'STR' 
), SecondColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
      from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'NCD' 
) 
select 
    a.STRMnth, a.cnt1 as STRRFT, b.cnt2 as STRAFT 
from 
    (select month(ED) as STRMnth, count(*) as cnt1 from FirstColl group by month(ED)) a 
    left join 
    (select month(ED) as STRMnth, count(*) as cnt2 from SecondColl group by month(ED)) b 
     on a.STRMnth = b.STRMnth 
order by a.STRMnth asc 

UPDATE1:

WITH cte as(
     SELECT * FROM K1 
     UNION ALL 
     SELECT * FROM K2 
     UNION ALL 
     SELECT * FROM K3 
     UNION ALL 
     SELECT * FROM K4 
    ), FirstColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
       from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'STR' 
    ), SecondColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
       from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'NCD' 
    ), ThirdColl as (SELECT Distinct ED, DP, RN FROM cte WHERE DT = 'STR') 
    select 
     a.STRMnth, a.cnt1 as STRRFT, b.cnt2 as STRAFT 
    from 
     (select month(ED) as STRMnth, count(*) as cnt1 from FirstColl group by month(ED)) a 
     left join 
     (select month(ED) as STRMnth, count(*) as cnt2 from SecondColl group by month(ED)) b 
      on a.STRMnth = b.STRMnth 
     left join 
     (select month(ED) as STRMnth, count(*) as cnt3 from ThirdColl group by month(ED)) c 
      on a.STRMnth = c.STRMnth 
    order by a.STRMnth asc 

UPDATE2:

WITH cte as(
     SELECT * FROM K1 
     UNION ALL 
     SELECT * FROM K2 
     UNION ALL 
     SELECT * FROM K3 
     UNION ALL 
     SELECT * FROM K4 
    ), FirstColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
       from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'STR' 
    ), SecondColl as (SELECT * FROM (Select DP, RN, ET, ED, DT, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count 
       from cte) a WHERE your_count = 1 and ET = 'Complete' and DT = 'NCD' 
    ), ThirdColl as (SELECT distinct ED, DP, RN FROM K1 WHERE Defect_Type = 'STR') 
    select 
     a.STRMnth, a.cnt1 as STRRFT, b.cnt2 as STRAFT 
    from 
     (select month(ED) as STRMnth, count(*) as cnt1 from FirstColl group by month(ED)) a 
     left join 
     (select month(ED) as STRMnth, count(*) as cnt2 from SecondColl group by month(ED)) b 
      on a.STRMnth = b.STRMnth 
     left join 
     (select month(ED) as STRMnth, count(*) as cnt3 from ThirdColl group by month(ED)) c 
      on a.STRMnth = c.STRMnth 
    order by a.STRMnth asc 
+1

由于ED未在agg功能 – Ryan

+0

中收到错误,对不起,请检查更新。 –

+0

我想添加一个额外的专栏与这个陈述与选择不同,你能指出我在这个正确的方向吗?这是我正在尝试STRTotalColl(选择不同DP,RN从cte 哪里DT ='STR' ) – Ryan