2011-11-18 106 views
3

我苦苦尋找的遠離這2個查詢組合...香港專業教育學院試圖subquerys,連接,聯合和有沒有運氣:(結合的MySQL查詢

查詢

SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created 
FROM master 
WHERE DATE(crtdtime) = '2011-11-16' 
GROUP BY cntldate, sd_class 

將會產生

+------------+----------+---------+ 
| cntldate | sd_class | created | 
+------------+----------+---------+ 
| 2011-11-16 | CUST  | 2226 | 
| 2011-11-16 | NET  |  238 | 
+------------+----------+---------+ 

查詢乙

SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored 
FROM master 
WHERE DATE(rstdtime) = '2011-11-16' 
GROUP BY cntldate, sd_class 

將會產生

+------------+----------+----------+ 
| cntldate | sd_class | restored | 
+------------+----------+----------+ 
| 2011-11-16 | CUST  |  2315 | 
| 2011-11-16 | NET  |  221 | 
+------------+----------+----------+ 

但想最終的結果是...

+------------+----------+---------+----------+ 
| cntldate | sd_class | created | restored | 
+------------+----------+---------+----------+ 
| 2011-11-16 | CUST  | 2226 |  2315 | 
| 2011-11-16 | NET  |  238 |  221 | 
+------------+----------+---------+----------+ 

將不勝感激任何幫助。

謝謝。

回答

0

使用

SELECT C.cntldate, C.sd_class, C.created, R.restored 
FROM 
(
SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created 
FROM master 
GROUP BY cntldate, sd_class 
) C 
INNER JOIN 
(
SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored 
FROM master 
GROUP BY cntldate, sd_class 
) R 
ON R.cntldate = C.cntldate AND R.sd_class = C.sd_class 
WHERE C.cntldate = '2011-11-16' AND R.cntldate = '2011-11-16' 
+0

謝謝大家對你的幫助是非常非常感謝,並與** Yahia的**版本,因爲它給了我最大的靈活性。 – mybigman

+0

@mybigman歡迎您:-)請不要忘記將upvote/mark標記爲已接受任何有幫助的答案(請參閱http://meta.stackexchange.com/questions/5234/how-does-accepting-an-回答工作)。 – Yahia

0

如何

select q1.cntldate, q1.sd_class, q1.created, q2.restored 
    from (
    SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created 
     FROM master 
     WHERE DATE(crtdtime) = '2011-11-16' 
     GROUP BY cntldate, sd_class 
    ) q1 
    inner join (
     SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored 
     FROM master 
     WHERE DATE(rstdtime) = '2011-11-16' 
     GROUP BY cntldate, sd_class 
) q2 
     on q1.cntldate=q2.cntldate and q1.sdclass = q2.sdclass 

這是Postgres的語法的,所以可能會發揮它得到它與MySQL

0

我相信這將工作運行:

SELECT DATE(m1.crtdtime) AS cntldate, 
m1.sd_class, 
COUNT(m1.crtdtime) AS created , 
(
    SELECT COUNT(m2.rstdtime) 
    FROM master m2 
    WHERE DATE(m2.rstdtime) = DATE(m1.rstdtime) 
    and m2.sd_class=m1.sd_class 
) as restored 
FROM master m1 
WHERE DATE(crtdtime) = '2011-11-16' 
GROUP BY cntldate, sd_class 
2

一種可能的方式

SELECT DATE(crtdtime) AS cntldate, 
    sd_class, 
    sum(DATE(crtdtime) = '2011-11-16') AS created 
    sum(DATE(rstdtime) = '2011-11-16') AS restored 
FROM master 
WHERE DATE(crtdtime) = '2011-11-16' or DATE(rstdtime) = '2011-11-16' 
GROUP BY cntldate, sd_class 
+0

我只是在那裏......但是,如果且僅當單一日期纔有效...... – DRapp

0

它實際上並不難(一旦你已經做了一段時間)。使用SUM(IF()),如...

SELECT 
     date(if(date(ctrdtime) = '2011-11-16', ctrdtime, rstdtime)) as CntlDate, 
     sd_class, 
     sum(if(date(ctrdtime) = '2011-11-16', 1, 0)) as CreatedCount, 
     sum(if(date(rstdtime) = '2011-11-16', 1, 0)) as RestoredCount 
    FROM 
     master 
    WHERE 
     DATE(crtdtime) = '2011-11-16' 
     OR DATE(rstdtime) = '2011-11-16' 
    GROUP BY 
     1, 2 

這也將工作,如果你擴展了雙方建立和恢復的日期範圍...