有没有办法将“%”符号添加到我在 Oracle SQL 中的透视结果中并将它们转换为 to_char

Posted

技术标签:

【中文标题】有没有办法将“%”符号添加到我在 Oracle SQL 中的透视结果中并将它们转换为 to_char【英文标题】:Is there a way to add the '%' symbol to my pivoted result in Oracle SQL and convert them to to_char 【发布时间】:2020-10-21 04:39:38 【问题描述】:

我正在尝试旋转具有多列的表,但我还想为 1 行附加一个“%”符号。我还需要该行显示 1 个小数点。

原始查询:

select distinct a.TIME_DESC,
round(sum(a.TRX_VOLUMES)) TRx_volume,
round(sum(TARGET_TRX_VOLUMES)) Goal,
round(sum(a.TRX_VOLUMES_LY)) TRx_volume_LY,
round((sum(a.TRX_VOLUMES)/sum(c.TRX_VOLUMES_MKT))*100,1) Mkt_share
from a
join b on a.BRAND_ID=b.BRAND_ID and a.GEO_ID=b.GEO_ID
and a.TIMEPERIODTYPE=b.TIMEPERIODTYPE and a.TIME_DESC=b.TIME_DESC
join c on a.BRAND_ID=c.BRAND_ID and a.GEO_ID=c.GEO_ID
and a.TIMEPERIODTYPE=c.TIMEPERIODTYPE and a.TIME_DESC=c.TIME_DESC
where a.GEO_DESC='BC/YK' and a.type_code='B' and a.dash_brand='Copaxone'
and a.TIMEPERIODTYPE='YEAR'
group by a.TIME_DESC

对于我使用的旋转行

with stg1 as (select distinct a.TIME_DESC,
round(sum(a.TRX_VOLUMES)) TRx_volume,
round(sum(TARGET_TRX_VOLUMES)) Goal,
round(sum(a.TRX_VOLUMES_LY)) TRx_volume_LY,
round((sum(a.TRX_VOLUMES)/sum(c.TRX_VOLUMES_MKT))*100,1) Mkt_share
from a
join b on a.BRAND_ID=b.BRAND_ID and a.GEO_ID=b.GEO_ID
and a.TIMEPERIODTYPE=b.TIMEPERIODTYPE and a.TIME_DESC=b.TIME_DESC
join c on a.BRAND_ID=c.BRAND_ID and a.GEO_ID=c.GEO_ID
and a.TIMEPERIODTYPE=c.TIMEPERIODTYPE and a.TIME_DESC=c.TIME_DESC
where a.GEO_DESC='BC/YK' and a.type_code='B' and a.dash_brand='Copaxone'
and a.TIMEPERIODTYPE='YEAR'
group by a.TIME_DESC),
stg5 AS (SELECT * FROM (select time_desc, Mkt_share from stg1)
pivot
(SUM(Mkt_share) FOR time_desc IN ('Jan 2020', 'Feb 2020', 'Mar 2020', 'Apr 2020', 'May 2020', 'Jun 2020', 'Jul 2020', 'Aug 2020', 'Sep 2020', 'Oct 2020', 'Nov 2020', 'Dec 2020')))
select 'MS %', d.* from stg5 d


'MS%'   'Jan 2020'  'Feb 2020'  'Mar 2020'  
MS %    7           6.5           7.1   

我想为 7 添加一个小数,使其成为 7.0 和一个 % 符号。这可能吗?

【问题讨论】:

【参考方案1】:

一种方法,但看起来并不漂亮,而且与任何“动态”类型的东西相去甚远。

这是一个普通的透视查询:

SQL> select *
  2  from
  3    (select deptno, sal
  4     from emp
  5    )
  6  pivot
  7  (sum(sal) sumsal
  8   for deptno in (10, 20, 30)
  9  )
 10  order by 1;

 10_SUMSAL  20_SUMSAL  30_SUMSAL
---------- ---------- ----------
     13750      10995       9400

SQL>

假设你想格式化这些薪水;那么你会做这样的事情:

SQL> select to_char("10_SUMSAL", '99990D0') || '%' as "10",
  2         to_char("20_SUMSAL", '99990D0') || '%' as "20",
  3         to_char("30_SUMSAL", '99990D0') || '%' as "30"
  4  from
  5    (select deptno, sal
  6     from emp
  7    )
  8  pivot
  9  (sum(sal) sumsal
 10   for deptno in (10, 20, 30, 40)
 11  )
 12  order by 1;

10        20        30
--------- --------- ---------
 13750,0%  10995,0%   9400,0%

SQL>

【讨论】:

【参考方案2】:

答案是重新格式化像这样的透视列

TO_CHAR( "Jan 2020",'fm999G990D0','NLS_NUMERIC_CHARACTERS = ''.,''')||'%'

但是做更多,通过创建这样一个函数将您的查询转换为动态查询:

CREATE OR REPLACE FUNCTION get_brands_volume RETURN SYS_REFCURSOR IS
  recordset SYS_REFCURSOR;
  sqlqry    VARCHAR2(32767);
  cols1     VARCHAR2(32767);
  cols2     VARCHAR2(32767);  
BEGIN
  SELECT LISTAGG(''''||TO_CHAR(TO_DATE(level,'mm'),'Mon yyyy')||''' AS "'||
         TO_CHAR(TO_DATE(level,'mm'),'Mon yyyy')||'"' ,',') 
         WITHIN GROUP (ORDER BY level)
    INTO cols1
    FROM dual
 CONNECT BY level <= 12; 

  SELECT LISTAGG('TO_CHAR( "'||TO_CHAR(TO_DATE(level,'mm'),'Mon yyyy')||'",''fm999G990D0'',
                             ''NLS_NUMERIC_CHARACTERS = ''''.,'''''') AS "'||
                               TO_CHAR(TO_DATE(level,'mm'),'Mon yyyy')||'"',',') 
         WITHIN GROUP (ORDER BY level)
    INTO cols2   
    FROM dual
 CONNECT BY level <= 12;
 
  sqlqry :=
  'WITH stg1  AS
   (SELECT DISTINCT a.TIME_DESC,
                    ROUND(SUM(a.TRX_VOLUMES)) TRx_volume,
                    ROUND(SUM(TARGET_TRX_VOLUMES)) Goal,
                    ROUND(SUM(a.TRX_VOLUMES_LY)) TRx_volume_LY,
                    ROUND((SUM(a.TRX_VOLUMES) / SUM(c.TRX_VOLUMES_MKT)) * 100,1) Mkt_share
      FROM a
      JOIN b
        ON a.BRAND_ID = b.BRAND_ID
       AND a.GEO_ID = b.GEO_ID
       AND a.TIMEPERIODTYPE = b.TIMEPERIODTYPE
       AND a.TIME_DESC = b.TIME_DESC
      JOIN c
        ON a.BRAND_ID = c.BRAND_ID
       AND a.GEO_ID = c.GEO_ID
       AND a.TIMEPERIODTYPE = c.TIMEPERIODTYPE
       AND a.TIME_DESC = c.TIME_DESC
     where a.GEO_DESC = ''BC/YK''
       AND a.type_code = ''B''
       AND a.d ASh_brAND = ''Copaxone''
       AND a.TIMEPERIODTYPE = ''YEAR''
     GROUP BY a.TIME_DESC),
  stg5  AS
   (SELECT '||cols2||'
      FROM stg1
      PIVOT(
             MAX(Mkt_share) FOR time_desc IN('||cols1||')
            )
   )';    

  OPEN recordset FOR sqlqry;
  RETURN recordset;
END;

对于当年的月份,然后从 SQL Developer 的命令行调用

VAR rc REFCURSOR
EXEC :rc := get_brands_volume;
PRINT rc

【讨论】:

以上是关于有没有办法将“%”符号添加到我在 Oracle SQL 中的透视结果中并将它们转换为 to_char的主要内容,如果未能解决你的问题,请参考以下文章

将设备添加到团队配置文件

我的 Joomla 模板正在使用 JS 加载我的自定义 CSS。有没有办法将唯一版本标签添加到我的自定义 CSS 中?即:custom.css?20180101

将元素数组添加到dom

有没有什么办法可以将我的存储库下载为zip并添加到我的本地存储库而不是使用git(repo snc命令)?

有没有办法抑制 Xcode 中的警告?

有没有办法使用android studio在我的flutter项目中创建一个Podfile?