I have one requirement in SQL where
We have couple of market ids
Let say 20,30,40
And here for each market using one plsql function we are getting one campaign number let's say for market 20 I got 20210306,for mrkt 30 we got 20210307, and for 40 mrkt id 20210308.
Now what I want to achieve for each market we should have current campaign no as 20210306 as well as 12 future campaign should be produced by SQL query based on each current campaign of the market.
I am doing it using union all and campaign+1,2,3 and so on which is taking time . Do we have some brief logic for it.
Please suggest
Thanks,
Vijay
You can generate series of data (numbers or dates or even characters) using the LEVEL pseudo column with a CONNECT BY clause.
In your case, it sounds like you want generate the next 10 numbers starting from 20210306.
That could be done like this:
WITH current_campaign ( nr ) AS
( SELECT 20210306 FROM dual )
SELECT
nr + level
FROM
current_campaign
CONNECT BY
level < 13;
20210307
20210308
20210309
20210310
20210311
20210312
20210313
20210314
20210315
20210316
20210317
20210318
The 20210306 could also represent the date March, 6, 2021. If you want to generate those numbers date based numbers you could do that too. For example (starting from 0629 so the result shows the month change, only showing 5 rows)
WITH current_campaign ( cdate ) AS
( SELECT DATE'2021-06-29' FROM dual ) SELECT
TO_CHAR(cdate + level,'YYYYDDMM')
FROM
current_campaign
CONNECT BY
level < 6;
20213006
20210107
20210207
20210307
20210407
Related
Consider if there is a table with two column
TABLE TIME_FRAME
------------------------
|FROM |TO |
|2013-12-13 |2014-01-06|
|2011-12-05 |2011-12-31|
|2014-01-23 |2014-02-22|
|2011-11-21 |2011-12-17|
........
FROM and TO from each row defines a period of time. Also there can be overlap between the periods (here row 2 and row 4) or cover multiple periods
if give a start_date and end_date as parameters here the requirement is about return all the dates falls within the parameters and also within any of the periods in the columns
for example
if start_date is 2013-12-25 and end_date is 2014-02-10
so from above data it should return all dates between
`2013-12-25` and `2014-01-06`
plus
`2014-01-23` and `2014-02-10`
is it possible to create a query for above requirement (not by PL/SQL)?
It's possible by creating set of days using LEVEL recursion operator and then filtering this set by comparing it to your table data. Here is the working Oracle SQL query for you:
select day
from (select to_date('25-DEC-2013', 'dd-mon-yyyy') - 1 + level day
from dual
connect by level <= to_date('10-FEB-2014', 'dd-mon-yyyy') -
to_date('25-DEC-2013', 'dd-mon-yyyy') + 1)
where exists
(select 1 from TIME_FRAME p where day between p.FROM and p.TO);
Hope this helps!
Hi I have source data in a field in different formats like below
1Y3M6D (1 Year, 3 Months, 6 Days) I need to split this into 3 fields Year, Month, Days but the source data format can change like month can come first as 3M1Y6D OR source data can only have 3M with no year and day. How do I write a query to get the preceding number from M, Y or D?
Thanks in advance for help.
Thanks everyone, Unoembre command helped.
select my_value, REGEXP_SUBSTR(my_value,'(\d+)Y',1,1,NULL,1) REG_Y, REGEXP_SUBSTR(my_value,'(\d+)M',1,1,NULL,1) REG_M, REGEXP_SUBSTR(my_value,'(\d+)D',1,1,NULL,1) REG_D from ( select '3M6Y2D' my_value from dual );
Here is the query I have: I would like to display 6 periods back and 12 periods forward from the value in the PERIOD column:
SELECT
item_id,
min(period),
min(picks_class),
min(vau_class),
min(warehouse_code),
min(stocked)
FROM
(select
i.warehouse_code,
ih.item_id,
ih.picks_class,
ih.period,
ih.vau_class,
ih.stocked,
substr(i.pareto_set, 6, 6) AS "Policy"
from d_item_history ih
join d_item_snapshot i on ih.item_id=i.id
where ih.picks_class='P2'
and i.supplier_code='DC'
and ih.valid_for_calc='Y'
and ih.vau_class in ('C1', 'C2')
and i.warehouse_code='YAFD'
-- and ih.item_id='1427084842208'
order by ih.period asc
)
group by item_id
;
Here is the output of the code: I would like to display the picks class for each one of the ITEM_IDs; 6 periods back from queried period and 12 months forward.
Please let me know if that is at all possible if/or how. All help is appreciated
When you want to calculate 6 periods back or 12 periods forward, Are you looking to move by months ? If so, the following code snippet might help you :
select to_char(add_months(to_date(period,'YYYYMM'),-6),'YYYYMM') PERIOD_6,
to_char(add_months(to_date(period,'YYYYMM'),12),'YYYYMM')PERIOD_12
from dual
Scratching my head here. I have a very simple postgres table from which I need to select a unique row per day, based solely on a text column which updates as follows.
First update= 'AA1', 2nd update= 'AB', 3rd update= 'D4'
id item date run value
---------------------------------------
23 apple 01/01/16 AA1 232
25 apple 01/01/16 AB 254
26 apple 01/01/16 D4 212
Depending on the time of day, running a query based on the date ('01/01/2016') would return 1, 2 or 3 rows. However I only need the latest row e.g. Run = D4 above.
How can I write a simple select query that always returns just the latest row based of a text based column? I presume i need to create a ranking based on the 'run' column but Im not sure how to do this.
regards
Using the handy distinct on:
select distinct on (date) *
from t
order by date, run desc
I am trying to select next 30 dates for an input date parameter, i.e. if I enter 2/3/2016 I should be able to select next 30 days. Is there a way to do it in oracle ?
The expected output is :
3/3/2016
4/3/2016
5/3/2016....
Try the following, replacing sysdate with your start date:
select trunc(sysdate) + level
from dual
connect by level <= 30
This is based on hierarchical queries and on the fact that adding a number to a date means adding days.
LEVEL is a pseudo-column, disposable where you use CONNECT BY, which indicates, as it seems, the level in recursion; so, the first occurrence has level 1, then 2, and so on.
Here are some more details on syntax and on the way this implements recursion.
Given that we have recursion on DUAL, with no conditions ( no PRIOR clause used), we use LEVEL both to sum 1, 2, 3 ( + LEVEL) days and to limit the number of recursione levels we need ( <=30)