How to use "group by" query on AgensGraph? - agens-graph

I tried to use "group by" query on AgensGraph.
But, there is no "group by" clause on CYPHER.
How can I use "group by" on query.

CYPHER is distinct from SQL.
There is no explicit "GROUP BY" clause on grammar.
But, You can list extra column with aggregate function on return or with cluase.
# match (n:v) return n.x, sum(n.y);
x | sum
---+-----
1 | 4
2 | 9
(2 rows)

Cypher has no Group by clause, but with AgensGraph you can mix SQL and Cypher. You can write a Cypher-Query and insert it into another SQL-Query with group_by.
At the documentation is an example for such hybrid querys.
https://bitnine.net/documentations/developer-manual-en.html#hybrid-query

Related

SQL Server testing for 1 value in multiple columns

I am testing a table and would like to find out if 10 columns of that table (integer fields) EQUAL the value 999. can ANY or the IN clause be used for this?
At a pure guess, and this is pseudo-SQL, but
SELECT {Columns}
FROM {YourTable}
WHERE '999' IN ({First Column},{Second Column},{Third Column},...,{Tenth Column});
The only way to test this is something like this:
select * from table1 where i1=999 and i1=i2 and i2=i3 and i3=i4 and i4=i5 and i5=16 and i6=i7 and i7=i8 and i8=i9;
where iX are column names. This will return rows which match all the value for all 9 columns.
TSQL is a MS implementation of various SQL standards.

TSQL - Aggregates in HAVING Clause

I know this problem has been asked a lot but when I address the error message and use a HAVING clause, I am still receiving the dreaded:
An aggregate may not appear in the WHERE clause unless it is in a
subquery contained in a HAVING clause or a select list,
and the column being aggregated is an outer reference.
What am I doing wrong, here?
SELECT
mr.ClubKeyNumber,
COUNT(mr.MonthlyReportID),
SUM(CONVERT(int,mr.Submitted))
FROM MonthlyReport mr
WHERE mr.ReportYear = 2014
AND COUNT(mr.MonthlyReportID) = 12
GROUP BY mr.ClubKeyNumber
HAVING (SUM(CONVERT(int,mr.Submitted))) > 11
The problem isn't with your HAVING clause it's in your WHERE clause.
You have an aggregate count in your where clause, try this:
SELECT
mr.ClubKeyNumber,
COUNT(mr.MonthlyReportID),
SUM(CONVERT(int,mr.Submitted))
FROM MonthlyReport mr
WHERE mr.ReportYear = 2014
GROUP BY mr.ClubKeyNumber
HAVING (SUM(CONVERT(int,mr.Submitted))) > 11 and COUNT(mr.MonthlyReportID) = 12
The where clause checks each row being aggregated before the group by clause. It cannot count your MonthlyReportID until after the group by so move it to the having clause.
Here is a simple example you can play with to demonstrate where vs have.

Postgresql not using my index

I can't understand why my query is doing a sequential scan.
select columns
from table
where (column_1 ilike '%whatever%' or column_2 ilike '%whatever%')
I have an index on both column_1 and column_2.
The cardinality on both columns is very high.
My table is roughly 25 million rows.
What do you think I might be doing wrong? No matter what I do, it always does a sequential scan.
Edit #1:
My index looks like this:
Create index xxx on table (column_1, column_2);
Edit #2:
Changing my sql query to
select columns
from table
where (column_1 ilike 'whatever%' and column_2 ilike 'whatever%')
still made my query use a sequential scan. I got the same result when I just used like instead of ilike. But this query:
select columns
from table
where (column_1 = 'whatever' and column_2 = whatever)
made my query use an index scan and my query went much faster :)
Two Reasons:
Your query does a OR condition in which index can't be used.
You are doing a ilike on "%xyz%". This can't use any help of sorted(i.e. indexed) data.
--
Edit: See if you can have like on "xyz%". Then index can be used if you do a separate condition on both columns (and separate index on both)
Edit2: By the query, the thing you are trying to do looks like Full Text Search. For that you would need search indexing techniques (Read Elasticsearch, Sphinx, Solr)

Convert SQL columns to rows

I would like to split one records columns into multiple rows.
If I have a SQL statement like the following:
SELECT 1,2,3
Result:
1 | 2 | 3
How do I convert that to the following result?
1
2
3
I am currently using MS SQL 2008.
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
To sum up the comments above:
If you want to convert columns into rows, you will need to use the
T-SQL UNPIVOT clause.
If you want to split a single column that contains comma separated
values, you will need to create a Function (example here)

SQL Server where clause using In() vs Wildcard

Is there any performance difference between query A and query B?
Query A
SELECT * FROM SomeTable
WHERE 1 = 1 AND (SomeField LIKE '[1,m][6,e][n]%')
Query B
SELECT * FROM SomeTable
WHERE 1 = 1 AND (SomeField IN ('16', 'Mens'))
The first could be much slower. An index can't be used with LIKE unless there is a constant prefix, for example LIKE 'foo%'. The first query will therefore require a table scan. The second query however could use an index on SomeField if one is available.
The first query will also give the wrong results as it matches '1en'.

Resources