2017-09-14 103 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