我有这个声明,它返回2个不同的计数,我正在尝试加入这个月的计数。以下是我当前的陈述,我知道我的问题是COUNT(SecondColl。*)作为第二张桌子的STRAFT。我可以这样做吗?
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 :(得分: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