简体   繁体   English

sql UNPIVOT查询日期格式

[英]sql UNPIVOT query date format

I have a query with UnPivot as follows.我对 UnPivot 进行了如下查询。 This will give me code, value1, value2.这会给我代码,value1,value2。 Now i want to change the format of the date fields as 'MM/DD/YYYY'.现在我想将日期字段的格式更改为“MM/DD/YYYY”。 Tried to_char(MATH_DATE, 'MM/DD/YYYY') but getting尝试 to_char(MATH_DATE, 'MM/DD/YYYY') 但得到

ORA-00904: "from$_subquery$_001"."MATH_DATE": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:

select
            * 
        from
            ( select
                MATH,
                to_char(MATH_DATE,'MM/DD/YYYY'),
                SCI,
                SCI_DATE,
                HIST,
                HIST_DATE,
                GEO,
                GEO_DATE,
                PE,
                PE_DATE 
            from
                subj_view vw,
                inventory bi  
            where
                bi.id = vw.id  
                and id = 161) UNPIVOT INCLUDE NULLS((value1,
            value2)  FOR code in( (MATH,
            math_date) as 'MATH',
            (SCI,
            SCI_DATE) as 'SCI',
            (HIST,
            HIST_DATE) as 'HIST',
            (GEO,
            GEO_DATE) as 'GEO',
            (PE,
            PE_DATE) as 'PE' ) );

Output looks like Output 看起来像

code    value1  value2
MATH    100     20070401
SCI     86  
HIST    89      201904
GEO     89      20191206
PE      90      20070118

How can I change the formatof valaue2 with the same query.如何使用相同的查询更改 valaue2 的格式。 Please help.请帮忙。

Any suggestions highly appreciated.任何建议高度赞赏。

The names of your columns must match with the names used in the for... in clause.列的名称必须与for... in子句中使用的名称匹配。 As you have not given an alias to the to_char() , there is no longer a match_date column.由于您没有为to_char()提供别名,因此不再有match_date列。

So use an alias;所以使用别名; it can be the same as the original column name, so that you don't need to change the for... in clause:它可以与原始列名相同,因此您无需更改for... in子句:

 to_char(MATH_DATE,'MM/DD/YYYY') math_date,

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

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