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
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
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)
I have a Collectionview(iPad) with
flow layout
Scroll direction: horizonlal
3 items at Y and 7 items at X
Possible Device-Orientation is only Landscape.
The normal sorting is like this(one page):
01 04 07 10 13 16 19
02 05 08 11 14 17 20
03 06 09 12 15 18 21
But i need a sorting like this:
act. page............................next page
01 02 03 04 05 06 07 | 22 23 24 25 26 27 28
08 09 10 11 12 13 14 | 29 30 31 32 33 34 35
15 16 17 18 19 20 21 | 36 37 38 39 40
Is it possible to change the item-position with custom-layout?
I tried to sort the data source pagewise like 01, 08, 15 ... but this dont help if jump to an entry middle of such a page and at all i cannot use sections.
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.