Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何根据Teradata中的列值获取所有月份_Sql_Date_Teradata - Fatal编程技术网

Sql 如何根据Teradata中的列值获取所有月份

Sql 如何根据Teradata中的列值获取所有月份,sql,date,teradata,Sql,Date,Teradata,我从db输出中得到了这样的数据 yearmonth orig_region_name performance_percentage 2020-04 AMERICAS 95.45 2020-04 ASIA PACIFIC 100.00 2020-04 EUROPE 97.78 2020-04 GLOBAL

我从db输出中得到了这样的数据

yearmonth   orig_region_name            performance_percentage
2020-04     AMERICAS                    95.45
2020-04     ASIA PACIFIC                100.00
2020-04     EUROPE                      97.78

2020-04     GLOBAL                      97.76
2020-05     AMERICAS                    100.00
2020-05     EUROPE                      97.20
2020-05     GLOBAL                      97.21
但我需要补充的是亚太地区,这一地区在5月份就消失了。原点值是动态的,它将根据用户从UI中选择的参数进行更改

yearmonth   orig_region_name            performance_percentage
2020-04     AMERICAS                    95.45
2020-04     ASIA PACIFIC                100.00
2020-04     EUROPE                      97.78
2020-04     GLOBAL                      97.76

2020-05     AMERICAS                    100.00
2020-05     ASIA PACIFIC                0
2020-05     EUROPE                      97.20
2020-05     GLOBAL                      97.21
如何在不影响现有代码的情况下实施此逻辑?有人能帮我吗

质疑-

SELECT yearmonth, orig_region_name, 
 (Cast(sum_net_volume AS DECIMAL(15,4)) 
  / NullIf(sum_monitored_volume ,0))*100 AS performance_percentage
 FROM
 (
SELECT 
Trim(Year(start_date)) || '-' || To_Char(start_date, 'MM') AS yearmonth, 
  orig_region_name,
  Sum(net_volume) AS sum_net_volume,
  Sum(monitored_vol) AS sum_monitored_volume
 FROM TABLE
 WHERE start_date BETWEEN '2020-07-01' AND '2021-09-30' 
 AND group_code IN ('230') 
 AND (filter_BILL_RGN IN ('AM','EU') OR  (1=2)  )  
GROUP BY 1,2
) A 

您需要所有月份的所有区域。这是一个交叉连接开始

SELECT
  yearmonths.yearmonth,
  regions.orig_region_name, 
  CASE WHEN data.yearmonth IS NULL THEN 0 ELSE
    (Cast(data.sum_net_volume AS DECIMAL(15,4)) /
    NullIf(data.sum_monitored_volume, 0)) * 100
  END AS performance_percentage
FROM
(
  SELECT DISTINCT
    Trim(Year(start_date)) || '-' || To_Char(start_date, 'MM') AS yearmonth
  FROM TABLE
  WHERE start_date BETWEEN '2020-07-01' AND '2021-09-30' 
  AND group_code IN ('230') 
  AND (filter_BILL_RGN IN ('AM','EU') OR (1=2))
) yearmonths
CROSS JOIN
(
(
  SELECT DISTINCT
    orig_region_name
  FROM TABLE
  WHERE start_date BETWEEN '2020-07-01' AND '2021-09-30' 
  AND group_code IN ('230') 
  AND (filter_BILL_RGN IN ('AM','EU') OR (1=2))
) regions
LEFT JOIN
(
  SELECT 
    Trim(Year(start_date)) || '-' || To_Char(start_date, 'MM') AS yearmonth, 
    orig_region_name,
    Sum(net_volume) AS sum_net_volume,
    Sum(monitored_vol) AS sum_monitored_volume
  FROM TABLE
  WHERE start_date BETWEEN '2020-07-01' AND '2021-09-30' 
  AND group_code IN ('230') 
  AND (filter_BILL_RGN IN ('AM','EU') OR (1=2))  
  GROUP BY 1,2
) data ON data.yearmonth = yearmonths.yearmonth
       AND data.orig_region_name = regions.orig_region_name
ORDER BY yearmonths.yearmonth, regions.orig_region_name;

如果您不想将条件限制在那些有数据的情况下,请从
年月
地区
中删除条件。

您可以使用cte来实现此目的。我刚刚在cte中将您的查询用作内部查询

 with cte as
(
 SELECT yearmonth, orig_region_name, 
 (Cast(sum_net_volume AS DECIMAL(15,4)) 
  / NullIf(sum_monitored_volume ,0))*100 AS performance_percentage
 FROM
 (
SELECT 
Trim(Year(start_date)) || '-' || To_Char(start_date, 'MM') AS yearmonth, 
  orig_region_name,
  Sum(net_volume) AS sum_net_volume,
  Sum(monitored_vol) AS sum_monitored_volume
 FROM TABLE
 WHERE start_date BETWEEN '2020-07-01' AND '2021-09-30' 
 AND group_code IN ('230') 
 AND (filter_BILL_RGN IN ('AM','EU') OR  (1=2)  )  
GROUP BY 1,2
) A  
  )
  ,ym as (select distinct yearmonth from cte)
  ,regionname as (select distinct orig_region_name from cte)
  ,finalcte as (select * from ym cross join regionname)
  select f.yearmonth,f.orig_region_name,coalesce(cte.performance_percentage,0) from finalcte f left join cte
  on f.yearmonth=cte.yearmonth and f.orig_region_name=cte.orig_region_name
 
提供虚拟数据的示例:

 create table yourtable (yearmonth varchar(10),  orig_region_name varchar(50), performance_percentage float);
 insert into yourtable values('2020-04'     ,'AMERICAS',                    95.45);
 insert into yourtable values('2020-04'     ,'ASIA PACIFIC',                100.00);
 insert into yourtable values('2020-04'     ,'EUROPE',                      97.78);
 insert into yourtable values('2020-04'     ,'GLOBAL',                      97.76);
 insert into yourtable values('2020-05'     ,'AMERICAS',                    100.00);
 insert into yourtable values('2020-05'     ,'EUROPE',                      97.20);
 insert into yourtable values('2020-05'     ,'GLOBAL',                      97.21);
查询:

  with cte as(
  SELECT yearmonth, orig_region_name, 
   performance_percentage
   from  yourtable 
   )
   ,ym as (select distinct yearmonth from cte)
   ,regionname as (select distinct orig_region_name from cte)
   ,finalcte as (select * from ym cross join regionname)
   select f.yearmonth,f.orig_region_name,coalesce(cte.performance_percentage,0) from finalcte f left join cte
   on f.yearmonth=cte.yearmonth and f.orig_region_name=cte.orig_region_name
  
输出:

年月 原始区域名称 (没有列名称) 2020-04 美洲 95.45 2020-04 亚太地区 100 2020-04 欧洲 97.78 2020-04 全球的 97.76 2020-05 美洲 100 2020-05 亚太地区 0 2020-05 欧洲 97.2 2020-05 全球的 97.21
Teradata 16.20支持时间序列表和聚合:

SELECT TO_CHAR(BEGIN(pd), 'YYYY-MM') AS YEARMONTH, orig_region_name, 
 (Cast(sum_net_volume AS DECIMAL(15,4)) 
  / NullIf(sum_monitored_volume ,0))*100 AS performance_percentage
 FROM
 (
SELECT 
  -- returns a date period
  CAST($TD_TIMECODE_RANGE AS PERIOD(DATE)) AS pd,
  orig_region_name,
  Sum(net_volume) AS sum_net_volume,
  Sum(monitored_vol) AS sum_monitored_volume
 FROM TABLE
 WHERE start_date BETWEEN DATE '2020-07-01' AND DATE '2021-09-30' 
 AND group_code IN ('230') 
 AND (filter_BILL_RGN IN ('AM','EU') OR  (1=2)  )  
         -- times series aggregation, one row per month/region
GROUP BY TIME (CAL_MONTHS(1) AND orig_region_name)
         -- if the base table has no Primary Time Index TIMECODE must be specified
         USING TIMECODE (start_date)
         -- this creates the missing rows based on the date range in WHERE
         FILL (0)
) A 
您的百分比计算可能也可以简化。假设卷是十进制/整数列:

100.00 * sum_net_volume
  / NullIf(sum_monitored_volume ,0) AS performance_percentage

经验法则:先乘后除。

我知道你是一名新的投稿人,如果你能在markdown中使用
table
更好地格式化表格,那将非常有帮助。而且,直接显示结果也没有帮助。如果您可以显示您的表-它们的列是什么,然后使用什么查询。谢谢您的建议。是的,我不熟悉堆栈溢出。将为我即将进行的查询格式化数据。我已经更新了我的查询。谢谢卡齐,你的解决方案非常有效:)太好了。不客气。由于它有效,请单击绿色复选框接受此答案。祝你一切顺利,谢谢托尔斯滕。我将检查这个方法。