试图加入3个陈述

时间:2016-05-13 18:25:00

标签: mysql sql

SELECT       INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT = 'Mobl Hlth') AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

SELECT       INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT = 'MHV') AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

SELECT       INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT = 'eBenefits') AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

每个代码都提供了我需要的不同信息

4 个答案:

答案 0 :(得分:1)

您可以将WHERE子句组合在一起:

SELECT INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE SR_AREA IS NOT NULL
AND CREATED BETWEEN '10/1/2014' AND '09/30/2015'
AND INS_PRODUCT IN ('Mobl Hlth', 'MHV', 'eBenefits')
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

或者你可以将它们结合在一起:

SELECT INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT = 'Mobl Hlth') AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

UNION ALL

SELECT INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT = 'MHV') AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

UNION ALL

SELECT INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT = 'eBenefits') AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

有两点需要注意:UNION ALL当您知道您的记录不重叠时。这可以避免DB检查重复项。另外,最后只放置一个ORDER BY

答案 1 :(得分:1)

SELECT INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE INS_PRODUCT IN ('eBenefits', 'MHV', 'Mobl Hlth') 
  AND CREATED BETWEEN '10/1/2014' AND '09/30/2015' 
  and SR_AREA IS NOT NULL 
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

答案 2 :(得分:0)

由于巡回三选中的所有其他内容相同,您可以将它们组合成一个像

一样的选项
SELECT  INS_PRODUCT, SR_AREA, RESOLUTION_CD, COUNT(*) AS Vol        
FROM S_SRV_REQ          
WHERE  (INS_PRODUCT in ('Mobl Hlth','MHV','eBenefits')) AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') and (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

答案 3 :(得分:0)

SELECT 
  INS_PRODUCT, 
  SR_AREA, 
  RESOLUTION_CD, 
  SUM(IF(INS_PRODUCT = 'Mobl Hlth',1,0)) Mobl_Hlth_Cnt,
  SUM(IF(INS_PRODUCT = 'MHV',1,0)) MHV_Cnt,
  SUM(IF(INS_PRODUCT = 'eBenefits',1,0)) eBenefits_Cnt
FROM S_SRV_REQ          
WHERE INS_PRODUCT IN ('Mobl Hlth', 'MHV', 'eBenefits')
   AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') 
   AND (SR_AREA IS NOT NULL)    
GROUP BY INS_PRODUCT, SR_AREA, RESOLUTION_CD
ORDER BY INS_PRODUCT, SR_AREA, RESOLUTION_CD

或者如果我的猜测是正确的,您可以从INS_PRODUCT移除GROUP BY,以便获得:

SELECT 
  SR_AREA, 
  RESOLUTION_CD, 
  SUM(IF(INS_PRODUCT = 'Mobl Hlth',1,0)) Mobl_Hlth_Cnt,
  SUM(IF(INS_PRODUCT = 'MHV',1,0)) MHV_Cnt,
  SUM(IF(INS_PRODUCT = 'eBenefits',1,0)) eBenefits_Cnt
FROM S_SRV_REQ          
WHERE INS_PRODUCT IN ('Mobl Hlth', 'MHV', 'eBenefits')
   AND (CREATED BETWEEN '10/1/2014' AND '09/30/2015') 
   AND (SR_AREA IS NOT NULL)    
GROUP BY SR_AREA, RESOLUTION_CD
ORDER BY SR_AREA, RESOLUTION_CD