DEFECT
25-10-2016
25-10-2016
26-10-2016
PRODUCTION
25-10-2016
26-10-2016
26-10-2016
26-10-2016
如何在OUTPUT下面编写查询...
OUTPUT D_COUNT P_COUNT
25-10-2016 2 1
26-10-2016 1 3
请帮帮我
我试过了:
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
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
我希望它对你有用。
答案 3 :(得分:0)
不完全理解这个问题但是如果DEFECT和PRODUCTION不同的表那么你可以尝试----
select PRODUCTION As OUTPUT,count(*) P_COUNT from PRODUCTION
group by PRODUCTION
select DEFECT As OUTPUT,count(*) D_COUNT from DEFECT
group by DEFECT
答案 4 :(得分: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