In a Knex query I'm getting that a column is undefined and I don't know how to resolve
the query I have in Knex
this.builder.orderBy(
this.tx(messageTable)
.max(messageColumns.createdAt)
.where(messageColumns.conversationId, 'conversation.id')
, direction)
The undefined happen on the last part of the .where(messageColumns.conversationId, 'conversation.id')
the SQL I want to get it to work with Knex is as follow
SELECT
*
FROM
"conversation"
ORDER BY
(
SELECT
max("created_at")
FROM
"message"
WHERE
"conversation_id" = conversation.id)
DESC
The query is wrong , try to do this:
select c.* from conversation c
join (
select conversation_id, max("created_at") created_at
from message
group by conversation_id
) m on c.conversation_id = m.conversation_id
order by m.created_at DESC
Related
I am using the Nothwind data base & working in pgAdmin, and my query is looking like this at the moment
SELECT
TO_CHAR (o.ShippedDate, 'yyyy.MM') AS Month
,o.OrderID
,Total
,SUM (Total) OVER PARTITION BY TO_CHAR (ShippedDate,
‘yyyy.MM’) ORDER BY O.OrderID) AS Running_Total
FROM public.orders O
INNER JOIN (
SELECT OrderID, SUM(Quantity*UnitPrice) AS Total
FROM public.order_details
GROUP BY OrderID
ORDER BY OrderID
) OD ON O.OrderID = OD.OrderID
WHERE
TO_CHAR (o.ShippedDate, 'yyyy.MM') IS NOT NULL
And is is not working, it says:
ERROR: column "o.shippeddate" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: TO_CHAR (o.ShippedDate, 'yyyy.MM') AS Month
Can you help me out what could be the issue? Thanks!
I fixed the query, so it is now the correct one.
I'm trying to run the following query with postgres's support for LATERAL subqueries:
with s as
(
select
tagValues ->> 'mode' as metric
, array_agg(id) as ids
from
metric_v3.v_series
where
name = 'node_cpu'
group by 1
)
select
t.starttime
, s.metric
, t.max
from
s, lateral (
select
d.starttime
, max(d.max) as max
from
metric_v3.gaugedata d
where
d.starttime >= '2020-01-17T00:00Z' AND d.starttime < '2020-01-24T00:00Z'
and d.seriesid in s.ids
group by 1
) t
order by 1,2;
It fails with, where s relates to the reference in the lateral subquery's where clause.
SQL Error [42601]: ERROR: syntax error at or near "s"
I have tried different methods for the lateral query, but I always get the same error. I don't know what I'm missing.
If I run the CTE expression and select s.* from it, I get the expected results, so that part is working fine.
I'm running Postgres 11.6 on CentOS.
You can't use IN with an array. You need to use the ANY operator:
and d.seriesid = any(s.ids)
I am currently using Hive and I have a table with the fields user_id and value. I want to order the values in descending order within each user_id and then only emit the top 100 records for each user_id. This is the code I am attempting to use:
DROP TABLE IF EXISTS mytable2
CREATE TABLE mytable2 AS
SELECT * FROM
(SELECT *, rank (user_id) as rank
FROM
(SELECT * from mytable
DISTRIBUTE BY user_id
SORT BY user_id, value DESC)a )b
WHERE rank<101
ORDER BY rank;
However when I run this query, I get the following error:
Error while compiling statement: FAILED: SemanticException [Error 10247]: Missing over clause for function : rank [ERROR_STATUS]
FYI - My UserIds are alpha-numeric.
Can anyone help?
Thanks in advance.
Add comment
As the error message says, you have error using the rank function,
try to add over after rank as following:
....
(SELECT *, rank (user_id) over (order by user_id) as rank
....
for further information how to use the rank function you could refer to this documentation
I've written a simple query that uses a WITH clause, but I'm getting this error:
Error : ERROR: missing FROM-clause entry for table "cte"
Here's the query, where I'm clearly putting a FROM clause. I know this must be simple but I'm just not seeing what I've done wrong. Thanks.
WITH cte AS (
SELECT cident, "month"
FROM orders_extended io
WHERE io.ident = 1 -- 1 will be replaced with a function parameter
)
SELECT *
FROM orders_extended o
WHERE o.cident = cte.cident AND o."month" = cte."month"
ORDER BY o."month" DESC, o.cname
The message didn't lie.
WITH cte AS (
SELECT cident, "month"
FROM orders_extended io
WHERE io.ident = 1 -- 1 will be replaced with a function parameter
)
SELECT o.*
FROM orders_extended o
INNER JOIN cte ON (o.cident = cte.cident and o."month" = cte."month")
ORDER BY o."month" DESC, o.cname
I have a nested select query that is returning the proper amount of rows. The query builds a recordset and compares it to a table and returns the records in the query that are not in the table.
I converted the select query to an update query. I am trying to populate the table with the rows returned from the query. When I run the update query it is returning with zero rows to update. I dont understand why because the select query is returning record and I am using the same code in the update query.
Thanks
Select Query: (This is returning several records)
Select *
From
(SELECT DISTINCT
ProductClass,SalProductClass.[Description],B.Branch,B.BranchDesc,B.Salesperson,B.Name,
CAST(0 AS FLOAT) AS Rate,'N' AS Split
FROM (SELECT SalBranch.Branch,SalBranch.[Description] AS BranchDesc,A.Salesperson,A.Name
FROM (SELECT DISTINCT
Salesperson,Name
FROM SalSalesperson
) A
CROSS JOIN SalBranch
) B
CROSS JOIN SalProductClass
) C
Left Outer Join RateComm On
RateComm.ProductClass = C.ProductClass and
RateComm.Branch = C.Branch And RateComm.Salesperson = C.Salesperson
Where RateComm.ProductClass is Null
Update Query: (This is returning zero records)
UPDATE RateComm
SET RateComm.ProductClass=C.ProductClass,RateComm.ProdClassDesc=C.ProdClassDesc,
RateComm.Branch=C.Branch,RateComm.BranchDesc=C.BranchDesc,RateComm.Salesperson=C.Salesperson,
RateComm.Name=C.Name,RateComm.Rate=C.Rate,RateComm.Split=C.Split
FROM (SELECT DISTINCT
ProductClass,SalProductClass.[Description] AS ProdClassDesc,B.Branch,B.BranchDesc,B.Salesperson,B.Name,
CAST(0 AS FLOAT) AS Rate,'N' AS Split
FROM (SELECT SalBranch.Branch,SalBranch.[Description] AS BranchDesc,A.Salesperson,A.Name
FROM (SELECT DISTINCT
Salesperson,Name
FROM SalSalesperson
) A
CROSS JOIN SalBranch
) B
CROSS JOIN SalProductClass
) C
LEFT OUTER JOIN RateComm ON C.ProductClass=RateComm.ProductClass AND
C.Salesperson=RateComm.Salesperson AND C.Branch=RateComm.Branch
WHERE RateComm.ProductClass IS NULL
It's difficult to update what doesn't exist. Have you tried an INSERT query instead?