Hello i need to get transaction count and total amount for transactions in last quarter.
i use following to get data for a quarter (last 90 days)
WITH date_range as
(SELECT TRUNC(sysdate) - 90 + level AS week_day
FROM dual
CONNECT BY ROWNUM <= 90),
the_data
AS (SELECT TRUNC(systemdate) AS log_date, count(*) AS num_obj,status AS log_status, nvl(sum(
CASE
WHEN VERSION = '1.1'
THEN nvl(amount/100,'0.0')
ELSE nvl(amount,'0.0')
END), 0) AS totalamount
from transactionlog where ((merchantcode in (
SELECT regexp_substr('MERC0003','[^,]+', 1, LEVEL) FROM dual
connect by regexp_substr('MERC0003', '[^,]+', 1, level) is not null ) OR 'MERC0003' IS NULL) AND status = 'xxxx')
GROUP BY TRUNC(systemdate),status)
SELECT TO_CHAR(dr.week_day,'DD/MM/YYYY HH:MI AM') AS TXNDATE, NVL(trans_log.num_obj,0) as TXNCOUNT,trans_log.log_status,trans_log.totalamount
FROM date_range dr LEFT OUTER JOIN the_data trans_log
on trans_log.log_date = dr.week_day
ORDER BY dr.week_day DESC ;
From above, I get 90 days record , as in 90 rows containing transaction count and amount for 90 days,
i need to get data in terms of weeks in quarter. That is, 12 rows containing data for each week which has transaction count and amount in last 12 weeks.
You can use TRUNC( date_value ,'IW' ) to truncate a date to the start of the ISO week (Monday 00:00) and add multiples of a week in your date range and perform the same truncation in your query:
WITH date_range( week_day ) as (
SELECT TRUNC( sysdate - 90, 'IW' ) + ( level - 1 ) * INTERVAL '7' DAY
FROM dual
CONNECT BY TRUNC( sysdate - 90, 'IW' ) + ( level - 1 ) * INTERVAL '7' DAY <= SYSDATE
),
the_data ( log_date, num_obj, log_status, totalamount ) AS (
SELECT TRUNC(systemdate, 'IW'),
-- rest of your query
GROUP BY TRUNC(systemdate, 'IW'),
status
)
SELECT *
FROM date_range dr
LEFT OUTER JOIN the_data trans_log
on trans_log.log_date = dr.week_day
ORDER BY dr.week_day DESC;
Related
hi dear i have a query which create dates for specic lenght which is mentioned and a query which is create random time want to join both for date generating is this
It create dates for specific lenght and insert into table query is
insert into calendar_date2
with dates as (
select date'2019-12-31'+level dt
from dual
connect by level <= 365
)
select dt,
case
when to_char(dt, 'fmday') in ('sunday')
then 'N' else 'Y'
end
from dates
;
what it will do is enter CALENDER_DATE2 all dates mentioned on connect by level
2nd query is to genrate random time
with test (start_date) as
(select to_date('01.01.2021 07:45', 'dd.mm.yyyy hh24:mi') from dual
)
select start_date + round(dbms_random.value(1, 15)) / (24 * 60) result
from test
connect by level <= 15
I want to write both query work together what i wrote which is not working for me that is
insert into calendar_date2
with dates as (
select dates(to_date('01.01.2020 07:45', 'dd.mm.yyyy hh24:mi'))+level dt
from dual
select start_date + round(dbms_random.value(1, 15)) / (24 * 60) result
from test
connect by level <= 365
)
select dt,
case
when to_char(dt, 'fmday') in ('sunday')
then 'N' else 'Y'
end
from dates
;
i write this but it not worked for me please help struck here from past many hours
You can use:
insert into calendar_date2 (dt, is_workday )
SELECT dt,
DECODE( TRUNC( dt ) - TRUNC( dt, 'IW' ), 6, 'N', 'Y' )
FROM (
SELECT DATE '2020-01-01' + FLOOR( (LEVEL - 1) / 15 )
+ FLOOR(DBMS_RANDOM.VALUE(1, 16)) * INTERVAL '1' MINUTE
AS dt
FROM DUAL
CONNECT BY
DATE '2020-01-01' + FLOOR( (LEVEL - 1) / 15 )
< ADD_MONTHS( DATE '2020-01-01', 12 )
)
Which for the table:
dt DATE
NOT NULL,
is_workday CHAR(1)
NOT NULL
CONSTRAINT calendar_date2__iwd__chk CHECK ( is_workday IN ( 'Y', 'N' ) )
);
Gives you 15 rows for each day of 2020 each with random offset between 1 and 15 minutes from midnight.
Note:
The number of days in the year is calculated so you do not need to account for leap years.
Use FLOOR( DBMS_RANDOM.VALUE( 1, 16 ) ) rather than ROUND( DBMS_RANDOM.VALUE( 1, 15 ) ) as the first will give you an even spread of values whereas the second will only give a value 1 when the random value is between 1 and 1.5 and a value of 15 between 14.5 and 15 which will both occur with only half the frequency as the rest of the numbers and would give you an unbalanced distribution.
You can calculate the day of the week by comparing the start of the day TRUNC( dt ) to the start of the ISO week TRUNC( dt, 'IW' ). This has the benefit that it is independent of the NLS settings and will work in all languages rather than just being limited to English speaking territories.
db<>fiddle here
I have two date columns and trying to measure days between the two dates excluding weekends. I'm getting a negative number and need help solving.
Table
CalendarDate DayNumber FirstAssgn FirstCnt DayNumber2 Id BusinessDays
5/21/2017 Sunday 5/21/17 5/21/17 Sunday 1 -1
Query:
TRUNC(TO_DATE(A.FIRST_CONTACT_DT, 'DD/MM/YYYY')) - TRUNC(TO_DATE(A.FIRST_ASSGN_DT, 'DD/MM/YYYY'))
- ((((TRUNC(A.FIRST_CONTACT_DT,'D'))-(TRUNC(A.FIRST_ASSGN_DT,'D')))/7)*2)
- (CASE WHEN TO_CHAR(A.FIRST_ASSGN_DT,'DY','nls_date_language=english') ='SUN' THEN 1 ELSE 0 END)
- (CASE WHEN TO_CHAR(A.FIRST_CONTACT_DT,'DY','nls_date_language=english')='SAT' THEN 1 ELSE 0 END)
- (SELECT COUNT(1) FROM HUM.CALENDAR CAL
WHERE 1=1
AND CAL.CALENDAR_DATE >= A.FIRST_ASSGN_DT
AND CAL.CALENDAR_DATE < A.FIRST_CONTACT_DT
--BETWEEN A.FIRST_ASSGN_DT AND A.FIRST_CONTACT_DT
AND CAL.GRH_HOLIDAY_IND = 'Y'
) AS Business_Days
Looks like below piece needs editing...
- (CASE WHEN TO_CHAR(A.FIRST_ASSGN_DT,'DY','nls_date_language=english')='SUN' THEN 1 ELSE 0 END)
Adapted from my answer here:
Get the number of days between the Mondays of both weeks (using TRUNC( datevalue, 'IW' ) as an NLS_LANGUAGE independent method of finding the Monday of the week) then add the day of the week (Monday = 1, Tuesday = 2, etc., to a maximum of 5 to ignore weekends) for the end date and subtract the day of the week for the start date. Like this:
SELECT ( TRUNC( end_date, 'IW' ) - TRUNC( start_date, 'IW' ) ) * 5 / 7
+ LEAST( end_date - TRUNC( end_date, 'IW' ) + 1, 5 )
- LEAST( start_date - TRUNC( start_date, 'IW' ) + 1, 5 )
AS WeekDaysDifference
FROM your_table
With RANGE_TEMP as (
SELECT
STARTPERIOD start_date,
ENDPERIOD end_date
FROM
TABLE_DATA -- YOUR TABLE WITH ALL DATA DATE
), DATE_TEMP AS (
SELECT
(start_date + LEVEL) DATE_ALL
FROM
RANGE_TEMP
CONNECT BY LEVEL <= (end_date - start_date)
), WORK_TMP as (
SELECT
COUNT(DATE_ALL) WORK_DATE
FROM
DATE_TEMP
WHERE
TO_CHAR(DATE_ALL,'D', 'NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('1','7')
), BUSINESS_TMP as (
SELECT
COUNT(DATE_ALL) BUSINESS_DATE
FROM
DATE_TEMP
WHERE
TO_CHAR(DATE_ALL,'D', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('1','7')
)
SELECT
L.WORK_DATE,
H.BUSINESS_DATE
FROM
BUSINESS_TMP H,
WORK_TMP L
;
hello i am using the following sql query to get records count per hour in last 24 hours:
WITH date_range
AS (SELECT TRUNC(sysdate - (rownum/24),'HH24') as the_hour
FROM dual
CONNECT BY ROWNUM <= 24),
the_data
AS (SELECT TRUNC(systemdate, 'HH24') AS log_date, count(*) AS num_obj
FROM transactionlog where merchantcode='merc0003'
GROUP BY TRUNC(systemdate, 'HH24'))
SELECT TO_CHAR(dr.the_hour,'DD/MM/YYYY HH:MI AM'), NVL(trans_log.num_obj,0)
FROM date_range dr LEFT OUTER JOIN the_data trans_log
ON trans_log.log_date = dr.the_hour
ORDER BY dr.the_hour DESC ;
I am trying to get record count per day in oracle for last 7 days .. can some one guide what i may do to get last 7 days by changing above query?
i tried following to get last 7 days, but no gain as yet:
WITH date_range
AS
(SELECT TRUNC(sysdate - (7)) as the_hour
FROM dual
CONNECT BY ROWNUM <= 7),
the_data
AS (SELECT TRUNC(systemdate, 'HH24') AS log_date, count(*) AS num_obj
FROM transactionlog where merchantcode='merc0003'
GROUP BY TRUNC(systemdate, 'HH24'))
SELECT TO_CHAR(dr.the_hour,'DD/MM/YYYY HH:MI AM'), NVL(trans_log.num_obj,0)
FROM date_range dr LEFT OUTER JOIN the_data trans_log
ON trans_log.log_date = dr.the_hour
ORDER BY dr.the_hour DESC ;
Your mistake is you are not adding days; you are retrieving the same day seven times. Add level (or level - 1, whichever you want) to the day:
SELECT TRUNC(sysdate) - 7 + level AS the_hour
FROM dual
CONNECT BY ROWNUM <= 7
I'm surprised your initial CTE even worked. I had not heard of using CONNECT BY together with ROWNUM (although, having done some Googling, I understand that it works under some conditions. Still, I'm convinced that it's a misuse). To get a date range of the last seven days you'll want something like the following:
WITH date_range AS (
-- If you want to include today's date, add +1 to the date below
SELECT TRUNC(SYSDATE - LEVEL) AS the_date
FROM dual
CONNECT BY LEVEL <= 7
)
Putting this together with your existing query:
WITH date_range AS (
SELECT TRUNC(SYSDATE - LEVEL) AS the_date
FROM dual
CONNECT BY LEVEL <= 7
), the_data AS (
-- You still had 'HH24' here in the call to TRUNC()!
SELECT TRUNC(systemdate) AS log_date, count(*) AS num_obj
FROM transactionlog
WHERE merchantcode = 'merc0003'
GROUP BY TRUNC(systemdate)
)
SELECT dr.the_date, COALESCE(trans_log.num_obj, 0)
FROM date_range dr LEFT JOIN the_data trans_log
ON dr.the_date = trans_log.log_date
ORDER BY dr.the_date DESC;
Incidentally, you can just use aliases for your CTEs; you don't need to give them full names only to alias them again.
Here are the dates available in my Record_Date (Date) column in Attendance table in Oracle 10g.You can find the dates 04/06/2016 08/06/2016 16/06/2016 23/06/2016 29/06/2016 are missing in the sequence.
**Record_Date**
01/06/2016
02/06/2016
03/06/2016
05/06/2016
06/06/2016
07/06/2016
09/06/2016
10/06/2016
12/06/2016
13/06/2016
14/06/2016
15/06/2016
17/06/2016
18/06/2016
19/06/2016
20/06/2016
21/06/2016
22/06/2016
24/06/2016
25/06/2016
26/06/2016
27/06/2016
28/06/2016
30/06/2016
01/07/2016
I just need a query to find the missing dates in the specific month (and later also in the Year).
Kindly show me an approach
You can use this one:
WITH all_days AS
(SELECT DATE '2016-06-01' + LEVEL-1 AS the_day
FROM dual
CONNECT BY DATE '2016-06-01' + LEVEL-1 <= DATE '2016-06-30')
SELECT the_day
FROM all_days
WHERE the_day <>ALL (SELECT Record_Date FROM Attendance);
Or, if you like to have it more dynamically:
WITH all_days AS
(SELECT START_DATE + LEVEL AS the_day
FROM dual
CROSS JOIN
(SELECT
TRUNC(MIN(Record_Date), 'MM') -1 AS START_DATE,
TRUNC(LAST_DAY(MAX(Record_Date))) AS END_DATE
FROM Attendance)
CONNECT BY START_DATE + LEVEL <= END_DATE)
SELECT the_day
FROM all_days
WHERE the_day <>ALL (SELECT Record_Date FROM Attendance);
Note, <>ALL is the same as NOT IN - it's just my personal preference.
with
nums(num )
as
(select 0 from dual
union all
select num + 1 from nums
where num < (select max(col) from qtable)- (select min(col) from qtable)
),
date_btwn(dat)
as(select num + (select min(col) from qtable) from nums)
select dat from date_btwn
minus
select col from qtable;
Inline views nums will generate all numbers to add from start date. date_btwn contains all dates between start date and end date in table. We are excluding the dates in our table using minus.
You need to generate all dates and you have to find missing ones. Below with cte i have done it
Using CTE and not inQuery :
with calendar as (
select rownum - 1 as daynum,
to_date('1-jun-2016') + (rownum - 1) as monthdate
from dual )
select monthdate as monthdate
from calendar
where monthdate not in (
SELECT
Record_Date
FROM Attendance )
and monthdate >= to_date('1-jun-2016') /* CHANGE as per need */
and monthdate < to_date('1-jul-2016') /* CHANGE as per need */
Or Using CTE and left join Query :
with calendar as (
select rownum - 1 as daynum,
to_date('1-jun-2016') + (rownum - 1) as monthdate
from dual )
select monthdate as monthdate
from calendar C left join Attendance A on A.Record_Date=c.monthdate
where A.Record_Date is null
and monthdate >= to_date('1-jun-2016') /* CHANGE as per need */
and monthdate < to_date('1-jul-2016') /* CHANGE as per need */
as per datecolumn format change it to validformat into selection query first then use this query.
I have a SQL query which displays count, date, and time.
This is what the output looks like:
And this is my SQL query:
select
count(*),
to_char(timestamp, 'MM/DD/YYYY'),
to_char(timestamp, 'HH24')
from
MY_TABLE
where
timestamp >= to_timestamp('03/01/2016','MM/DD/YYYY')
group by
to_char(timestamp, 'MM/DD/YYYY'), to_char(timestamp, 'HH24')
Now, in COUNT column, I want to display 0 if the count doesn't exist for that hour. So on 3/2/2016 at 8am, the count was 6. Then at 9am the count was 0 so that row didn't get displayed. I want to display that row. And at 10am & 11am, the counts are displayed then it just goes to next day.
So how do I display count of 0? I want to display 0 count for each day every hour doesn't matter if it's 0 or 6 or whatever. Thanks :)
Use a partition outer join:
SELECT m.day,
h.hr,
COALESCE( freq, 0 ) AS freq
FROM ( SELECT LEVEL - 1 AS hr
FROM DUAL
CONNECT BY LEVEL <= 24
) h
LEFT OUTER JOIN
( SELECT COUNT(*) AS freq,
TO_CHAR( "timestamp", 'mm/dd/yyyy' ) AS day,
EXTRACT( HOUR FROM "timestamp" ) AS hr
FROM MY_TABLE
WHERE "timestamp" >= TIMESTAMP '2016-03-01 00:00:00'
GROUP BY
TO_CHAR( "timestamp", 'mm/dd/yyyy' ),
EXTRACT( HOUR FROM "timestamp" )
) m
PARTITION BY ( m.day, m.hr )
ON ( m.hr = h.hr );
Use a cte to generate numbers for all the hours in a day. Then cross join the result with all the possible dates from the table. Then left join on the cte which has all date and hour combinations, to get a 0 count when a row is absent for a particular hour.
with nums(n) as (select 1 from dual
union all
select n+1 from nums where n < 24)
,dateshrscomb as (select n,dt
from nums
cross join (select distinct trunc(timestamp) dt from my_table
where timestamp >= to_timestamp('03/01/2016','MM/DD/YYYY')
) alldates
)
select count(trunc(m.timestamp)), d.dt, d.n
from dateshrscomb d
left join MY_TABLE m on to_char(m.timestamp, 'HH24') = d.n
and trunc(m.timestamp) = d.dt
and m.timestamp >= to_timestamp('03/01/2016','MM/DD/YYYY')
group by d.dt, d.n
with cteHours(h) as (select 0 from dual
union all
select h+1 from cteHours where h < 24)
, cteDates(d) AS (
SELECT
trunc(MIN(timestamp)) as d
FROM
My_Table
WHERE
timestamp >= to_timestamp('03/01/2016','MM/DD/YYYY')
UNION ALL
SELECT
d + 1 as d
FROM
cteDates
WHERE
d + 1 <= (SELECT trunc(MAX(timestamp)) FROM MY_TABLE)
)
, datesNumsCross (d,h) AS (
SELECT
d, h
FROM
cteDates
CROSS JOIN cteHours
)
select count(*), to_char(d.d, 'MM/DD/YYYY'), d.h
from datesNumsCross d
LEFT JOIN MY_TABLE m
ON d.d = trunc(m.timestamp)
AND d.h = to_char(m.timestamp, 'HH24')
group by d.d, d.h
#VPK is doing a good job at answering, I just happened to be writing this at the same time as his last edit to generate a date hour cross join. This solution differs from his in that it will get all dates between your desired max and min. Where as his will get only the dates within the table so if you have a day missing completely it would not be represented in his but would in this one. Plus I did a little clean up on the joins.
Here is one way to do that.
Using Oracle's hierarchical query feature and level psuedo column, generate the dates and hours.
Then do an outer join of above with your data.
Need to adjust the value of level depending upon your desired range (This example uses 120). Start date needs to be set as well. It is ( trunc(sysdate, 'hh24')-2/24 ) in this example.
select nvl(c1.cnt, 0), d1.date_part, d1.hour_part
from
(
select
to_char(s.dt - (c.lev)/24, 'mm/dd/yyyy') date_part,
to_char(s.dt - (c.lev)/24, 'hh24') hour_part
from
(select level lev from dual connect by level <= 120) c,
(select trunc(sysdate, 'hh24')-2/24 dt from dual) s
where (s.dt - (c.lev)/24) < trunc(sysdate, 'hh24')-2/24
) d1
full outer join
(
select
count(*) cnt,
to_char(timestamp, 'MM/DD/YYYY') date_part,
to_char(timestamp, 'HH24') hour_part
from
MY_TABLE
where
timestamp >= to_timestamp('03/01/2016','MM/DD/YYYY')
group by
to_char(timestamp, 'MM/DD/YYYY'), to_char(timestamp, 'HH24')
) c1
on d1.date_part = c1.date_part
and d1.hour_part = c1.hour_part