Problem with a query using GROUP BY in PostgreSQL - postgresql

I'm using the query tool in PGAdmin 4.20 and trying the following query:
select * from metadatavalue group by resource_id order by resource_id;
And I'm getting the following:
ERROR: column "metadatavalue.metadata_value_id" must appear in the
GROUP BY clause or be used in an aggregate function LINE 3: select *
from metadatavalue group by resource_id order by re...
^ SQL state: 42803 Character: 176
The thing is that in another table, I use the same syntax and works:
select * from metadatafieldregistry group by metadata_field_id order by metadata_field_id;
Also, I'm not getting all the entries from a same resource_id, only a few. Could these two problems be related?
Please, help!
Thank you in advance.

Related

The sqliite db query is not working in postgresql db

i am having a query which is working correctly in SQLite. but its giving error in PostgreSQL.
SELECT decks.id, decks.name, count(cards.id)
from decks
JOIN cards ON decks.id = cards.did
GROUP BY cards.did
above query is giving error in postgresql.
ERROR: column "decks.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT decks.id, decks.name, count(cards.id) FROM decks JOIN...
You can't have columns in the SELECT list, that are not used in an aggregate function or part of the GROUP BY. The fact that SQLite accepts this, is a bug in SQLite. The fact that Postgres rejects this, is correct.
You need to rewrite your query to:
SELECT decks.id, decks.name, count(cards.id)
from decks
JOIN cards ON decks.id = cards.did
GROUP BY decks.id, decks.name;
If decks.id is the primary key, you can shorten the grouping to GROUP BY decks.id

selecting a distinct column with alias table not working in postgres

SELECT pl_id,
distinct ON (store.store_ID),
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID;
while running this query in postgres server i am getting the below error..How to use the distinct clause..in above code plan 1 is the schema name.
ERROR: syntax error at or near "distinct" LINE 2: distinct ON
(store.store_ID),
You are missing an order by where the first set of rows should be the ones specified in the distinct on clause. Also, the distinct on clause should be at start of the selection list.
Try this:
SELECT distinct ON (store_ID) store.store_ID, pl_id,
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID
order by store_ID, pl_id;

JPQL #Query causing "unexpected token: ("

I have following method defined:
#Query("SELECT AVG(total) FROM (SELECT COUNT(t.name) total FROM DataTable t GROUP BY DATE(actiontime)) result")
Long countAvg();
However it causes this error:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 24 [SELECT AVG(total) FROM (SELECT COUNT(t.name) total FROM backend.DataTable t GROUP BY DATE(actiontime)) result]
But following SQL works fine:
SELECT AVG(total) FROM (SELECT COUNT(NAME) total FROM DATA_TABLE GROUP BY DATE(actiontime)) result
If i understand correctly, JPQL has problems with subquery. How should i create this kind of query then?
I don't think jpql supports subselect with from clause. As per jpa docs
Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.
You can use nativeQuery = true in the #Query annotation and run it as native query instead or rewrite the query if possible.

Proper GROUP BY syntax

I'm fairly proficient in mySQL and MSSQL, but I'm just getting started with postgres. I'm sure this is a simple issue, so to be brief:
SQL error:
ERROR: column "incidents.open_date" must appear in the GROUP BY clause or be used in an aggregate function
In statement:
SELECT date(open_date), COUNT(*)
FROM incidents
GROUP BY 1
ORDER BY open_date
The type for open_date is timestamp with time zone, and I get the same results if I use GROUP BY date(open_date).
I've tried going over the postgres docs and some examples online, but everything seems to indicate that this should be valid.
The problem is with the unadorned open_date in the ORDER BY clause.
This should do it:
SELECT date(open_date), COUNT(*)
FROM incidents
GROUP BY date(open_date)
ORDER BY date(open_date);
This would also work (though I prefer not to use integers to refer to columns for maintenance reasons):
SELECT date(open_date), COUNT(*)
FROM incidents
GROUP BY 1
ORDER BY 1;
"open_date" is not in your select list, "date(open_date)" is.
Either of these will work:
order by date(open_date)
order by 1
You can also name your columns in the select statement, and then refer to that alias:
select date(open_date) "alias" ... order by alias
Some databases require the keyword, AS, before the alias in your select.

oracle not a group by expression

I have a table with 3 columns.
ORDER CATEGORY
NAV_PER_SHARE
Number_OUTSTANDING_SHARES
Now:
SELECT ORDER_CATEGORY, SUM(OUTSTANDING_SHARES) GROUP BY ORDER_CATEGORY , it runs fine
But:
SELECT ORDER_CATEGORY, NAV_PER_SHARE * SUM(OUTSTANDING_SHARES) GROUP BY ORDER_CATEGORY , it says : Not a group by expression .
What am I missing?
You caNnot group because NAV_PER_SHARE is given per result. Did you mean
SUM(NAV_PER_SHARE*OUTSTANDING_SHARES)
?
From what it looks like, the solution would be to use a subquery.
SELECT ORDER_CATEGORY, NAV_PER_SHARE * SUM_OF_OS
FROM (SELECT ORDER_CATEGORY, SUM(OUTSTANDING_SHARES) SUM_OF_OS
GROUP BY ORDER_CATEGORY);
Although, I'm not sure how your one query works without a FROM keyword.