如何将这两个查询合并为一个。?

时间:2012-12-10 07:39:52

标签: mysql

这是我的两个问题当我分别解雇它时,我给出了正确的答案但是当它们合并时它们没有给出相同的答案。请帮助他们合并

SELECT SUM(hours) AS S1
,operating_company
 FROM Hours WHERE operating_company='AP-OH' GROUP BY operating_company

SELECT COUNT(*) AS S1,event_type
,operating_company
 FROM Event WHERE operating_company='AP-OH' AND event_type='OSH Restricted' GROUP BY 
 operating_company,event_type

由于 RajeshB

2 个答案:

答案 0 :(得分:0)

你可以这样做:

SELECT
  e.event_type,
  e.operating_company,
  SUM(h.hours) AS S1,
  COUNT(e.*) AS S2
FROM Hours h 
INNER JOIN event e ON h.operating_company = e.operating_company
WHERE h.operating_company = 'AP-OH' 
  AND e.event_type        = 'OSH Restricted' 
GROUP BY e.operating_company, e.event_type;

答案 1 :(得分:0)

试试这个

  SELECT SUM(S1.hours) ,S1.operating_company ,S2.operating_company ,COUNT(S2.*),S2.event_type , S2.operating_company
   FROM Hours as S1 WHERE operating_company='AP-OH' 

   INNER JOIN event AS S2 
   ON    S2.operating_company = S1.operating_company
    WHERE S2.operating_company=S1.operating_company='AP-OH' AND S2.event_type='OSH Restricted'
   GROUP BY 
   S2.operating_company,S2.event_type