简体   繁体   中英

How to combine these two Queries in single one.?

these are my two queries When i fired them separately i gives correct response but when combined they do not give the same response. Please help to combine them

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

Thanks RajeshB

You can do this:

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;

try this

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM