I have written the below query to retrieve movie and corresponding rating:
match (rvwr:Person)-[r:REVIEWED]->(m:Movie)
where m.released > 2003
return m.title, sum(r.rating)/count(r) as rating
I want to confirm if there is a need to use anything similar to groupby clause.
No such 'group by' clause in neo4j cypher. Sum is an aggregate function and m.title is not. So cypher will use title as a group by key. Below is the documentation about it.
https://neo4j.com/docs/cypher-manual/current/functions/aggregating/
Related
Requested behaviour:
I would like to create a search form in Google Sheets to query a table that I use as a database.
The user should be able to query the table by multiple searches
categories which the user can type into a cell of the sheet.
If the user does not type in a search string, then all items
should be displayed.
Also, the user should be able to choose between an
"including OR" and an "excluding AND" search.
The original table is on a different sheet than the search form. The final searchform should have more than 10 searchable categories.
Current State
Since the original table is in a different sheet than the search form, my idea has been to import the table by a dynamic query() function.
I created two input search input fields and a field where the user can switch between "OR" and "AND". I also created a query function that connects these 3 search terms.
The change between "OR" and "AND" search works (with the first approach).
First approach:
=QUERY('Geschäftsvorfälle'!A2:AG1000, "select * WHERE A="&B4&" "&D1&" B='"&B5&"'")
Second approach:
=QUERY('Geschäftsvorfälle'!A2:AG1000, "select * " &if(B5="Alle",, "WHERE B='"&B5&"'") &if(B4="",, "WHERE A="&B4&""))
Issue
The first approach works with the "OR" search but gives an empty sheet back if I use multiple search terms. It also throws a "VALUE" error if leave one search term blank. The second approach throws a "VALUE" error if I use multiple search terms even there should be matching rows.
Is there a way to make this kind of a searchform work in Google Sheets? If yes is it possible to do it with query() and how do I do it? Could you provide some example screenshots or code?
Screenshots
The searchform:
The combining query:
try:
=QUERY('Geschäftsvorfälle'!A2:AG,
"where "&TEXTJOIN(" "&D1&" ", 1,
IF(B4<>"", " A="&B4, ),
IF(B5<>"", " B='"&B5&"'", ),
IF(B6<>"", " lower(F) contains '"&LOWER(B6)&"'", )), 1)
Basicly I just want to say "give me all the values from column Y" and not "select all values and filter for Y based on primary key Z"
The word "column" is only mentioned one time on the Django documentation page and the "primary key" and "row" options are very clearly explained in my eyes.
Now I wonder, how do I do this? :)
If I understand well, you want to query values for only a column, I think this is what you need:
MyModel.objects.values('column_name')
Or if you want to get a list of values:
MyModel.objects.values_list('column_name', flat=True)
You can apply any filter before selecting values:
MyModel.objects.filter(**criteria**).values_list('column_name', flat=True)
In CFWheels I am looking at the findAll() page and it has a group by option by doesn't have a Having Clause option. Is there a way to use having clause using findAll() in CFWheels.
Just to share. Credit to Pankaj in the comment for his answer. Thank you
checklist = model("user_checklist").findAll(select="MAX(user_checklist.r_id)", group="r_id HAVING MAX(user_checklist.r_id) > 13");
gives you
SELECT MAX(user_checklist.r_id) FROM user_checklist GROUP BY r_id HAVING MAX(user_checklist.r_id) > 13
It is very disappointing that the dynamic filtering of the grouped data is not available even in the new release CFWheels 1.4.2.
What I found is this issue, it is a very old issue posted under the cfwheels google group and a google group discussion. Even to this day the workaround for using a having in the group by statement is to use the cfrel by dumphreys which is a ColdFusion Relational Algebra Framework.
I would recommend trying it, it is very easy to use and cleanly written. If you navigate to cfrel.cfc you would find a findAll() function which looks similar to the original findAll() in cfwheels (check out the \wheels\model\read.cfm), but there you'll find it supports having() right out of the box.
Example (cfrel having clause):
/*
SQL: SELECT productId, SUM(total) AS totalSum FROM orders
GROUP BY productId HAVING SUM(total) > ?
ORDER BY totalSum DESC LIMIT 5
PARAMS: [1000]
*/
myOrdersRel = relation(datasource="cfrel")
.select("productId,SUM(total) AS totalSum")
.from("orders")
.group("productId")
.having("SUM(total) > ?", [1000])
.order("totalSum DESC")
.limit(5);
query2 = rel2.query();
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production.
I have a table in the below format.
Name Department
Johny Dep1
Jacky Dep2
Ramu Dep1
I need an output in the below format.
Dep1 - Johny,Ramu
Dep2 - Jacky
I have tried the 'LISTAGG' function, but there is a hard limit of 4000 characters. Since my db table is huge, this cannot be used in the app. The other option is to use the
SELECT CAST(COLLECT(Name)
But my framework allows me to execute only select queries and no PL/SQL scripts.Hence i dont find any way to create a type using "CREATE TYPE" command which is required for the COLLECT command.
Is there any alternate way to achieve the above result using select query ?
You should add GetClobVal and also need to rtrim as it will return delimiter in the end of the results.
SELECT RTRIM(XMLAGG(XMLELEMENT(E,colname,',').EXTRACT('//text()')
ORDER BY colname).GetClobVal(),',') from tablename;
if you cant create types (you can't just use sql*plus to create on as a one off?), but you're OK with COLLECT, then use a built-in array. There's several knocking around in the RDBMS. run this query:
select owner, type_name, coll_type, elem_type_name, upper_bound, length
from all_coll_types
where elem_type_name = 'VARCHAR2';
e.g. on my db, I can use sys.DBMSOUTPUT_LINESARRAY which is a varray of considerable size.
select department,
cast(collect(name) as sys.DBMSOUTPUT_LINESARRAY)
from emp
group by department;
A derivative of #anuu_online but handle unescaping the XML in the result.
dbms_xmlgen.convert(xmlagg(xmlelement(E, name||',')).extract('//text()').getclobval(),1)
For IBM DB2, Casting the result to a varchar(10000) will give more than 4000.
select column1, listagg(CAST(column2 AS VARCHAR(10000)), x'0A') AS "Concat column"...
I end up in another approach using the XMLAGG function which doesn't have the hard limit of 4000.
select department,
XMLAGG(XMLELEMENT(E,name||',')).EXTRACT('//text()')
from emp
group by department;
You can use:
SELECT department
, REGEXP_REPLACE(XMLCAST(XMLAGG(XMLELEMENT(x, name, ',')) AS CLOB), ',$')
FROM emp
GROUP BY department
it will return CLOB that has no size limit, handles correctly XML entity escapes and separators.
Instead of REGEXP_REPLACE(..., ',$')) you can use RTRIM(..., ','), which should be faster, but will remove all separators from the end of the result (including those that can appear in name at the end, or previous ones if last names are empty).
I have a table with one column having a large json object in the format below. The column datatype is VARCHAR
column1
--------
{"key":"value",....}
I'm interested in the first value of the column data
in regex I can do it by .*?:(.*),.* with group(1) giving me the value
How can i use it in the select query
Don't do that, it's bad database design. Shred the keys and values to their own table as columns, or use the XML data type. XML would work fine because you can index the structure well, and you can use XPATH queries on the data. XPATH supports regexp natively.
You can use regular expression with xQuery, you just need to call the function matches from a SQL query or a FLORW query.
This is an example of how to use regular expressions from SQL:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
http://angocadb2.blogspot.fr/2014/04/regular-expressions-in-db2.html
DB2 doesn't have any built in regex functionality, unfortunately. I did find an article about how to add this with libraries:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
Without regexes, this operation would be a mess. You could make a function that goes through the string character by character to find the first value. Or, if you will need to do more than this one operation, you could make a procedure that parses the json and throws it into a table of keys/values. Neither one sounds fun, though.
In DB2 for z/OS you will have to pass the variable to XMLQUERY with the PASSING option
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($TEXT,''^[A-Za-z 0-9]*$'')'
PASSING t.text as "TEXT") as integer) = 0
)
select * from val"