Problem with UNICODE characters in substrings - memgraphdb

I've used the following code to create a node:
memgraph> CREATE (n:Node {city: 'København'}) RETURN n;
+------------------------------+
| n |
+------------------------------+
| (:Node {city: "København"}) |
+------------------------------+
1 row in set (round trip in 0.003 sec)
1 labels have been created.
1 nodes have been created.
When I try to return substring sometimes Unicode character is shown correctly, other times not nut the count is off when it is displayed corecttly.
memgraph> MATCH (n:Node) RETURN substring(n.city, 0, 1);
+-------------------------+
| substring(n.city, 0, 1) |
+-------------------------+
| "K" |
+-------------------------+
1 row in set (round trip in 0.000 sec)
memgraph> MATCH (n:Node) RETURN substring(n.city, 0, 2);
+-------------------------+
| substring(n.city, 0, 2) |
+-------------------------+
| "K�" |
+-------------------------+
1 row in set (round trip in 0.001 sec)
memgraph> MATCH (n:Node) RETURN substring(n.city, 0, 3);
+-------------------------+
| substring(n.city, 0, 3) |
+-------------------------+
| "Kø" |
+-------------------------+
1 row in set (round trip in 0.001 sec)
memgraph> MATCH (n:Node) RETURN substring(n.city, 0, 4);
+-------------------------+
| substring(n.city, 0, 4) |
+-------------------------+
| "Køb" |
+-------------------------+
1 row in set (round trip in 0.000 sec)
How can I resolve this issue?

This is a known bug and there is an GitHub issue. You will need to wait for the fix.

Related

Cumulative Return For the following data

Actually I was reading a blog on how to calculate the cumulating return of a stock for each day.
The formula that was described in the blog to calculate cumulating return was
(1 + TodayReturn) * (1 + Cumulative_Return_Of_Previous_Day) - 1 , but still I am not able to calculate the cumulative return that was provided on it.
Can someone please make this clear that how is cumulative return has been calculated in the table given below. This would be a lot of help.
Thanks in advance.
| Days | Stock Price | Return | Cumulative Return|
---------------------------------------------------
| Day 1 | 150 | | |
| Day 2 | 153 | 2.00 % | 2.00 % |
| Day 3 | 160 | 4.58 % | 6.67 % |
| Day 4 | 163 | 1.88 % | 8.67 % |
| Day 5 | 165 | 1.23 % | 10.00 % |
---------------------------------------------------

Filtering with criteria applied to subsets in Google Sheets

See this doc for live example
Given this data:
+-----------------+-----------+--------------------+
| extraction_date | seller_id | listings_remaining |
+-----------------+-----------+--------------------+
| 2020-02-03 | 110569676 | 69 |
| 2020-02-03 | 91489962 | 10 |
| 2020-01-04 | 120000084 | 4 |
| 2020-02-01 | 102356225 | 3 |
| 2020-02-26 | 110569676 | 176 |
| 2020-02-26 | 91489962 | 12 |
| 2020-02-10 | 120000084 | 8 |
+-----------------+-----------+--------------------+
I want to return one row per seller, matching the row with the latest extraction date per seller, and only if the latest extraction date of that seller is more than 15 days in the past.
'=filter(A4:C4,(match(A4,maxifs(A4:A10,B4:B10,"="&B4:B10),0))*(A4:A10<today()-15))
I cannot seem to make it work. I tried maxifs'ing without the match, inserting the 15 day condition into the maxif... but filter function resists all my attempts.
Edit: this is going to be part of an already existing working FILTER function, that's why I am constraining myself to the user of filter and not QUERY or database functions.
use:
=QUERY(SORTN(SORT(A4:C, 1, 0), 999^99, 2, 2, 0),
"where Col1 < date '"&TEXT(TODAY()-15, "yyyy-MM-dd")&"'", 0)
or with FILTER
=FILTER(SORTN(SORT(A4:C, 1, 0), 999^99, 2, 2, 0),
INDEX(SORTN(SORT(A4:C, 1, 0), 999^99, 2, 2, 0),,1)<TODAY()-15)

Oracle unpivot many to one

I would like to unpivot from 1 column to 3 rows and my data looks like this.
to transform to this.
With this code I get 1 to 2, but not 1 to 3:
SELECT *
FROM (SELECT ID,
CAT1_Y1_FACT,
CAT1_Y2_FACT,
CAT1_Y3_FACT,
CAT2_Y1_FACT,
CAT2_Y2_FACT,
CAT2_Y3_FACT
FROM TABLE T) UNPIVOT(CAT1_Y1_FACT FOR NBR_YEARS IN(CAT1_Y1_FACT AS 1,
CAT1_Y1_FACT AS 2,
CAT1_Y1_FACT AS 3))
Use UNPIVOT with multiple arguments in the FOR ( ... ) IN clause:
Oracle Setup:
CREATE TABLE table_name (
id, cat1_y1_fact, cat1_y2_fact, cat1_y3_fact, cat2_y1_fact, cat2_y2_fact, cat2_y3_fact
) AS
SELECT LEVEL,
100 * LEVEL + 11,
100 * LEVEL + 12,
100 * LEVEL + 13,
100 * LEVEL + 21,
100 * LEVEL + 22,
100 * LEVEL + 23
FROM DUAL
CONNECT BY LEVEL <= 3;
Query:
SELECT *
FROM table_name
UNPIVOT ( fact FOR (cat, year) IN (
cat1_y1_fact AS (1, 1),
cat1_y2_fact AS (1, 2),
cat1_y3_fact AS (1, 3),
cat2_y1_fact AS (2, 1),
cat2_y2_fact AS (2, 2),
cat2_y3_fact AS (2, 3)
) )
Output:
ID | CAT | YEAR | FACT
-: | --: | ---: | ---:
1 | 1 | 1 | 111
1 | 1 | 2 | 112
1 | 1 | 3 | 113
1 | 2 | 1 | 121
1 | 2 | 2 | 122
1 | 2 | 3 | 123
2 | 1 | 1 | 211
2 | 1 | 2 | 212
2 | 1 | 3 | 213
2 | 2 | 1 | 221
2 | 2 | 2 | 222
2 | 2 | 3 | 223
3 | 1 | 1 | 311
3 | 1 | 2 | 312
3 | 1 | 3 | 313
3 | 2 | 1 | 321
3 | 2 | 2 | 322
3 | 2 | 3 | 323
db<>fiddle here
For multiple pivot columns the syntax is given in the Oracle SELECT documentation:
Oracle Setup:
CREATE TABLE table_name (
id, cat1_y1_fact, cat1_y2_fact, cat1_y3_fact, cat2_y1_fact, cat2_y2_fact, cat2_y3_fact,
cat1_y1_value, cat1_y2_value, cat1_y3_value, cat2_y1_value, cat2_y2_value, cat2_y3_value
) AS
SELECT LEVEL,
100 * LEVEL + 11,
100 * LEVEL + 12,
100 * LEVEL + 13,
100 * LEVEL + 21,
100 * LEVEL + 22,
100 * LEVEL + 23,
1000 * LEVEL + 110,
1000 * LEVEL + 120,
1000 * LEVEL + 130,
1000 * LEVEL + 210,
1000 * LEVEL + 220,
1000 * LEVEL + 230
FROM DUAL
CONNECT BY LEVEL <= 3;
Query:
SELECT *
FROM table_name
UNPIVOT ( (fact, value) FOR (cat, year) IN (
(cat1_y1_fact, cat1_y1_value) AS (1, 1),
(cat1_y2_fact, cat1_y2_value) AS (1, 2),
(cat1_y3_fact, cat1_y3_value) AS (1, 3),
(cat2_y1_fact, cat2_y1_value) AS (2, 1),
(cat2_y2_fact, cat2_y2_value) AS (2, 2),
(cat2_y3_fact, cat2_y3_value) AS (2, 3)
) )
Output:
ID | CAT | YEAR | FACT | VALUE
-: | --: | ---: | ---: | ----:
1 | 1 | 1 | 111 | 1110
1 | 1 | 2 | 112 | 1120
1 | 1 | 3 | 113 | 1130
1 | 2 | 1 | 121 | 1210
1 | 2 | 2 | 122 | 1220
1 | 2 | 3 | 123 | 1230
2 | 1 | 1 | 211 | 2110
2 | 1 | 2 | 212 | 2120
2 | 1 | 3 | 213 | 2130
2 | 2 | 1 | 221 | 2210
2 | 2 | 2 | 222 | 2220
2 | 2 | 3 | 223 | 2230
3 | 1 | 1 | 311 | 3110
3 | 1 | 2 | 312 | 3120
3 | 1 | 3 | 313 | 3130
3 | 2 | 1 | 321 | 3210
3 | 2 | 2 | 322 | 3220
3 | 2 | 3 | 323 | 3230
db<>fiddle here

Count Max number of day continious overdraff

Date | Account | Amount | Count max number of day continuous < 0 |
1 | 1001 | 100 | 0 |
2 | 1001 | -100 | 1 |
3 | 1001 | -100 | 2 |
4 | 1001 | 100 | 2 |
5 | 1001 | -100 | 2 |
6 | 1001 | -100 | 2 |
7 | 1001 | -100 | 3 |
8 | 1001 | -100 | 4 |
9 | 1001 | 100 | 4 |
I have sample data. I want have column "Count max number of day continuous < 0". How i can select it in database oracle
In order to find continuous periods you can use Tabibitosian method. Then use analytical count and finally max:
select date_, account, amount,
max(cnt) over (partition by account order by date_) max_overdraft_period
from (
select date_, account, amount,
count(case when amount <= 0 then 1 end)
over (partition by account, grp order by date_) cnt
from (
select date_, account, amount,
date_ - sum(case when amount <= 0 then 1 else 0 end)
over (partition by account order by date_) grp
from t ))
demo
I assumed that dates are continuous, if not then use row numbering at first.

Oracle ROUND HALF EVEN [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Is there any Oracle function to perform the ROUND HALF EVEN?
I've found this post MySQL round half however I couldn't make it work in Oracle
CREATE FUNCTION roundHalfEven (numberToRound IN NUMBER, roundPrecision IN NUMBER)
RETURN NUMBER
IS roundedNumber NUMBER;
BEGIN
DECLARE digitEvenOdd NUMBER;
DECLARE digitPosition NUMBER;
DECLARE digitToRound NUMBER;
DECLARE roundedNumber DECIMAL(20,6) DEFAULT 0;
SET digitPosition = INSTR(numberToRound, '.');
IF (roundingPrecision < 1) THEN
SET digitPosition = digitPosition + roundingPrecision;
ELSE
SET digitPosition = digitPosition + roundingPrecision + 1;
END IF;
IF (digitPosition > 0 AND digitPosition <= CHAR_LENGTH(numberToRound)) THEN
SET digitToRound = CAST(SUBSTR(numberToRound, digitPosition, 1) AS UNSIGNED);
SET digitPosition = digitPosition - 1;
IF (digitPosition > 0 AND digitPosition <= CHAR_LENGTH(numberToRound)) THEN
SET digitEvenOdd = CAST(SUBSTR(numberToRound, digitPosition, 1) AS UNSIGNED);
END IF;
END IF;
IF (digitToRound > -1) THEN
IF (digitToRound >= 5 AND digitEvenOdd IN (1,3,5,7,9)) THEN
SET roundedNumber = ROUND(numberToRound, roundingPrecision);
ELSE
SET roundedNumber = TRUNCATE(numberToRound, roundingPrecision);
END IF;
ELSE IF (roundingPrecision > 0) THEN
SET roundedNumber = numberToRound;
END IF;
RETURN(roundedNumber);
END;
/
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE test_data( value ) AS
SELECT (LEVEL -11)/2 FROM DUAL CONNECT BY LEVEL <= 21;
Query 1:
SELECT value,
ROUND( value ),
CASE
WHEN MOD( ABS( value ), 2 ) = 0.5
THEN TRUNC( value )
ELSE ROUND( value )
END AS round_half_even
FROM test_data
Results:
| VALUE | ROUND(VALUE) | ROUND_HALF_EVEN |
|-------|--------------|-----------------|
| -5 | -5 | -5 |
| -4.5 | -5 | -4 |
| -4 | -4 | -4 |
| -3.5 | -4 | -4 |
| -3 | -3 | -3 |
| -2.5 | -3 | -2 |
| -2 | -2 | -2 |
| -1.5 | -2 | -2 |
| -1 | -1 | -1 |
| -0.5 | -1 | 0 |
| 0 | 0 | 0 |
| 0.5 | 1 | 0 |
| 1 | 1 | 1 |
| 1.5 | 2 | 2 |
| 2 | 2 | 2 |
| 2.5 | 3 | 2 |
| 3 | 3 | 3 |
| 3.5 | 4 | 4 |
| 4 | 4 | 4 |
| 4.5 | 5 | 4 |
| 5 | 5 | 5 |
Or as a function:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE test_data( value ) AS
SELECT (LEVEL -11)/20 FROM DUAL CONNECT BY LEVEL <= 21
/
CREATE FUNCTION round_half_even(
value NUMBER,
prec INTEGER DEFAULT 0
) RETURN NUMBER
IS
whole NUMBER := POWER( 10, -prec );
BEGIN
RETURN CASE
WHEN ABS( MOD( value, 2*whole ) ) = 0.5*whole
THEN TRUNC( value, prec )
ELSE ROUND( value, prec )
END;
END;
/
Query 1:
SELECT value,
ROUND( value , 1),
round_half_even( value, 1 )
FROM test_data
Results:
| VALUE | ROUND(VALUE,1) | ROUND_HALF_EVEN(VALUE,1) |
|-------|----------------|--------------------------|
| -0.5 | -0.5 | -0.5 |
| -0.45 | -0.5 | -0.4 |
| -0.4 | -0.4 | -0.4 |
| -0.35 | -0.4 | -0.4 |
| -0.3 | -0.3 | -0.3 |
| -0.25 | -0.3 | -0.2 |
| -0.2 | -0.2 | -0.2 |
| -0.15 | -0.2 | -0.2 |
| -0.1 | -0.1 | -0.1 |
| -0.05 | -0.1 | 0 |
| 0 | 0 | 0 |
| 0.05 | 0.1 | 0 |
| 0.1 | 0.1 | 0.1 |
| 0.15 | 0.2 | 0.2 |
| 0.2 | 0.2 | 0.2 |
| 0.25 | 0.3 | 0.2 |
| 0.3 | 0.3 | 0.3 |
| 0.35 | 0.4 | 0.4 |
| 0.4 | 0.4 | 0.4 |
| 0.45 | 0.5 | 0.4 |
| 0.5 | 0.5 | 0.5 |

Resources