简体   繁体   English

SQL:SQL查询中的Union All时出错

[英]SQL : Error at Union All in sql query

Below is my query. 以下是我的查询。 I am using UNION ALL because i need to bring data when MONTH column is both NOT NULL and NULL. 我正在使用UNION ALL,因为当MONTH列既不是NULL又是NULL时,我需要带数据。 But this is not working, i am wondering why. 但这不起作用,我想知道为什么。

    SELECT metricname,
         careertrackid,
         monthyrname,
         SUM(metricvalue) AS metricval,
         CASE
           WHEN MONTH IS NOT NULL THEN CONVERT(VARCHAR,YEAR) + LPAD(CONVERT(VARCHAR,MONTH),2,'0')
           ELSE CONVERT(VARCHAR,acnfiscalyear) + CONVERT(VARCHAR,acnfiscalquarter)
         END AS dispmonth
  FROM atp_9375_forecast,
       vspforecasts
  WHERE ibfcollectionid = vspforecasts.ibfid
  AND   (YEAR>= 2018 AND MONTH>= 7)
  GROUP BY careertrackid,
           metricname,
           dispmonth,
           monthyrname
  ORDER BY dispmonth

UNION ALL

SELECT metricname,
         careertrackid,
         monthyrname,
         SUM(metricvalue) AS metricval,
         CASE
           WHEN MONTH IS NOT NULL THEN CONVERT(VARCHAR,YEAR) + LPAD(CONVERT(VARCHAR,MONTH),2,'0')
           ELSE CONVERT(VARCHAR,acnfiscalyear) + CONVERT(VARCHAR,acnfiscalquarter)
         END AS dispmonth
  FROM atp_9375_forecast,
       vspforecasts
  WHERE ibfcollectionid = vspforecasts.ibfid
  AND   (YEAR>= 2018 AND MONTH IS NULL)
  GROUP BY careertrackid,
           metricname,
           dispmonth,
           monthyrname
  ORDER BY dispmonth

Is there any other way in which i can still apply MONTH filter but that should include both NULL and NOT NULL as MONTH column is having both these values in table. 还有什么其他方法仍然可以应用MONTH过滤器,但是应该同时包含NULL和NOT NULL,因为MONTH列在表中同时具有这两个值。

Sample Result This is the desired result but this only comes when MONTH column includes NULL and NOT NULL 示例结果这是期望的结果,但这仅在MONTH列包含NULL和NOT NULL时出现

{"201806":"0.00","201807":"0.00","201808":"0.00","201809":"0.00","201810":"0.00","201811":"0.00","FY19Q2":"0.00","FY19Q3":"0.00","FY19Q4":"0.00","FY20Q1":"0.00"}

From your question your union all two queries are almost the same, only in the month to judge, you don't need to use UNION ALL just use add a condition (MONTH>= 7 OR MONTH IS NULL) in WHERE 从您的问题中,您的union all两个查询几乎相同,仅需在month进行判断,就无需使用UNION ALL只需在WHERE使用添加条件(MONTH>= 7 OR MONTH IS NULL)

SELECT metricname,
     careertrackid,
     monthyrname,
     SUM(metricvalue) AS metricval,
     CASE
       WHEN MONTH IS NOT NULL THEN CONVERT(VARCHAR,YEAR) + LPAD(CONVERT(VARCHAR,MONTH),2,'0')
       ELSE CONVERT(VARCHAR,acnfiscalyear) + CONVERT(VARCHAR,acnfiscalquarter)
     END AS dispmonth
FROM atp_9375_forecast INNER JOIN 
   vspforecasts ON ibfcollectionid = vspforecasts.ibfid
WHERE (YEAR>= 2018 AND (MONTH>= 7 OR MONTH IS NULL))
GROUP BY careertrackid,
       metricname,
       dispmonth,
       monthyrname
ORDER BY dispmonth

Note 注意

I suggest avoid to use , comma to connect tables, because it's an old style, you can use join instead of it. 我建议避免使用,逗号连接表,因为它是一种旧样式,可以使用join代替它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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