What is the casting in AgensGraph ver 1.3 double quote - agens-graph

I used agensgraph version 1.3.
Following query
'MATCH (a:movie) RETURN a.title
gives result:
title
---------------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Revolutions"
"The Devils Advocate"
"A Few Good Men"
"Top Gun"
"Jerry Maguire"
"Stand By Me"
I don't know why is double quote operator.
What is the way to replace ""?

You can use function "btrim" for removing needless quotes.
agens=# select btrim( n.value::text, '"' ) from ( match (n) return n.value ) as n;
btrim
-------
test
(1 row)
You must use trim on SQL part of quires for removing quotes.

Related

Snowflake: Trouble getting numbers to return from a PIVOT function

I am moving a query from SQL Server to Snowflake. Part of the query creates a pivot table. The pivot table part works fine (I have run it in isolation, and it pulls numbers I expect).
However, the following parts of the query rely on the pivot table- and those parts fail. Some of the fields return as a string-type. I believe that the problem is Snowflake is having issues converting string data to numeric data. I have tried CAST, TRY_TO_DOUBLE/NUMBER, but these just pull up 0.
I will put the code down below, and I appreciate any insight as to what I can do!
CREATE OR REPLACE TEMP TABLE ATTR_PIVOT_MONTHLY_RATES AS (
SELECT
Market,
Coverage_Mo,
ZEROIFNULL(TRY_TO_DOUBLE('Starting Membership')) AS Starting_Membership,
ZEROIFNULL(TRY_TO_DOUBLE('Member Adds')) AS Member_Adds,
ZEROIFNULL(TRY_TO_DOUBLE('Member Attrition')) AS Member_Attrition,
((ZEROIFNULL(CAST('Starting Membership' AS FLOAT))
+ ZEROIFNULL(CAST('Member Adds' AS FLOAT))
+ ZEROIFNULL(CAST('Member Attrition' AS FLOAT)))-ZEROIFNULL(CAST('Starting Membership' AS FLOAT)))
/ZEROIFNULL(CAST('Starting Membership' AS FLOAT)) AS "% Change"
FROM
(SELECT * FROM ATTR_PIVOT
WHERE 'Starting Membership' IS NOT NULL) PT)
I realize this is a VERY big question with a lot of moving parts... So my main question is: How can I successfully change the data type to numeric value, so that hopefully the formulas work in the second half of the query?
Thank you so much for reading through it all!
EDITED FOR SHORTENING THE QUERY WITH UNNEEDED SYNTAX
CAST(), TRY_TO_DOUBLE(), TRY_TO_NUMBER(). I have also put the fields (Starting Membership, Member Adds) in single and double quotation marks.
Unless you are quoting your field names in this post just to highlight them for some reason, the way you've written this query would indicate that you are trying to cast a string value to a number.
For example:
ZEROIFNULL(TRY_TO_DOUBLE('Starting Membership'))
This is simply trying to cast a string literal value of Starting Membership to a double. This will always be NULL. And then your ZEROIFNULL() function is turning your NULL into a 0 (zero).
Without seeing the rest of your query that defines the column names, I can't provide you with a correction, but try using field names, not quoted string values, in your query and see if that gives you what you need.
You first mistake is all your single quoted columns names are being treated as strings/text/char
example your inner select:
with ATTR_PIVOT(id, studentname) as (
select * from values
(1, 'student_a'),
(1, 'student_b'),
(1, 'student_c'),
(2, 'student_z'),
(2, 'student_a')
)
SELECT *
FROM ATTR_PIVOT
WHERE 'Starting Membership' IS NOT NULL
there is no "starting membership" column and we get all the rows..
ID
STUDENTNAME
1
student_a
1
student_b
1
student_c
2
student_z
2
student_a
So you need to change 'Starting Membership' -> "Starting Membership" etc,etc,etc
As Mike mentioned, the 0 results is because the TRY_TO_DOUBLE always fails, and thus the null is always turned to zero.
now, with real "string" values, in real named columns:
with ATTR_PIVOT(Market, Coverage_Mo, "Starting Membership", "Member Adds", "Member Attrition") as (
select * from values
(1, 10 ,'student_a', '23', '150' )
)
SELECT
Market,
Coverage_Mo,
ZEROIFNULL(TRY_TO_DOUBLE("Starting Membership")) AS Starting_Membership,
ZEROIFNULL(TRY_TO_DOUBLE("Member Adds")) AS Member_Adds,
ZEROIFNULL(TRY_TO_DOUBLE("Member Attrition")) AS Member_Attrition
FROM ATTR_PIVOT
WHERE "Starting Membership" IS NOT NULL
we get what we would expect:
MARKET
COVERAGE_MO
STARTING_MEMBERSHIP
MEMBER_ADDS
MEMBER_ATTRITION
1
10
0
23
150

Creating formula in SQL server

Maybe some of you could help me with creation of formula in sql. I need to perform calculations of the result of the expression for all given formulas. The notation of the formula is simple: P (X) means that the expression appends the integer X in parentheses. M (Y) means that the expression subtracts the integer Y in parentheses. The “+” symbol combines elements of the formula.
Example. given formula: P (10) + M (5) + M (3) + P (1). It translates to 10-5-3 + 1 = 3.
The result should look like this:
Those easy formulas can be done in Microsoft SQL Server.
Split the formula in the different parts with STRING_SPLIT and + as separator.
Use REPLACE to apply a negative number sign: P(X) --> X and M(X) --> -X.
Use CONVERT to turn the string parts into numbers.
Add everything up with a SUM aggregation and group by clause.
Sample data
create table input
(
formula nvarchar(50)
);
insert into input (formula) values
('P(10)+M(5)+M(3)+P(1)'),
('P(7)+M(3)+M(4)');
Solution
select i.formula,
sum(convert(int, replace(replace(replace(s.value,'P(', ''),'M(','-'),')',''))) as rez
from input i
cross apply string_split(i.formula, '+') s
group by i.formula;
Result
formula rez
-------------------- ---
P(10)+M(5)+M(3)+P(1) 3
P(7)+M(3)+M(4) 0
Fiddle to see everything in action with intermediate steps.

How to use regular expression in snowflake?

Rule:
1. Start with R ;
2. One or more number ;
3. One space ;
4. Follow with other characters ;
Test case:
Input : 'R1 ABC' 'R4 DEF' 'Randwick Acca' 'R11 PPP'
Expect Output : 'R1 ABC' 'R4 DEF' 'R11 PPP'
Regular expression : "R\d{1,} "
I use regular expression tester, it works.
https://regex-golang.appspot.com/assets/html/index.html?_sm_au_=iHVPMjQb0QjFkMTHfLJ4vK7214sJW
Test query:
WITH tbl
AS (select t.column1 mycol from values('R1 ABC'),('R4 DEF'),('Randwick Acca'),('R11 PPP') t)
SELECT *
FROM tbl
WHERE mycol regexp 'R\d{1,} ' ;
Return NULL .
Thanks,
Bin
1) where's the "any other character"? Because what you have ends with space, period
2) welcome to SQL. \ is a special character and needs to be escaped
So:
WHERE mycol regexp 'R\\d{1,} .*';
I tested it on your query and it seemed to work

Find first appearance of a character in a set of possible characters in a string in SQL Server 2012

I'm aware of the SQL Server CHARINDEX function which returns the position of a character (or sub-string) within another string. Still, I did not find any evident that there is support for regular expressions (unless I develop my own UDF).
What I'm looking for is the ability to find the first position of any character in a set within a string.
Example:
DECLARE #_Source_String NVARCHAR(100) = 'This is "MY" string \ and here is more text' ;
SELECT <some function> (#_Source_String,'"\') ;
This should return 9 because " appears before \. On the other hand:
SELECT <some function> (#_Source_String,'x\') ;
should return 21 because \ is before x.
I should add that performance is very important since this function/mechanism will be invoked with very high frequency.
Pattern matching capabilities in TSQL are pretty basic and often you would require CLR and regular expressions.
You can do this requirement with PATINDEX though. A list of characters in square brackets denotes a set of characters to match.
DECLARE #_Source_String NVARCHAR(100) = 'This is "MY" string \ and here is more text';
SELECT PATINDEX('%["\]%', #_Source_String),
PATINDEX('%[x\]%', #_Source_String);
Returns
+------------------+------------------+
| (No column name) | (No column name) |
+------------------+------------------+
| 9 | 21 |
+------------------+------------------+

Find RegExp and Replace specific character

I need to find a numeric value + quotes within a string (example 5"). Once found, I need to replace the quote with "pound". Unfortunately, the string is complex:
Sample string
Work and run with Joe "The King" Mel using a 3" vest with "Dog-Bone" weights.
I have tried
SELECT REPLACE(REPLACE('Work and run with Joe "The King" Mel using a 3" vest with "Dog-Bone" weights.', '%[^0-9]"%', 'pound'), '"', 'pound')
But it replaces all the quotes with 'pound'.
This will find the first occurrence of a number followed by a " and replace " with pound.
declare #s varchar(100)
set #s = 'Work and run with Joe "The King" Mel using a 3" vest with "Dog-Bone" weights.'
select stuff(#s, patindex('%[0-9]"%', #s)+1, 1, ' pound')
STUFF
PATINDEX
If you have more than one you can put it in a while loop.
while patindex('%[0-9]"%', #s) > 0
begin
set #s = stuff(#s, patindex('%[0-9]"%', #s)+1, 1, ' pound')
end
If you can't express a deterministic algorithm for the change you want to make, you'll never be able to write a regular expression as an implementation.
You'd written two replace expressions: one replaces only quotes after numbers, but the other replaces all the quotes, period.
If you can use a CLR: http://msdn.microsoft.com/en-us/magazine/cc163473.aspx

Resources