Generate One year dates using Oracle SQL - sql
I have a table with three ID and Dates
i want generate dates of one year from current date and before and after six months
Then i want to plot three dates on this one year generated calandar
I tried
SELECT (TO_DATE(SYSDATE)-180) + ROWNUM DT
FROM (SELECT 1
FROM DUAL
CONNECT BY LEVEL <=180)
The concept of "month" is variable, so you may want to double check if "6 months" is really what you're looking for. It can give unexpected results if you don't know the oddities of date math.
That being said, a calendar which contains 6 months on either side of the current date can be generated with the following. Note that 184 days is the longest a 6 month span can last (July-December).
SELECT dt
FROM (SELECT TRUNC(SYSDATE - 184) + LEVEL AS dt
FROM dual
CONNECT BY LEVEL <= 369)
WHERE dt BETWEEN add_months(TRUNC(SYSDATE), -6) AND add_months(TRUNC(SYSDATE), 6);
Literally following what you said: there's a table with 3 IDs and their dates, and you want to plot the calendar.
Using a hierarchical query, dates are generated as
SQL> with
2 threeid (id, datum) as
3 (select 1, date '2020-05-29' from dual union all
4 select 2, date '2020-01-01' from dual union all
5 select 3, date '2020-02-29' from dual
6 ),
7 cal as
8 (select add_months(datum, -6) + column_value - 1 datum
9 from threeid cross join
10 table(cast(multiset(select level from dual
11 connect by level <= add_months(datum, 6) - add_months(datum, -6)
12 ) as sys.odcinumberlist))
13 where id = 1 --> for the 1st ID; change that for other calendars
14 )
15 select * from cal order by datum;
DATUM
----------
29.11.2019
30.11.2019
01.12.2019
02.12.2019
03.12.2019
<snip>
25.11.2020
26.11.2020
27.11.2020
28.11.2020
366 rows selected.
If you want to plot that, instead of line #15 you'd use this ...
<snip>
15 select lpad(month, 20, ' ') month,
16 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
17 from (select to_char(dt, 'fmMonthfm YYYY') month,
18 week,
19 max(decode(to_char(dt, 'd'), '1', lpad(to_char(dt, 'fmdd'), 2))) "Mon",
20 max(decode(to_char(dt, 'd'), '2', lpad(to_char(dt, 'fmdd'), 2))) "Tue",
21 max(decode(to_char(dt, 'd'), '3', lpad(to_char(dt, 'fmdd'), 2))) "Wed",
22 max(decode(to_char(dt, 'd'), '4', lpad(to_char(dt, 'fmdd'), 2))) "Thu",
23 max(decode(to_char(dt, 'd'), '5', lpad(to_char(dt, 'fmdd'), 2))) "Fri",
24 max(decode(to_char(dt, 'd'), '6', lpad(to_char(dt, 'fmdd'), 2))) "Sat",
25 max(decode(to_char(dt, 'd'), '7', lpad(to_char(dt, 'fmdd'), 2))) "Sun"
26 from ( select dt,
27 case when dt >= to_date(to_char(dt, 'dd/') ||'12/'||
28 to_char(sysdate,'yyyy'), 'dd/mm/yyyy')
29 and wk = '01'
30 then '53'
31 else wk
32 end week
33 from (select datum dt, to_char(datum, 'iw') wk
34 from cal
35 )
36 )
37 group by to_char(dt, 'fmMonthfm YYYY'), week
38 )
39 order by to_date( month, 'Month YYYY' ), to_number(week);
... and get that:
MONTH Mon Tue Wed Thu Fri Sat Sun
-------------------- -------- -------- -------- -------- -------- -------- --------
November 2019 29 30
December 2019 30 31
December 2019 1
December 2019 2 3 4 5 6 7 8
December 2019 9 10 11 12 13 14 15
December 2019 16 17 18 19 20 21 22
December 2019 23 24 25 26 27 28 29
January 2020 1 2 3 4 5
January 2020 6 7 8 9 10 11 12
January 2020 13 14 15 16 17 18 19
January 2020 20 21 22 23 24 25 26
January 2020 27 28 29 30 31
February 2020 1 2
February 2020 3 4 5 6 7 8 9
February 2020 10 11 12 13 14 15 16
February 2020 17 18 19 20 21 22 23
February 2020 24 25 26 27 28 29
March 2020 1
March 2020 2 3 4 5 6 7 8
March 2020 9 10 11 12 13 14 15
March 2020 16 17 18 19 20 21 22
March 2020 23 24 25 26 27 28 29
March 2020 30 31
April 2020 1 2 3 4 5
April 2020 6 7 8 9 10 11 12
April 2020 13 14 15 16 17 18 19
April 2020 20 21 22 23 24 25 26
April 2020 27 28 29 30
May 2020 1 2 3
May 2020 4 5 6 7 8 9 10
May 2020 11 12 13 14 15 16 17
May 2020 18 19 20 21 22 23 24
May 2020 25 26 27 28 29 30 31
June 2020 1 2 3 4 5 6 7
June 2020 8 9 10 11 12 13 14
June 2020 15 16 17 18 19 20 21
June 2020 22 23 24 25 26 27 28
June 2020 29 30
July 2020 1 2 3 4 5
July 2020 6 7 8 9 10 11 12
July 2020 13 14 15 16 17 18 19
July 2020 20 21 22 23 24 25 26
July 2020 27 28 29 30 31
August 2020 1 2
August 2020 3 4 5 6 7 8 9
August 2020 10 11 12 13 14 15 16
August 2020 17 18 19 20 21 22 23
August 2020 24 25 26 27 28 29 30
August 2020 31
September 2020 1 2 3 4 5 6
September 2020 7 8 9 10 11 12 13
September 2020 14 15 16 17 18 19 20
September 2020 21 22 23 24 25 26 27
September 2020 28 29 30
October 2020 1 2 3 4
October 2020 5 6 7 8 9 10 11
October 2020 12 13 14 15 16 17 18
October 2020 19 20 21 22 23 24 25
October 2020 26 27 28 29 30 31
November 2020 1
November 2020 2 3 4 5 6 7 8
November 2020 9 10 11 12 13 14 15
November 2020 16 17 18 19 20 21 22
November 2020 23 24 25 26 27 28
64 rows selected.
SQL>
(That nice calendar query is originally (at least, I think so) posted on OraFAQ site; I adjusted it, hoping that it correctly displays dates)
Related
Indexing Time in Redshift
i have a dataset with that has sales by month and a column for when an promotion happens for a number of products. i want to index the months so that i can compare the different products over a pre and post 12m period easily this is the data i have Date sales. Promo? Jan 21 100 Feb 21 110. Mar 21 100. apr 21 110. may 21 90. jun 21 100. jul 21 120. aug 21 140. X sep 21 100. oct 21 90. nov 21 100. dec 21 120. jan 22 110. feb 22 100 this is what i want can someone tell me how to do this in redshift? Date sales. Promo? month index Jan 21 100 -7. Feb 21 110. -6 Mar 21 100. -5 apr 21 110. -4 may 21 90. -3 jun 21 100. -2 jul 21 120. -1 aug 21 140. X 0 sep 21 100. 1 oct 21 90. 2 nov 21 100. 3 dec 21 120. 4 jan 22 110. 5 feb 22 100 6
sorting (ascending & descending) value based on group same date?
I want to sort value (ascending/descending) value based on group same date. can anyone help how to achieve it? df = a b c 21-12-30 2 12 21 21-12-30 3 13 22 21-12-30 5 14 23 22-01-30 6 15 24 22-01-30 7 16 25 22-01-30 8 17 26 22-02-28 9 18 27 22-02-28 10 19 28 22-02-28 11 20 29 desired output = a b c 21-12-30 5 14 23 21-12-30 3 13 22 21-12-30 2 12 21 22-01-30 8 17 26 22-01-30 7 16 25 22-01-30 6 15 24 22-02-28 11 20 29 22-02-28 10 19 28 22-02-28 9 18 27
One option: out = (df.groupby(level=0, group_keys=False, sort=False) .apply(lambda x: x.sort_values(by='a', ascending=False)) ) Another: out = df.sort_values(by='a', ascending=False).sort_index(kind='stable') output: a b c 21-12-30 5 14 23 21-12-30 3 13 22 21-12-30 2 12 21 22-01-30 8 17 26 22-01-30 7 16 25 22-01-30 6 15 24 22-02-28 11 20 29 22-02-28 10 19 28 22-02-28 9 18 27
How can i sum values that are unique which are connected to an id which is connected to a different id on which the summation needs to be based upon?
I have a Database Warehouse. That database warehouse has to have measures but i can't seem to get the measure to work as i wanted. The measure is gonna be calculated by a dimension called document here is a table of what is currently in it: document_id client_id contract size 12 4 NULL 431 13 4 NULL 2543 14 5 NULL 2565 15 8 NULL 2345 I try to make a measure in the fact table that sums up the document_id's and combines their size. But because its a database warehouse the data is not normalized so in the fact table there are multiple rows which have the same document id so if i sum it it won't work i'll get this data if i sum it: company_id contract_id user_id task_id document_id insertion_date size_total 4 28 14 26 12 2021-01-16 431 4 28 14 27 12 2021-01-16 431 4 28 14 28 12 2021-01-16 431 4 28 14 29 12 2021-01-16 431 4 28 14 30 12 2021-01-16 431 4 28 14 26 13 2021-01-16 2543 4 28 14 27 13 2021-01-16 2543 4 28 14 28 13 2021-01-16 2543 4 28 14 29 13 2021-01-16 2543 4 28 14 30 13 2021-01-16 2543 4 29 14 26 12 2021-01-16 431 4 29 14 27 12 2021-01-16 431 4 29 14 28 12 2021-01-16 431 4 29 14 29 12 2021-01-16 431 4 29 14 30 12 2021-01-16 431 4 29 14 26 13 2021-01-16 2543 4 29 14 27 13 2021-01-16 2543 4 29 14 28 13 2021-01-16 2543 4 29 14 29 13 2021-01-16 2543 4 29 14 30 13 2021-01-16 2543 5 30 15 31 14 2021-01-16 2565 5 30 15 32 14 2021-01-16 2565 6 NULL 16 33 NULL 2021-01-16 NULL 6 NULL 16 34 NULL 2021-01-16 NULL 6 NULL 16 35 NULL 2021-01-16 NULL 6 NULL 17 33 NULL 2021-01-16 NULL 6 NULL 17 34 NULL 2021-01-16 NULL 6 NULL 17 35 NULL 2021-01-16 NULL 7 NULL NULL NULL NULL 2021-01-16 NULL 8 31 18 36 15 2021-01-16 2345 As you can see on the previous data you saw that company_id 4 had 2 documents combined the size was:431 +2543 = 2974 but i get different answers in the measure total_size. I want to have a total_size which combines unique ID's based upon their owner which is the company_id. I tried to do the following but it doesn't seem to work: insert into [Stg_zone].[customer_usage] ( size_total, company_id,contract_id,user_id, task_id,document_id,insertion_date) select sum ([size]) as size_total, cu.[client_id] , cont.[contract_id], usr.[user_id], tsk.[task_id], doc.[document_id] , SYSDATETIME ( ) as insertion_date FROM [Stg_zone].[company] cu left JOIN dbo.contract cont ON (cont.client_id=cu.client_id) left JOIN dbo.[user] usr ON (usr.client_id=cu.client_id) left JOIN dbo.document doc ON (doc.client_id=cu.client_id) left JOIN dbo.task tsk ON (tsk.client_id=cu.client_id) group by cu.[client_id] , cont.[contract_id], usr.[user_id], tsk.[task_id], doc.[document_id], doc.size, usr.role Does anybody know how i can fix this issue ?
How to get the data in decline pattern/trending using Oracle SQL?
How can I get the result only for those monthly data in decline pattern/trending ? And the data as below; ID JAN FEB MAR APR MAY JUN 112 50 45 40 35 30 20 113 30 30 30 30 30 30 114 20 25 20 20 20 20 115 45 50 60 60 30 30 Expected output ; ID JAN FEB MAR APR MAY JUN 112 50 45 40 35 30 20 115 45 50 60 60 30 30 Thanks !
SELECT * FROM your_table WHERE JAN > JUN or SELECT * FROM your_table WHERE JAN > FEB AND FEB > MAR AND MAR > APR AND APR > MAY AND MAY > JUN
It appears that what you're looking for is SELECT * FROM SOME_TABLE WHERE JAN > FEB OR FEB > MAR OR MAR > APR OR APR > MAY OR MAY > JUN Best of luck.
Transpose In SQL SERVER
Could you help me one case, below After Tranpose the table i get result as like this: Lno OID ConnectedDate 100000224 34931 Feb 7 201,Feb 7 201,Feb 8 201,Feb 8 201,Feb 4 201 100001489 9156 Jun 23 201,Jun 24 201,Jun 25 201,Jun 25 201,Oct 29 201,Oct 29 201 100002153 31514 Oct 5 201 100002740 32367 Sep 14 201,Sep 14 201,Oct 21 201,Sep 15 201,Sep 15 201,Sep 16 201 100004774 31558 May 19 201,May 19 201,May 20 201,May 20 201,Jun 2 201 100004935 5857 Sep 1 201,Sep 1 201,Sep 3 201,Sep 3 201,Sep 29 201,Aug 31 201,Sep 22 201 100004935 31684 Jun 16 201,Jun 17 201,Jun 17 201,Jun 19 201 100004983 33942 Dec 30 201,Dec 30 201,Dec 27 201,Dec 29 201,Dec 28 201 100005055 32347 Sep 14 201,Sep 13 201,Sep 13 201,Oct 1 201,Oct 5 201,Oct 20 201,Nov 17 201,Sep 15 201,Sep 16 201,Dec 4 201 100006146 31481 Apr 30 201,Apr 30 201,May 3 201,May 4 201,May 4 201,Jun 3 201,Jun 4 201,Jun 5 201,Jun 7 201,Jun 12 201 But i want output like this: LID OID ConnectedDate1 ConnectedDate2 ConnectedDate3 ConnectedDate4 100000224 34931 Feb 7 201 Feb 7 201 Feb 8 201 Feb 8 201 100001489 9156 Jun 23 201 Jun 24 201 Jun 25 201 Jun 25 201 100002153 31514 Oct 5 201 100002740 32367 Sep 14 201 Sep 14 201 Oct 21 201 Sep 15 201 100004774 31558 May 19 201 May 19 201 May 20 201 Plz help me Thanks in advance
You need the PIVOT command. To give more details, you'd need to provide your query