简体   繁体   English

Oracle查询按月和年显示数据

[英]Oracle query displays data by month and year

I want to display the amount of data by month and year. 我想按月份和年份显示数据量。 This is an example of displaying data by date: 这是一个按日期显示数据的示例:

select count(*) from db.trx where trxdate = to_date('2018-04-23','yyyy-mm-dd')

When I try to display the amount of data by month and year, no query results appear. 当我尝试按月份和年份显示数据量时,没有查询结果出现。 Is there something wrong with the query? 查询有问题吗?

The query: 查询:

select count(*) from db.trx where trxdate = to_date('2018-04','yyyy-mm')

You need to apply the function to trxdate . 您需要将该函数应用于trxdate Using your logic: 使用您的逻辑:

SELECT Count(*) 
FROM   olap.trxh2hpdam 
WHERE  To_char(trxdate, 'YYYY-MM') = '2018-04'; 

However, I strongly recommend that you use direct date comparisons: 但是,我强烈建议您使用直接日期比较:

WHERE trxdate >= date '2018-04-01' 
AND 
trxdate < date '2018-05-01'

This will allow the database to use an index on trxdate . 这将允许数据库在trxdate上使用索引。

There are a couple of ways of accomplishing what you're trying to do. 有两种方法可以完成您想做的事情。 Which one works for you will depend on your database design (for example, the indexes you've created). 哪种方法适合您取决于您​​的数据库设计(例如,您创建的索引)。 One way might be this: 一种方法可能是这样的:

SELECT COUNT(*) FROM olap.trxh2hpdam
 WHERE TRUNC(trxdate, 'MONTH') = DATE'2018-04-01';

This will round the date down to the first of the month (and, of course, remove any time portion). 这会将日期向下舍入到每月的第一天(当然,删除任何时间部分)。 Then you simply compare it to the first of the month for which you want the data. 然后,您只需将其与想要数据的月份的第一天进行比较。 However, unless you have an index on TRUNC(trxdate, 'MONTH') , this may not be the best course of action; 但是,除非您在TRUNC(trxdate, 'MONTH')上有索引,否则这可能不是最好的选择; if trxdate is indexed, you'll want to use: 如果将trxdate编入索引,则需要使用:

SELECT COUNT(*) FROM olap.trxh2hpdam
 WHERE trxdate >= DATE'2018-04-01'
   AND trxdate < DATE'2018-05-01';

There are a number of functions at your disposal in Oracle (eg ADD_MONTHS() ) in the event that the date you use in your query is supposed to be dynamic rather than static. 如果您在查询中使用的日期应该是动态的而不是静态的,则Oracle中有许多功能可供您使用(例如ADD_MONTHS() )。

Just FYI, there is no reason not to use ANSI date literals when trying to retrieve data by day as well. 仅供参考,在尝试按天检索数据时也没有理由不使用ANSI日期文字。 I'm not sure your original query is a good example of getting data for a particular day, since the Oracle DATE datatype does at least potentially include a time: 我不确定您的原始查询是获取特定DATE数据的一个很好的例子,因为Oracle DATE数据类型确实至少包含一个时间:

SELECT COUNT(*) FROM olap.trxh2hpdam
 WHERE trxdate >= DATE'2018-04-23'
   AND trxdate < DATE'2018-04-24';

or: 要么:

SELECT COUNT(*) FROM olap.trxh2hpdam
 WHERE TRUNC(trxdate) = DATE'2018-04-23';

EDIT 编辑

In case the month and year are dynamic, I would build a date from them (eg, TO_DATE('<year>-<month>-01', 'YYYY-MM-DD') ) and then use the following query: 如果月份和年份是动态的,则可以根据它们建立一个日期(例如TO_DATE('<year>-<month>-01', 'YYYY-MM-DD') ),然后使用以下查询:

SELECT COUNT(*) FROM olap.trxh2hpdam
 WHERE trxdate >= TO_DATE('<year>-<month>-01', 'YYYY-MM-DD')
   AND trxdate < ADD_MONTHS( TO_DATE('<year>-<month>-01', 'YYYY-MM-DD'), 1 );

Hope this helps. 希望这可以帮助。

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

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