is it possible with sphinx to sort a result like this mysql query?
order by lang='2' desc , time_popular desc
so get the latest documents with lang=2 in fist position.
I really need your help :)
Yes. Look at this page http://www.sphinxsearch.com/docs/current.html#sorting-modes and particularly SPH_SORT_EXTENDED.
Assuming you have attributes for them both:
sql_attr_uint = lang
sql_attr_timestamp = time_popular
Then using the API for example:
$cl->SetSortMode ( SPH_SORT_EXTENDED, "lang desc, time_popular desc" );
You could use filer along with order like
SELECT * FROM your_index where lang=2 order by lang desc, time_pupular desc;
Hope it helps.
Related
I have following table in cassandra:
CREATE TABLE article (
id text,
price int,
validFrom timestamp,
PRIMARY KEY (id, validFrom)
) WITH CLUSTERING ORDER BY (validFrom DESC);
With articles and historical price information (validFrom is a timestamp of new price). Article price changes often. I want to query for
Historic price for a particular article.
Last price for an article.
From my understanding, I can solve both problems with following query:
select id, price from article where id = X validFrom < Y limit 1;
This query uses article id as restriction, query uses the partition key. Since the clustering order is based on the validFrom timestamp in reversed order, cassandra can efficient perform this query.
Am I getting this right?
What is the best approach to delete old data (house-keeping). Let's assume, I want delete all articles with validFrom > 20150101 and validFrom < 20151231. Since I don't have a primary key, this would be inefficient, even if I use an index on validFrom, right? How can I achieve this?
You can use external tools for that:
Spark with Spark Cassandra Connector (even in the local mode). Code could look as following (note that I'm using validfrom as name, not validFrom, as it's not escaped in your schema):
import com.datastax.spark.connector._
val data = sc.cassandraTable("test", "article")
.where("validfrom >= '2020-07-28T11:50:00Z' AND validfrom < '2020-07-28T12:50:00Z'")
.select("id", "validfrom")
data.deleteFromCassandra("test", "article", keyColumns=SomeColumns("id", "validfrom"))
use DSBulk to do find the matching entries and output them into the file (output.csv in my case), and then perform their deletion:
bin/dsbulk unload -url output.csv \
-query "SELECT id, validfrom FROM test.article WHERE token(id) > :start AND token(id) <= :end AND validFrom >= '2020-07-28T11:50:00Z' AND validFrom < '2020-07-28T12:50:00Z' ALLOW FILTERING"
bin/dsbulk load -query "DELETE from test.article WHERE id = :id and validfrom = :validfrom" \
-url output.csv
To add to Alex Ott's answer, this comment of yours is incorrect:
This query uses article id as restriction, query uses the partition key. Since the clustering order is based on price, cassandra can efficient perform this query.
The rows are not ordered by price. They are ordered by validFrom in reverse-chronological order. Cheers!
Using Postgres 9.4
I have a posts table which relates to a users table. I'm querying for two users and 3 of their most recent posts.
SELECT
"users"."id" AS "id",
"posts"."id" AS "posts__id",
"posts"."created_at" AS "posts__created_at"
FROM (
SELECT * FROM accounts
WHERE TRUE
ORDER BY "id" ASC
LIMIT 2
) AS "users"
LEFT JOIN LATERAL (
SELECT * FROM posts
WHERE "users".id = posts.author_id
ORDER BY "created_at" DESC, "id" DESC
LIMIT 3
) AS "posts" ON "users".id = "posts".author_id
On mac, the order is as expected.
"2016-04-17 18:49:15.942"
"2016-04-15 03:29:31.212"
"2016-04-13 15:07:15.119"
I get descending order on created_at, which is a timestamptz. However, when run on my travis build, which is Ubuntu, the ordering is stable, but neither ascending nor descending....
"2016-04-15 03:29:31.212"
"2016-04-13 15:07:15.119"
"2016-04-17 18:49:15.942"
I made user to create the databases with the same LC_COLLATE = en_US.UTF-8 with no luck. Why on earth isn't the ordering working on travis?
To solve this, just add the order by statement under your existing statements above.
i.e.
SELECT
"users"."id" AS "id",
"posts"."id" AS "posts__id",
"posts"."created_at" AS "posts__created_at"
FROM (
SELECT * FROM accounts
WHERE TRUE
ORDER BY "id" ASC
LIMIT 2
) AS "users"
LEFT JOIN LATERAL (
SELECT * FROM posts
WHERE "users".id = posts.author_id
ORDER BY "created_at" DESC, "id" DESC
LIMIT 3
) AS "posts" ON "users".id = "posts".author_id
order by posts.created_at desc
The order of output on postgres (and many other dbms's) cannot be guaranteed without an order by statement.
While you do indeed have order by statements, they are within sub-queries, you need the order by on the outer query.
you may need to order the outer query too because the in join between the 2 inner queries, even when they are ordered, won't be guaranteed.
SELECT
"users"."id" AS "id",
"posts"."id" AS "posts__id",
"posts"."created_at" AS "posts__created_at"
FROM (
SELECT * FROM accounts
WHERE TRUE
ORDER BY "id" ASC
LIMIT 2
) AS "users"
LEFT JOIN LATERAL (
SELECT * FROM posts
WHERE "users".id = posts.author_id
ORDER BY "created_at" DESC, "id" DESC
LIMIT 3
) AS "posts" ON "users".id = "posts".author_id
order by "posts"."created_at" DESC
Because the actual sort order depends on both the order of id in the first table and the order of the created_at & id in the second one prior to joining them. This means the order of the first table can produce unexpected results when computing the selected values from the joined table.
To fix the sort order, you should sort the final result set by relevant columns as well.
I have just started a new job and am working with existing queries. As I am no expert on SQL I'm wondering if a date range such as 2015-8-1 through 2015-8-31 can be inserted into the query below. Any help offered is greatly appreciated.
SELECT
RANK() OVER (PARTITION BY DoctorFacility.ListName ORDER BY ApptSlot.Start)
as SlotNumber
, DoctorFacility.ListName as ProviderName
, ApptSlot.Start as ApptStartTime
, AppointmentsAlloc.Type as ApptType
INTO #TEMP
FROM CentricityPS.dbo.ApptSlot ApptSlot
INNER JOIN CentricityPS.dbo.AppointmentsAlloc AppointmentsAlloc
ON ApptSlot.ApptSlotId=AppointmentsAlloc.ApptSlotId
INNER JOIN CentricityPS.dbo.Schedule Schedule
ON ApptSlot.ScheduleId=Schedule.ScheduleId
INNER JOIN CentricityPS.dbo.DoctorFacility DoctorFacility
ON Schedule.DoctorResourceId=DoctorFacility.DoctorFacilityId
WHERE AppointmentsAlloc.Type IN
(
'Behavioral Health - 30'
,'Behavioral Health 45'
,'Established Patient - 15'
,'Established Patient - 20'
,'Fin Counsel - 30'
,'Gyn Visit - 15'
,'Pediatric Visit - 15'
)
AND ApptSlot.ListOrder=1
AND ApptSlot.Status IS NULL
AND ApptSlot.Start>= GETDATE()
ORDER BY DoctorFacility.ListName
SELECT
ProviderName
, ApptStartTime
, ApptType
FROM #TEMP
WHERE SlotNumber = 3
ORDER BY ProviderName
DROP TABLE #TEMP
There are two separate select queries here. It would be done the same way for either part of the query regardless. Just add it to your WHERE statement
AND ApptSlot.Start>= 2015-8-1
AND ApptSlot.Start<= 2015-8-31
Another syntax would be
AND ApptSlot.Start between 2015-8-1 and 2015-8-31
Ideally you want to pass those begin and start dates as variables so values would not be hard coded. It might look more like this.
AND ApptSlot.Start between #StartDate and #EndDate
I searched a lot about sorting elements by sum of votes (in another model), like I do in SQL here :
SELECT item.* FROM item
LEFT JOIN (
SELECT
vote.item,
SUM(vote.value) AS vote.rating
FROM vote
GROUP BY vote.item
) AS res ON item.id = vote.item
ORDER BY res.rating DESC
Is there a way to do it via waterline methods ?
I think you can't do the left join with simple waterline methods, but you can use the .query method to execute your raw SQL syntax.
Sails MySQL adapter makes sum('field') conflict with sort('field'). It will generate SQL query like:
SELECT SUM(table.field) AS field FROM table ORDER BY table.field;
But I want:
SELECT SUM(table.field) AS field FROM table ORDER BY field;
It same as:
SELECT SUM(table.field) AS f FROM table ORDER BY f;
My solution is using lodash.sortBy() to process results. https://lodash.com/docs/4.16.4#sortBy
UPDATE tracks
SET People_id_Reference = (SELECT People_id
FROM People
RIGHT JOIN top100
ON
People_name=top100.artist )
WHERE People_id_Reference IS NULL;
But I get Error like this:
ERROR 1242(21000): SUBQUERY RETURNS MORE THAN ONE ROW
Can some on help me with this. Thanks in advance
The simplest way would be to append a LIMIT 1 onto the end of the subquery to only force it to return a single row.
change your code like this :
SELECT *
FROM package_reviews
WHERE post_id = ANY (SELECT post_id
FROM wp_posts
WHERE post_author=1);
this link is very useful
http://dev.mysql.com/doc/refman/5.0/en/subquery-errors.html