2017-06-16 108 views
-4

缺陷
25-10-2016
25-10-2016
26-10-2016
SQL服務器:連接查詢多個查詢

生產
25-10-2016
26-10 -2016
26-10-2016
26-10-2016

我怎麼能寫查詢下面OUTPUT ...

  OUTPUT    的d_count       P_COUNT
25-10-2016                                                 26-10-2016                                                

請幫我

我嘗試這樣做:

Select 
    QD.defect_time, 
    Count(OD.start_time) as production_count, 
    Count(QD.defect_time) as defect_count 
from QUALITY_DEFECT_1 QD 
JOIN ORDER_DETAILS OD ON QD.order_id = OD.order_id 
group by QD.defect_time 
+2

你嘗試過什麼?你能提供你現有的嘗試嗎? – Michal

+0

我想寫的sql查詢給我提到的輸出: –

+2

當然,但你已經嘗試過什麼?我們會幫助你,但我們不在這裏爲你編碼。 –

回答

0

您可以從像這樣開始:

SELECT A.COL1, B.D_COUNT, C.P_COUNT 
FROM 
(SELECT DISTINCT DEF_COL AS COL1 FROM DEFECT 
UNION 
SELECT DISTINCT PROD_COL FROM PRODUCTION 
) A 
LEFT JOIN (SELECT DEF_COL, COUNT(*) AS D_COUNT FROM DEFECT GROUP BY DEF_COL) B ON A.COL1 = B.DEF_COL 
LEFT JOIN (SELECT PROD_COL, COUNT(*) AS P_COUNT FROM PROUCTION GROUP BY PROD_COL) C ON A.COL1 = C.PROD_COL 
0

你需要Full Outer Join

SELECT COALESCE(d.dates, p.dates), 
     d.cnt , 
     p.cnt 
FROM (SELECT dates,Count(dates) cnt 
     FROM DEFECT 
     GROUP BY dates) d 
     FULL OUTER JOIN (SELECT dates,Count(dates) cnt 
         FROM PRODUCTION 
         GROUP BY dates) p 
        ON d.dates = p.dates 
0
SELECT 

ISNULL(DEFECT.D_OUTPUT, PRODUCTION.D_OUTPUT) AS D_OUTPUT 
,ISNULL(DEFECT.D_COUNT,0)    AS D_COUNT 
,ISNULL(PRODUCTION.P_COUNT,0)   AS P_COUNT 

FROM 
(

SELECT 
DateD  AS D_OUTPUT 
,COUNT(*) AS D_COUNT 
FROM DEFECT 
GROUP BY DateD 

) DEFECT 

FULL JOIN 
(

SELECT 
DateP  AS D_OUTPUT 
,COUNT(*) AS P_COUNT 
FROM PRODUCTION 
GROUP BY DateP 

) PRODUCTION ON PRODUCTION.D_OUTPUT = DEFECT.D_OUTPUT 

我希望它能爲你工作。

+0

我們可以在沒有子查詢的情況下編寫它嗎? –

+0

@@ Shamsndar Gawade行。我會再次編輯。 –

+0

@@ Shamsndar Gawade我編輯過。 –

0

沒有完全理解這個問題,但如果缺陷和生產不同的表,那麼你可以嘗試----

select PRODUCTION As OUTPUT,count(*) P_COUNT from PRODUCTION 
group by PRODUCTION 


select DEFECT As OUTPUT,count(*) D_COUNT from DEFECT 
group by DEFECT 
0
;With DEFECT(DEFECT_Date) 
AS 
(
SELECT '25-10-2016' Union all 
SELECT '25-10-2016' Union all 
SELECT '26-10-2016' 
) 
,PRODUCTION (PRODUCTION_Date) 
AS 
(
SELECT '25-10-2016' UNION ALL 
SELECT '26-10-2016' UNION ALL 
SELECT '26-10-2016' UNION ALL 
SELECT '26-10-2016' 
) 
,CTe3 
AS 
(
SELECT DISTINCT DEFECT_Date 
    ,COUNT(DEFECT_Date) OVER (
     PARTITION BY DEFECT_Date ORDER BY DEFECT_Date 
     ) AS D_COUNT 
    ,NULL AS P_COUNT 
FROM DEFECT D 

UNION ALL 

SELECT DISTINCT PRODUCTION_Date 
    ,NULL 
    ,COUNT(PRODUCTION_Date) OVER (
     PARTITION BY PRODUCTION_Date ORDER BY PRODUCTION_Date 
     ) AS P_COUNT 
FROM PRODUCTION D) 
    ,FINAL AS (
     SELECT DEFECT_Date 
      ,D_COUNT 
     FROM CTe3 
     WHERE D_COUNT IS NOT NULL 
     ) 

SELECT C.DEFECT_Date 
     ,F.D_COUNT 
     ,c.P_COUNT 

FROM CTe3 c 
FULL JOIN FINAL F ON C.DEFECT_Date = F.DEFECT_Date 
WHERE P_COUNT IS NOT NULL 

輸出

DEFECT_Date D_COUNT P_COUNT 
----------------------------- 
25-10-2016  2  1 
26-10-2016  1  3