[英]how to select value from month to month name in oracle database table in sql
I want to my database table date column to month, and wanted to calculate the data from month to month But, i getting this error again and again我想将我的数据库表日期列改为月份,并想逐月计算数据但是,我一次又一次地收到此错误
SQL> desc fac_cus_chd
Name Null? Type
----------------------------------------- -------- ----------------------------
FDC NOT NULL VARCHAR2(9)
WHDATE NOT NULL DATE
SL_NO NOT NULL NUMBER(3)
ITEMCODE NOT NULL VARCHAR2(11)
DECCODE NOT NULL VARCHAR2(15)
GRADE NOT NULL VARCHAR2(1)
QTY NOT NULL NUMBER(9,2)
UNIT NOT NULL VARCHAR2(6)
CARTON VARCHAR2(5)
UPRICE NUMBER(9,2)
UPT VARCHAR2(1)
CARTON_DET VARCHAR2(5)
SQL> select SUM(QTY)
from fac_cus_chd
where whdate between to_date(to_char('01-JAN-2019', 'DD-MM-YYYY'), 'Month') AND to_date(to_char('31-DEC-2019', 'DD-MM-YYYY') ;
2 /
select to_date(to_char('WHDATE', 'DD-MM-YYYY'), 'Month') from fac_cus_chd
*
ERROR at line 1:
ORA-01722: invalid number
SQL>
I want o have value like我想要 o 有这样的价值
Month Qty
Jan 200000
Feb 33,0000
Mar 34,0000
APR 32293
May 242434
JUN 24242
JUL 24234
AUG 232423
SEP 242432
OCT 232342
NOV 33423
DEC 3233333
Remove the quotes enclosing WHDATE
.删除包含
WHDATE
的引号。 Also TO_DATE() doesnt convert the column directly but reads the data in the format given like you have DD-MM-YYYY
below this ensures the data is read like this and converted to default date format of oracle TO_DATE() 也不会直接转换列,而是以给定的格式读取数据,就像下面有
DD-MM-YYYY
,这确保数据像这样读取并转换为 oracle 的默认日期格式
TO_DATE(WHDATE, 'DD-MM-YYYY')
TO_CHAR(WHDATE, 'MON')
//this will give the desired
I think the solution would be something like that我认为解决方案是这样的
with fac_cus_chd as(
select to_date('01-01-2019', 'DD-MM-YYYY') as whdate, 10 as qty from dual union all
select to_date('01-02-2019', 'DD-MM-YYYY') as whdate, 30 as qty from dual union all
select to_date('01-03-2019', 'DD-MM-YYYY') as whdate, 5 as qty from dual union all
select to_date('01-04-2019', 'DD-MM-YYYY') as whdate, 10 as qty from dual union all
select to_date('01-07-2019', 'DD-MM-YYYY') as whdate, 2 as qty from dual union all
select to_date('01-09-2019', 'DD-MM-YYYY') as whdate, 4 as qty from dual union all
select to_date('01-10-2019', 'DD-MM-YYYY') as whdate, 5 as qty from dual union all
select to_date('25-10-2019', 'DD-MM-YYYY') as whdate, 8 as qty from dual union all
select to_date('01-11-2019', 'DD-MM-YYYY') as whdate, 2 as qty from dual union all
select to_date('01-12-2019', 'DD-MM-YYYY') as whdate, 3 as qty from dual union all
select to_date('20-12-2019', 'DD-MM-YYYY') as whdate, 7 as qty from dual
)
select to_char(trunc(whdate, 'MON'), 'Mon') as month_name
, sum(qty) as quantity
from fac_cus_chd
where trunc(WHDATE, 'MON') between to_date('01-01-2019', 'DD-MM-YYYY')
and to_date('31-12-2019', 'DD-MM-YYYY')
group by trunc(whdate, 'MON')
order by trunc(whdate, 'MON')
you could then apply ordering etc然后你可以申请订购等
MONTH_NAME QUANTITY
Jan 10
Feb 30
Mar 5
Apr 10
Jul 2
Sep 4
Oct 13
Nov 2
Dec 10
9 rows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.