Having Clause in OrientDB - orientdb

I am using OrientDB 2.1.4. Is the HAVING Clause now available with GROUP BY? Below is my query, can it be further reduced?
select CardName, CardCount from (select CardName, sum(Count) as CardCount from (select inV().Name as CardName, outV().ActiveSessionCount as Count from RealizedBy) group by CardName) where CardCount > 1
The schema export command gives the output as mentioned below:
{"info":{"name":"Smarts","default-cluster-id":3,"exporter-version":11,"engine-version":"2.1.4","engine-build":"UNKNOWN#r${buildNumber}; 2015-10-13 12:23:58+0000","storage-config-version":14,"schema-version":4,"mvrbtree-version":3,"schemaRecordId":"#0:1","indexMgrRecordId":"#0:2"},"clusters":[{"name":"internal","id":0},{"name":"index","id":1},{"name":"manindex","id":2},{"name":"default","id":3},{"name":"orole","id":4},{"name":"ouser","id":5},{"name":"ofunction","id":6},{"name":"oschedule","id":7},{"name":"orids","id":8},{"name":"v","id":9},{"name":"e","id":10},{"name":"_studio","id":11},{"name":"device","id":12},{"name":"card","id":13},{"name":"port","id":14},{"name":"vp","id":15},{"name":"configuredon","id":16},{"name":"realizedby","id":17},{"name":"connectedto","id":18},{"name":"composedof","id":19}],"schema":{"version":2,"classes":[{"name":"Card","default-cluster-id":13,"cluster-ids":[13],"oversize":0.0,"super-classes":["V"],"cluster-selection":"round-robin","properties":[{"name":"ActiveSessionCount","type":"INTEGER","collate":"default"},{"name":"Name","type":"STRING","collate":"default"},{"name":"TotalSessionCount","type":"INTEGER","collate":"default"}]},{"name":"Composedof","default-cluster-id":19,"cluster-ids":[19],"super-classes":["E"],"cluster-selection":"round-robin"},{"name":"ConfiguredOn","default-cluster-id":16,"cluster-ids":[16],"super-classes":["E"],"cluster-selection":"round-robin"},{"name":"ConnectedTo","default-cluster-id":18,"cluster-ids":[18],"super-classes":["E"],"cluster-selection":"round-robin"},{"name":"Device","default-cluster-id":12,"cluster-ids":[12],"oversize":0.0,"super-classes":["V"],"cluster-selection":"round-robin","properties":[{"name":"ActiveSessionCount","type":"INTEGER","collate":"default"},{"name":"Name","type":"STRING","collate":"default"},{"name":"TotalSessionCount","type":"INTEGER","collate":"default"},{"name":"Type","type":"STRING","collate":"default"}]},{"name":"E","default-cluster-id":10,"cluster-ids":[10],"cluster-selection":"round-robin"},{"name":"OFunction","default-cluster-id":6,"cluster-ids":[6],"cluster-selection":"round-robin","properties":[{"name":"code","type":"STRING","collate":"default"},{"name":"idempotent","type":"BOOLEAN","collate":"default"},{"name":"language","type":"STRING","collate":"default"},{"name":"name","type":"STRING","collate":"default"},{"name":"parameters","type":"EMBEDDEDLIST","linked-type":"STRING","collate":"default"}]},{"name":"OIdentity","default-cluster-id":-1,"cluster-ids":[-1],"abstract":true,"cluster-selection":"round-robin"},{"name":"ORIDs","default-cluster-id":8,"cluster-ids":[8],"cluster-selection":"round-robin"},{"name":"ORestricted","default-cluster-id":-1,"cluster-ids":[-1],"abstract":true,"cluster-selection":"round-robin","properties":[{"name":"_allow","type":"LINKSET","linked-class":"OIdentity","collate":"default"},{"name":"_allowDelete","type":"LINKSET","linked-class":"OIdentity","collate":"default"},{"name":"_allowRead","type":"LINKSET","linked-class":"OIdentity","collate":"default"},{"name":"_allowUpdate","type":"LINKSET","linked-class":"OIdentity","collate":"default"}]},{"name":"ORole","default-cluster-id":4,"cluster-ids":[4],"super-classes":["OIdentity"],"cluster-selection":"round-robin","properties":[{"name":"inheritedRole","type":"LINK","linked-class":"ORole","collate":"default"},{"name":"mode","type":"BYTE","collate":"default"},{"name":"name","type":"STRING","mandatory":true,"not-null":true,"collate":"ci"},{"name":"rules","type":"EMBEDDEDMAP","linked-type":"BYTE","collate":"default"}]},{"name":"OSchedule","default-cluster-id":7,"cluster-ids":[7],"cluster-selection":"round-robin","properties":[{"name":"arguments","type":"EMBEDDEDMAP","collate":"default"},{"name":"function","type":"LINK","mandatory":true,"not-null":true,"linked-class":"OFunction","collate":"default"},{"name":"name","type":"STRING","mandatory":true,"not-null":true,"collate":"default"},{"name":"rule","type":"STRING","mandatory":true,"not-null":true,"collate":"default"},{"name":"start","type":"BOOLEAN","collate":"default"},{"name":"starttime","type":"DATETIME","collate":"default"},{"name":"status","type":"STRING","collate":"default"}]},{"name":"OTriggered","default-cluster-id":-1,"cluster-ids":[-1],"abstract":true,"cluster-selection":"round-robin"},{"name":"OUser","default-cluster-id":5,"cluster-ids":[5],"super-classes":["OIdentity"],"cluster-selection":"round-robin","properties":[{"name":"name","type":"STRING","mandatory":true,"not-null":true,"collate":"ci"},{"name":"password","type":"STRING","mandatory":true,"not-null":true,"collate":"default"},{"name":"roles","type":"LINKSET","linked-class":"ORole","collate":"default"},{"name":"status","type":"STRING","mandatory":true,"not-null":true,"collate":"default"}]},{"name":"Port","default-cluster-id":14,"cluster-ids":[14],"oversize":0.0,"super-classes":["V"],"cluster-selection":"round-robin","properties":[{"name":"ActiveSessionCount","type":"INTEGER","collate":"default"},{"name":"Name","type":"STRING","collate":"default"},{"name":"TotalSessionCount","type":"INTEGER","collate":"default"}]},{"name":"RealizedBy","default-cluster-id":17,"cluster-ids":[17],"super-classes":["E"],"cluster-selection":"round-robin"},{"name":"V","default-cluster-id":9,"cluster-ids":[9],"oversize":2.0,"cluster-selection":"round-robin"},{"name":"VP","default-cluster-id":15,"cluster-ids":[15],"oversize":0.0,"super-classes":["V"],"cluster-selection":"round-robin","properties":[{"name":"ActiveSessionCount","type":"INTEGER","collate":"default"},{"name":"Name","type":"STRING","collate":"default"},{"name":"TotalSessionCount","type":"INTEGER","collate":"default"}]},{"name":"_studio","default-cluster-id":11,"cluster-ids":[11],"cluster-selection":"round-robin"}]},"indexes":[{"name":"dictionary","type":"DICTIONARY","algorithm":"SBTREE","definition":{"defClass":"com.orientechnologies.orient.core.index.OSimpleKeyIndexDefinition","stream":{"#type":"d","#version":0,"keyTypes":["STRING"],"collate":"default","nullValuesIgnored":true}},"metadata":{"#type":"d","#version":0,"durableInNonTxMode":true,"trackMode":"FULL"}},{"name":"Device.Name","type":"UNIQUE","algorithm":"SBTREE","clustersToIndex":["device"],"definition":{"defClass":"com.orientechnologies.orient.core.index.OPropertyIndexDefinition","stream":{"#type":"d","#version":0,"className":"Device","field":"Name","keyType":"STRING","collate":"default","nullValuesIgnored":true}}},{"name":"OUser.name","type":"UNIQUE","algorithm":"SBTREE","clustersToIndex":["ouser"],"definition":{"defClass":"com.orientechnologies.orient.core.index.OPropertyIndexDefinition","stream":{"#type":"d","#version":0,"className":"OUser","field":"name","keyType":"STRING","collate":"ci","nullValuesIgnored":true}}},{"name":"ORole.name","type":"UNIQUE","algorithm":"SBTREE","clustersToIndex":["orole"],"definition":{"defClass":"com.orientechnologies.orient.core.index.OPropertyIndexDefinition","stream":{"#type":"d","#version":0,"className":"ORole","field":"name","keyType":"STRING","collate":"ci","nullValuesIgnored":true}}},{"name":"Card.Name","type":"UNIQUE","algorithm":"SBTREE","clustersToIndex":["card"],"definition":{"defClass":"com.orientechnologies.orient.core.index.OPropertyIndexDefinition","stream":{"#type":"d","#version":0,"className":"Card","field":"Name","keyType":"STRING","collate":"default","nullValuesIgnored":true}}},{"name":"VP.Name","type":"UNIQUE","algorithm":"SBTREE","clustersToIndex":["vp"],"definition":{"defClass":"com.orientechnologies.orient.core.index.OPropertyIndexDefinition","stream":{"#type":"d","#version":0,"className":"VP","field":"Name","keyType":"STRING","collate":"default","nullValuesIgnored":true}}},{"name":"Port.Name","type":"UNIQUE","algorithm":"SBTREE","clustersToIndex":["port"],"definition":{"defClass":"com.orientechnologies.orient.core.index.OPropertyIndexDefinition","stream":{"#type":"d","#version":0,"className":"Port","field":"Name","keyType":"STRING","collate":"default","nullValuesIgnored":true}}}],"manualIndexes":[{"name":"dictionary","content":[]}]}

No, current OrientDB release does not support HAVING clause.
Probably it will be introduced in next releases, but it's not high priority in the roadmap

Related

Postgres SQL query group by get most recent record instead of an aggregate

This is a current postgres query I have:
sql = """
SELECT
vms.campaign_id,
avg(vms.open_rate_uplift) as open_rate_average,
avg(vms.click_rate_uplift) as click_rate_average,
avg(vms.conversion_rate_uplift) as conversion_rate_average,
avg(cms.incremental_opens),
avg(cms.incremental_clicks),
avg(cms.incremental_conversions)
FROM
experiments.variant_metric_snapshot vms
INNER JOIN experiments.campaign_metric_snapshot cms ON vms.campaign_id = cms.campaign_id
WHERE
vms.campaign_id IN %(campaign_ids)s
GROUP BY
vms.campaign_id
"""
whereby I get the average incremental_opens, incremental_clicks, and incremental_conversions per campaign group from the cms table. However, instead of the average, I want the most recent values for the 3 fields. See the cms table screenshot below - I want the values from the record with the greatest (i.e. most recent) event_id (instead of an average for all records) for a given group).
How can I do this? Thanks
It sounds like you want a lateral join.
FROM
experiments.variant_metric_snapshot vms
CROSS JOIN LATERAL (select * from experiments.campaign_metric_snapshot cms where vms.campaign_id = cms.campaign_id order by event_id desc LIMIT 1) cms
WHERE...
If you are after a quick and dirty solution you can use array_agg function with minimal change to your query.
SELECT
vms.campaign_id,
avg(vms.open_rate_uplift) as open_rate_average,
avg(vms.click_rate_uplift) as click_rate_average,
avg(vms.conversion_rate_uplift) as conversion_rate_average,
(array_agg(cms.incremental_opens ORDER BY cms.event_id DESC))[1] AS incremental_opens,
..
FROM
experiments.variant_metric_snapshot vms
INNER JOIN experiments.campaign_metric_snapshot cms ON vms.campaign_id = cms.campaign_id
WHERE
vms.campaign_id IN %(campaign_ids)s
GROUP BY
vms.campaign_id;

select count(*) in WHERE Clause needing a comparison operator

Kinda new to SQL so I was reading up on some queries and chanced upon this (https://iggyfernandez.wordpress.com/2011/12/04/day-4-the-twelve-days-of-sql-there-way-you-write-your-query-matters/)
The part that got me curious is the aggregate query in the WHERE Clause. This is probably my misunderstanding but how does the author's code (shown below) run? I presumed that Count(*) - or rather aggregate functions cannot be used in the WHERE clause and you need a HAVING for that ?
SELECT per.empid, per.lname
FROM personnel per
WHERE (SELECT count(*) FROM payroll pay WHERE pay.empid = per.empid AND pay.salary = 199170) > 0;
My second question would be why the comparison operator (>0) is needed ? I was playing around and noticed that it would not run in PostgreSQL without the >0; also reformatting it to have a HAVING by Clause massively improves the query execution time
SELECT per.empid, per.lname
FROM personnel per
WHERE EXISTS (SELECT per.empid FROM payroll pay WHERE pay.empid = per.empid AND pay.salary = 199170)
GROUP BY per.empid, per.lname
HAVING COUNT(*) > 0;
Omit the GROUP BY and HAVING clauses in your version, then your query will be a more efficient query that is equivalent to the original.
In the original query, count(*) appears in the SELECT list of a subquery. You can use a parenthesized subquery almost anywhere in an SQL statement.

subquery instead of having clause using Slick

How can I express the following subquery in Slick without a HAVING clause?:
select team, min_count
from (
select team, count(min) as min_count
from table
group by team
) t
where count > 500
Following the documented approach, groupBy followed by filter and map will cause HAVING:
select team, count(min) as min_count
from table
group by team
HAVING count(min) > 500
The reason for preferring a subquery is to avoid repeating the aggregate outside the select.

Creating a pagination with db2

I have read that DB2 doesn't support Limit and Offset. I also read that you have to uses ROW_NUMBER() and subqueries to get the desired result.
If this is an SQL query:
$sql = "SELECT * FROM ITEMS LIMIT $offset, $rowsperpage";
where $offset is the offset and $rowsperpage is the amount of rows from database I want to be displayed on the page, what could be equivalent of this as a DB2 query.
Well, depending on what platform of DB2 you are using, you didn't read the full story. DB2 LUW has support for LIMIT and OFFSET, but you have to turn it on (don't forget to restart DB2 after setting the flag). If you want to use DB2 with ROW_NUMBER() as you asked for, you could write the query as follows:
SELECT *
FROM (SELECT ROW_NUMBER() OVER() AS rn,
items.*
FROM items)
WHERE rn BETWEEN computelowerboundaryhere AND computeupperboundaryhere;
There is also an overview article describing the different ways of doing the LIMIT/OFFSET work in DB2.
DB2 for I also has support for LIMIT and OFFSET as of the latest technology refresh (7.1 TR11 and 7.2 TR3).

Filtering sphinxql group by query by count

Does anybody know how filter sphinxql group by query by count?
For example I have query like:
SELECT collection_id, count(*) as cnt
FROM mobile_content
WHERE collection_id != 0
GROUP BY collection_id
And I want to take to result only rows that have cnt greater than 5. If I make something like this, I get an error:
SELECT collection_id, count(*)
FROM mobile_content
WHERE collection_id != 0 AND count(*) > 5
GROUP BY collection_id;
ERROR 1064 (42000): sphinxql: Aggregates in 'where' clause prohibited near '5 GROUP BY collection_id'
I remember that in older versions I used #count to filtering group by results.
My current sphinxsearch version is 2.1.5. Is it possible to filter results by count in this version?
My current sphinxsearch version is 2.1.5. Is it possible to filter results by count in this version?
No.
The HAVING clause, was added in 2.2.1-beta
http://sphinxsearch.com/docs/current.html#sphinxql-select