如何计算一列,具有元素为null的条件和另一个元素不为空的条件?一切按另一个元素分组

时间:2014-03-24 14:14:17

标签: sql oracle

我必须按两个条件计算1列,得到2列,按另一列分组。我会告诉你:

SELECT cy.Name, COUNT(noa.GZ_Aparat_ID), COUNT(noa.GZ_Aparat_ID)
  FROM GZ_Nominalizare_Aparat noa 
  INNER JOIN GZ_Nominalizare no ON (no.GZ_NOMINALIZARE_ID = noa.GZ_NOMINALIZARE_ID)
  INNER JOIN GZ_AcordAcces aa ON (aa.GZ_ACORDACCES_ID = no.GZ_ACORDACCES_ID)
  INNER JOIN GZ_Abonament ab ON (ab.GZ_ABONAMENT_ID = aa.GZ_ABONAMENT_ID)
  INNER JOIN GZ_PunctConsum pc ON (pc.GZ_ABONAMENT_ID = ab.GZ_ABONAMENT_ID)
  INNER JOIN GZ_IU iu ON (iu.GZ_PUNCTCONSUM_ID = pc.GZ_PUNCTCONSUM_ID)
  LEFT OUTER JOIN C_BPartner_Location bplo ON (ab.C_BPartner_Location_ID = bplo.C_BPARTNER_LOCATION_ID)
  LEFT OUTER JOIN C_Location lo ON (bplo.C_Location_ID = lo.C_Location_ID)
  LEFT OUTER JOIN C_City cy ON (lo.C_City_ID = cy.C_City_ID)
GROUP BY cy.Name
  

Firt COUNT必须包含noa.GZ_Aparat_ID的计数,其中iu.GZ_DataPIF为NULL   和第二个COUNT,其中iu.GZ_DataPIF不为空

1 个答案:

答案 0 :(得分:0)

不确定我的语法是否适合oracle,但它并不太相似。

SELECT cy.Name, 
       SUM(CASE WHEN noa.GZ_Aparat_ID is NULL THEN 0
                  WHEN  noa.GZ_Aparat_IDis NOT NULL THEN 1
                  END) as COUNTNONNULL, 
       SUM(CASE WHEN noa.GZ_Aparat_ID is NULL THEN 1
                  WHEN  noa.GZ_Aparat_ID is NOT NULL THEN 0
                  END) as COUNTNULL
  FROM GZ_Nominalizare_Aparat noa 
  INNER JOIN GZ_Nominalizare no ON (no.GZ_NOMINALIZARE_ID = noa.GZ_NOMINALIZARE_ID)
  INNER JOIN GZ_AcordAcces aa ON (aa.GZ_ACORDACCES_ID = no.GZ_ACORDACCES_ID)
  INNER JOIN GZ_Abonament ab ON (ab.GZ_ABONAMENT_ID = aa.GZ_ABONAMENT_ID)
  INNER JOIN GZ_PunctConsum pc ON (pc.GZ_ABONAMENT_ID = ab.GZ_ABONAMENT_ID)
  INNER JOIN GZ_IU iu ON (iu.GZ_PUNCTCONSUM_ID = pc.GZ_PUNCTCONSUM_ID)
  LEFT OUTER JOIN C_BPartner_Location bplo ON (ab.C_BPartner_Location_ID = bplo.C_BPARTNER_LOCATION_ID)
  LEFT OUTER JOIN C_Location lo ON (bplo.C_Location_ID = lo.C_Location_ID)
  LEFT OUTER JOIN C_City cy ON (lo.C_City_ID = cy.C_City_ID)
GROUP BY cy.Name