I'm trying to filter query with HAVING but all I get this error:
mysql> SELECT id FROM related_tags GROUP BY application_id HAVING COUNT(*)=10;
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT, expecting $end near 'HAVING COUNT(*)=10'
I'm using Sphinx 2.2.6-id64-release, it supports HAVING
This is my index, if it does matter (application_id attribute is for grouping by id).
sql_query = \
SELECT `id`, `id` as `application_id`, `clear_title`\
FROM `applications`\
WHERE `id`>=$start AND `id`<=$end
sql_query_range = SELECT MIN(id),MAX(id) FROM applications
sql_attr_uint = application_id
sql_attr_multi = uint tag_id from query; \
select application_id, tag_id \
from application_tag_stemmed2;
I think you have to make it a virtual attribute, arbitary expressions not allowed in the HAVING clause itself.
SELECT id,COUNT(*) AS cnt FROM related_tags GROUP BY application_id HAVING cnt=10;
Related
How do I translate this into ecto?
SELECT *
FROM mytable
WHERE (url,"when") IN
( SELECT url, MAX("when")
FROM mytable
GROUP BY url
)
it works fine in postgresql shell, but I get a Ecto.QueryError at runtime:
subquery must return a single field in order to be used on the right-side of `in` in query
from this code:
subq =
from(f in MyTable,
select: [f.url, max(f.when)],
group_by: f.url
)
from(s in MyTable,
where: [s.url, s.when] in subquery(subq)
)
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
How to convert below Oracle query to Postgres? Below is the error
ERROR: syntax error at or near "BY"¶ Position: 321
Query
SELECT listagg(app_rule_cd,',') within GROUP (
ORDER BY abc_cd) AS ERR_LST,
'1' AS JOIN1
FROM ABC_RULE
WHERE abc_cd IN
( WITH CTE AS
(SELECT VAL FROM config_server WHERE NAME = 'XXXXXXXXXX'
)
SELECT TRIM(REGEXP_SUBSTR( VAL, '[^,]+', 1, LEVEL))
FROM CTE
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(VAL, '[^,]+')) + 1 // BY is position 321
);
You did not explain what this query does, but the convoluted connect by level and regexp_replace() is a typically pattern to split a comma separated string into elements in Oracle.
That can be done way easier in Postgres:
SELECT string_agg(app_rule_cd,',' ORDER BY abc_cd) AS ERR_LST,
'1' AS JOIN1
FROM ABC_Rule
WHERE abc_cd = ANY ( (SELECT string_to_array(val, ',')
FROM config_server WHERE NAME = 'XXXXXXXXXX') )
Note the duplicated parentheses around the sub-query are necessary. Another way is to use the IN operator:
SELECT string_agg(app_rule_cd,',' ORDER BY abc_cd) AS ERR_LST,
'1' AS JOIN1
FROM ABC_Rule
WHERE abc_cd IN (SELECT unnest(string_to_array(val, ','))
FROM config_server
WHERE NAME = 'XXXXXXXXXX')
If there is one more UID in sessions than there is in users (obviously not supposed to be that way), then I expect to have a non-empty result set when I run the last select, but I get no rows returned - this result just doesn't make logical sense to me...
select count(distinct(uid)) from users;
> 108736
select count(distinct(uid)) from sessions;
> 108737
select count(*) from sessions where uid not in (select uid from users);
> 0
and just for completeness:
select count(*) from users where uid not in (select uid from sessions);
> 0
I have checked for nulls:
select count( * ) from sessions where uid is null;
> 0
select count( * ) from users where uid is null;
> 14
The schema is defined in sqlalchemy and includes a foreign key in the session table:
uid = Column(Integer, ForeignKey('users.uid', use_alter=True, name='fk_uid'))
This schema is a static dump for analytics purposes so there is no chance of concurrency issues...
Your third query does not do what you think it does.
The following query illustrates the problem:
SELECT 1 NOT IN (SELECT unnest(ARRAY[NULL]::int[]));
This returns NULL, because it can't say if 1 <> NULL.
So, in your query the where condition is always NULL, because users contains a NULL uid.
I recommend using EXCEPT do find the culprit in your sessions table.
SELECT uid from sessions EXCEPT SELECT uid from users;
I am using SQL Server. I have the following query:
select
convert(varchar(10), MAX(closedate), 101)
from
(select PSer.Signin_Date as closedate
from PSer
where ID = '12')
Note that the content within my from is more complicated than the simplified version I have.
I get a message saying
Invalid column name closedate
Make sure you're giving your subquery an alias.
from ( select PSer.Signin_Date as closedate from PSer where ID = '12') AS SOMENAME
use this:
select convert(varchar(10),MAX(t1.closedate),101)
from ( select PSer.Signin_Date as closedate from PSer where ID = '12') as t1
enjoy.