[英]Convert Concatenated String to Column Name in Oracle SQL
Problem Statement: 问题陈述:
I have a table named ACTIVITIES with all columns having a datatype of varchar(20) shown below: 我有一个名为ACTIVITIES的表,所有列的数据类型均为varchar(20),如下所示:
|ACTIVITY_NAME| Q1 | Q2 | Q3 | Q4 |
|ACT1 |02/05 |05/10 |08/21 |11/15 |
|ACT2 |01/20 |06/11 |08/06 |10/21 |
With this table, I want to get the current quarter based on the system date to select a particular row and convert the quarter data to date. 使用此表,我想基于系统日期获取当前季度,以选择特定的行并将季度数据转换为日期。 As of now, I have this query but an error is prompting saying that a non-numeric character was found where a numeric was expected which I'm actually familiar of.
到目前为止,我有此查询,但是出现错误提示,提示我在一个非数字字符中找到了我真正熟悉的数字。
select to_date(concat('Q', to_char(sysdate, 'Q')), 'MM/DD')
from activities
where activity_name = 'ACT2';
The result of the concat() function is a string which is shown below that's why there's an error prompt. concat()函数的结果是一个字符串,如下所示,这就是为什么出现错误提示的原因。
select to_date('Q4', 'MM/DD')
from activities
where activity_name = 'ACT2';
Now, I'm seeking for help on how to convert the concatenated string to a Column Name in order for me to extract that specific data on the current quarter and with the specific activity. 现在,我正在寻求有关如何将连接的字符串转换为列名的帮助,以便我可以在当前季度和特定活动中提取该特定数据。
Expected Result: 预期结果:
Query: 查询:
select to_date(Q4, 'MM/DD')
from activities
where activity_name = 'ACT2';
-- Q4 parameter inside to_date() function is now considered as Column name, not as String. -to_date()函数中的Q4参数现在被视为列名,而不是字符串。 As mentioned on the problem statement above, how can I convert that Q4 string paramater to a column name so I can get the output below.
如上面的问题声明所述,如何将Q4字符串参数转换为列名,以便获得下面的输出。
Output: 08/06/2016 输出:2016/08/06
I'm new to oracle sql and I would like to learn from you guys. 我是oracle sql的新手,我想向你们学习。 Really appreciate your help.
非常感谢您的帮助。 Thank you in-advance.
先感谢您。
The easiest way is to first unpivot your data: 最简单的方法是先取消数据透视:
SELECT *
FROM ACTIVITIES
UNPIVOT (
Quarter_date FOR qq IN (Q1, Q2, Q3, Q4 )
)
;
You will get a result like this: 您将得到如下结果:
ACTI QQ QUARTER_DA
---- -- ----------
ACT1 Q1 02/05
ACT1 Q2 05/10
ACT1 Q3 08/21
ACT1 Q4 11/15
ACT2 Q1 01/20
ACT2 Q2 06/11
ACT2 Q3 08/06
ACT2 Q4 10/21
And now picking a row for any quarter is very easy, for example if you need Q4
, just add WHERE QQ='Q4'
condition: 现在,为每个季度选择一行非常容易,例如,如果您需要
Q4
,只需添加WHERE QQ='Q4'
条件:
SELECT *
FROM (
-- the above query goes here ----
SELECT * FROM ACTIVITIES
UNPIVOT (
Quarter_date FOR qq IN (Q1, Q2, Q3, Q4 )
)
)
-- replace the below line with:
-- WHERE QQ = 'Q' || to_char(sysdate, 'Q')
-- if you want to get the current quarter based on sysdate
WHERE QQ = 'Q4'
;
ACTI QQ QUARTER_DA
---- -- ----------
ACT1 Q4 11/15
ACT2 Q4 10/21
And finaly, to convert a quarter to a date using current year, use: 最后,要将季度转换为使用当年的日期,请使用:
SELECT x.*
, to_date( to_char( sysdate, 'yyyy' ) || '/' || QUARTER_DATE, 'yyyy/mm/dd' ) as my_date
FROM (
SELECT * FROM ACTIVITIES
UNPIVOT (
Quarter_date FOR qq IN (Q1, Q2, Q3, Q4 )
)
) x
WHERE QQ = 'Q4'
;
ACTI QQ QUARTER_DA MY_DATE
---- -- ---------- ----------
ACT1 Q4 11/15 2016/11/15
ACT2 Q4 10/21 2016/10/21
---------- EDIT ---------- ----------编辑----------
Oracle 10 does not have UNPIVOT clause. Oracle 10没有UNPIVOT子句。
To unpivot data using regular SQL, UNION ALL
clause must be used, in this way: 要使用常规SQL取消数据透视,必须使用
UNION ALL
子句,这种方式:
SELECT ACTIVITY_NAME, 'Q1' As QQ, Q1 As QUARTER_DA FROM ACTIVITIES
UNION ALL
SELECT ACTIVITY_NAME, 'Q2' As QQ, Q2 As QUARTER_DA FROM ACTIVITIES
UNION ALL
SELECT ACTIVITY_NAME, 'Q3' As QQ, Q3 As QUARTER_DA FROM ACTIVITIES
UNION ALL
SELECT ACTIVITY_NAME, 'Q4' As QQ, Q4 As QUARTER_DA FROM ACTIVITIES
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.