Find all customers who engaged continuously for 6 months - oracle

I need code help in Oracle
I have table with the following structure. This is an aggregate table grouped by Account_ID and month (month rolled to the start date of the month) and number of transactions performed in that month.
Account_ID Date Trans_cnt
------------------------------
A00001 01Jan2018 12
A00002 01Jan2018 14
A00002 01Feb2018 01
A00003 01Feb2018 02
A00001 01Mar2018 12
I need to find accounts which have had continuous 6 months of transaction and 3 months of transactions
an example if account A00001 is analyzed for 6 months, then in a given year's time say from Jan 2018 to Dec 2018, this account should have continuous transactions from Jan to Jun or Feb to Jul; or Mar to Aug likewise.
Let me know how to come up with a sql for the same.

Related

Show each month as a column in a table in PowerBI

I want to show in a table each month of ExaminationDate rather than just the year and month. For example, currently I have ExaminationDate in Field with Year and Month selected which shows:
Year Month
2021 January
2021 Febuary
2021 March
But how would I get it to display thusly:
Year January February March etc
2021 value
2021
2021

Cross lookup in oracle without creating a table

I have a list of 20 records mapping year to a number from 2001 to 2021. For a couple of reasons these can not be loaded into a table, and I do not have permissions to create temporary tables. This lookup means I can't just run a single query in oracle - I have to export and join with a script. Is there a way I could just do a lookup in memory? I could do a CASE WHEN statement to handle each of the 20 cases. But is there some other smoother way to check values against a list in Oracle when you can't write to a table in between?
If I understood you correctly, a CTE might help:
SQL> with years as
2 (select 2000 + level as year
3 from dual
4 connect by level <= 21
5 )
6 select year
7 from years
8 /
YEAR
----------
2001
2002
2003
2004
<snip>
2020
2021
21 rows selected.
SQL>
You'd now join years with other tables, e.g.
with years as
...
select y.year, e.hiredate
from years y join employees e on e.year = y.year
where ...

Oracle SQL date getting first day of last quarter and last day of last quarter

I've been asked to provide an Oracle PL/SQL solution if a file is loaded into the system
for example between the dates of 1st Jan 2017 - 31st March 2017 I should
created two dates from the last quarter a loaded from date of
1st Oct 2016 and loaded to date of 31st Dec 2016. This should be future prove meaning it should work for future years, so if a file is loaded into the system lets say 21st August 2019, it should have a from date of 1st April 2019 and a to date of 30th June 2019.
This should be a PL/SQL solution most probably a procedure returning two dates to the main program and the to and from date returned should be in the format of DD/MM/YYYY.
Thanks in advance.
What about this solution?
SELECT
TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3), 'DD/MM/YYYY') AS output_from_date,
TO_CHAR(TRUNC(SYSDATE, 'Q')-1, 'DD/MM/YYYY') AS output_to_date
FROM dual;
OUTPUT_FROM_DATE OUTPUT_TO_DATE
01/04/2017 30/06/2017

Oracle SQL Developer: Sum rows based on value from another column

Trans. ID QTY TOOL Date
1 20 A2 2015
2 20 A2 2016
3 20 A2 2016
4 20 A3 2015
5 20 A3 2016
6 20 A3 2016
7 20 C 2016
8 20 C 2016
9 20 C 2016
10 20 C 2016
To Return
--Interested in 2016 only
Tool Total
A2 40
A3 40
Im not interested in the individual IDS. Just more interested totals.
Please notw Im also interested in grabbing data with tools specifically starting with the letter A since the actual table has a bunch of tools.
Im having a hard time figuring out how to put the WHERE statement and other things. I read in another thread to use distincts as well.
select Tool, sum(QTY)
from
(select distinct Tool, ID, QTY from "table")
where Tool like 'A%'
AND Date like '2016%'
group by PROCESS_EQP_ID
If you do the distinct in your subquery, you are going to knock out one of the rows with ids 2 and 3. However, from your expected output, you want both to be summed.
Also, what datatype is the date column? If it's DATE or TIMESTAMP, then I think this is what you're after:
select tool, sum(qty)
from "table"
where tool like 'A%'
and to_char(dt, 'yyyy') = '2016' -- note that I've renamed the date column as `date` is a reserved word in Oracle.
group by tool;

Power failure and Oracle data recovery

Database is OracleXE and here is the problem:
data gets entered in tables
UPS does not survive power shock
Oracle server reboots after power failure
everything seems normal
after some time we realize that some data is missing from few tables (this is ok, because all inserts happened in one transaction), and some data seems like half-committed
couple of reboots done by employee
strangest thing, half-committed data recovered to normal!
I guess data loss is possible, but is it possible to loose some part of transaction?
Does Oracle have some sort of recovery after these situations?
Scenario is written on the basis of my app logs and Oracle logs because it's remote system.
[EDIT]
My DBA is at home sick.
listener.log seems ok and I'm not much of a reader of alert_xe.log :)
I guess this is relevant info:
Oracle Data Guard is not available in this edition of Oracle.
Thu Oct 15 10:52:05 2009
alter database mount exclusive
Thu Oct 15 10:52:09 2009
Setting recovery target incarnation to 2
Thu Oct 15 10:52:09 2009
Successful mount of redo thread 1, with mount id 2581406229
Thu Oct 15 10:52:09 2009
Database mounted in Exclusive Mode
Completed: alter database mount exclusive
Thu Oct 15 10:52:09 2009
alter database open
Thu Oct 15 10:52:10 2009
Beginning crash recovery of 1 threads
Thu Oct 15 10:52:10 2009
Started redo scan
Thu Oct 15 10:52:10 2009
Completed redo scan
3923 redo blocks read, 520 data blocks need recovery
Thu Oct 15 10:52:10 2009
Started redo application at
Thread 1: logseq 649, block 88330
Thu Oct 15 10:52:12 2009
Recovery of Online Redo Log: Thread 1 Group 2 Seq 649 Reading mem 0
Mem# 0 errs 0: C:\ORACLEXE\APP\ORACLE\FLASH_RECOVERY_AREA\XE\ONLINELOG\O1_MF_2_558PBOPG_.LOG
Thu Oct 15 10:52:14 2009
Completed redo application
Thu Oct 15 10:52:14 2009
Completed crash recovery at
Thread 1: logseq 649, block 92253, scn 7229931
520 data blocks read, 498 data blocks written, 3923 redo blocks read
Thu Oct 15 10:52:15 2009
Thread 1 advanced to log sequence 650
Thread 1 opened at log sequence 650
[EDIT:]
"Write Caching" was left by mistake.
That explains data loss.
Sounds very odd to me. Data either is or is not commited. I suspect skulduggery by one of your collegues.
From your alert log, it looks like a normal automatic instance recovery. The last two lines indicate to me that the database is open and writing redo logs. There's no way I'd believe that a partial transaction existed. It's either committed or not - no in-between state exists.

Resources