I am trying to replicate the following raw query:
SELECT r.id, r.name, e.id, e.title, e.start, e.end
FROM room r
LEFT JOIN LATERAL (
SELECT evt.id, evt.title, evt.start, evt.end
FROM event evt, calendar cal
WHERE
r.calendar_id=cal.id AND evt.calendar_id=cal.id AND evt.end>%(start)s
ORDER BY abs(extract(epoch from (evt.start - %(start)s)))
LIMIT 1
) e ON TRUE
WHERE r.company_id=%(company_id)s;
with the SQLAlchemy ORM:
start = datetime.datetime.now()
company_id = 6
event_include = session.query(
Event.id,
Event.title,
Event.start,
Event.end) \
.filter(
Room.calendar_id == Calendar.id,
Event.calendar_id == Calendar.id,
Event.end > start,
) \
.order_by(func.abs(func.extract('epoch', Event.start - start))) \
.limit(1) \
.subquery() \
.lateral()
query = session.query(Room.id, Room.name, event_include) \
.filter(Room.company_id == company_id)
Which produces the following SQL:
SELECT room.id AS room_id, room.name AS room_name, anon_1.id AS anon_1_id, anon_1.title AS anon_1_title, anon_1.start AS anon_1_start, anon_1."end" AS anon_1_end
FROM room, LATERAL (
SELECT event.id AS id, event.title AS title, event.start AS start, event."end" AS "end"
FROM event, calendar
WHERE room.calendar_id = calendar.id AND event.calendar_id = calendar.id AND event."end" > %(end_1)s ORDER BY abs(EXTRACT(epoch FROM event.start - %(start_1)s)
)
LIMIT %(param_1)s) AS anon_1
WHERE room.company_id = %(company_id_1)s
This returns all the rooms and their next calendar event, but only if there is a next calendar event available. It needs to be a LEFT JOIN LATERAL() ON TRUE, but I'm not sure how to do that.
Any help here would be great.
Use outerjoin with true expression
from sqlalchemy import true
query = session.query(Room.id, Room.name, event_include) \
.outerjoin(event_include, true()) \
.filter(Room.company_id == company_id)
Related
The Below Query is dropping records when i join 2 N1QL sub queries -
We are using couchbase and using N1QL queries.
Full Query -
select
t3.appName,
t3.uuid_proj as uuid,
t3.description,
t3.env,
t3.productStatus
from
( select
t1.uuid as uuid_proj ,
t1.appName as appName ,
t1.description as description,
t2.env as env,
t2.productStatus as productStatus
from
(
select
api_external.uuid ,
api_external.data.appName ,
api_external.data.description
from `api_external`
where type = 'partnerApp'
and data.companyId = '70a149da27cc425da86cba890bf5b143' )t1
join
(
select
api_external.data.env,
api_external.data.productStatus,
api_external.data.partnerAppId
from
`api_external`
where type = 'integration' )t2
on t1.uuid = t2.partnerAppId
) as t3
join (
select t4.uuid as uuid_agg , min(t5.env) as env
from
(select api_external.uuid from `api_external` where type = 'partnerApp' and data.companyId = '70a149da27cc425da86cba890bf5b143' )as t4 join
(select api_external.data.env, api_external.data.partnerAppId from `api_external` where type = 'integration' ) as t5
on t4.uuid = t5.partnerAppId
group by t4.uuid
) as t6
on
t3.uuid_proj = t6.uuid_agg and t3.env = t6.env
As you see it has 2 sub queries -
The below subquery gives 16 records -
select
t1.uuid as uuid_proj
from
(
select
api_external.uuid ,
api_external.data.appName ,
api_external.data.description
from `api_external`
where type = 'partnerApp'
and data.companyId = '70a149da27cc425da86cba890bf5b143' )t1
join
(
select
api_external.data.env,
api_external.data.productStatus,
api_external.data.partnerAppId
from
`api_external`
where type = 'integration' )t2
on t1.uuid = t2.partnerAppId
group by t1.uuid
Also the other subquery also gives 16 records -
select t4.uuid as uuid_agg , min(t5.env) as env
from
(select api_external.uuid from `api_external` where type = 'partnerApp' and data.companyId = '70a149da27cc425da86cba890bf5b143' )as t4 join
(select api_external.data.env, api_external.data.partnerAppId from `api_external` where type = 'integration' ) as t5
on t4.uuid = t5.partnerAppId
group by t4.uuid
By Logic join of both the queries on the same grain UUID must also give 16 records . But it gives only 1 .
What am i doing wrong Please help
The query uses many subqueries and hit the issue.
Try following simplified version
CREATE INDEX ix1 ON api_external(data.companyId, uuid, data.appName, data.description) WHERE type = "partnerApp";
CREATE INDEX ix2 ON api_external(data.partnerAppId, data.env, data.productStatus) WHERE type = "integration";
WITH ct3 AS (SELECT t1.uuid, t1.data.appName, t1.data.description,
t2.data.env, t2.data.productStatus
FROM api_external AS t1
JOIN api_external AS t2 ON t1.uuid = t2.data.partnerAppId
WHERE t1.type = "partnerApp"
AND t1.data.companyId = "70a149da27cc425da86cba890bf5b143"
AND t2.type = "integration"
AND t2.data.partnerAppId IS NOT NULL),
ct6 AS ( SELECT t4.uuid AS uuid_agg , MIN(t5.data.env) AS env
FROM api_external AS t4
JOIN api_external AS t5 ON t4.uuid = t5.data.partnerAppId
WHERE t4.type = "partnerApp"
AND t4.data.companyId = "70a149da27cc425da86cba890bf5b143"
AND t5.type = "integration"
AND t5.data.partnerAppId IS NOT NULL
GROUP BY t4.uuid)
SELECT t3.*
FROM ct3 AS t3
JOIN ct6 AS t6 ON t3.uuid = t6.uuid_agg and t3.env = t6.env;
If same results see following works . After JOIN get all the fields of results of MIN env record each group
SELECT m[1].*
FROM api_external AS t4
JOIN api_external AS t5 ON t4.uuid = t5.data.partnerAppId
WHERE t4.type = "partnerApp"
AND t4.data.companyId = '70a149da27cc425da86cba890bf5b143'
AND t5.type = "integration"
AND t5.data.partnerAppId IS NOT NULL
GROUP BY t4.uuid
LETTING m = MIN([t5.data.env, {t4.uuid, t4.data.appName, t4.data.description,
t5.data.env, t5.data.productStatus}]);
I am trying to get the max date by account from 3 different tables and view those dates side by side. I created a separate query for each table, merged the results with UNION ALL, and then wrapped all that in a PIVOT.
The first 2 sections in the link/pic below show what I have been able to accomplish and the 3rd section is what I would like to do.
Query results by step
How can I get the results from 2 of the tables to repeat? Is that possible?
--define var_ent_type = 'ACOM'
--define var_ent_id = '52766'
--define var_dict_id = 113
SELECT
*
FROM
(
SELECT
E.ENTITY_TYPE,
E.ENTITY_ID,
'PERF_SUMMARY' as "TableName",
PS.DICTIONARY_ID,
to_char(MAX(PS.END_EFFECTIVE_DATE), 'YYYY-MM-DD') as "MaxDate"
FROM
RULESDBO.ENTITY E
INNER JOIN PERFORMDBO.PERF_SUMMARY PS ON (PS.ENTITY_ID = E.ENTITY_ID)
WHERE
1=1
-- AND E.ENTITY_TYPE = '&var_ent_type'
-- AND E.ENTITY_ID = '&var_ent_id'
AND PS.DICTIONARY_ID >= 100
AND (E.ACTIVE_STATUS <> 'N' )--and E.TERMINATION_DATE is null )
GROUP BY
E.ENTITY_TYPE,
E.ENTITY_ID,
'PERF_SUMMARY',
PS.DICTIONARY_ID
union all
SELECT
E.ENTITY_TYPE,
E.ENTITY_ID,
'POSITION' as "TableName",
0 as DICTIONARY_ID,
to_char(MAX(H.EFFECTIVE_DATE), 'YYYY-MM-DD') as "MaxDate"
FROM
RULESDBO.ENTITY E
INNER JOIN HOLDINGDBO.POSITION H ON (H.ENTITY_ID = E.ENTITY_ID)
WHERE
1=1
-- AND E.ENTITY_TYPE = '&var_ent_type'
-- AND E.ENTITY_ID = '&var_ent_id'
AND (E.ACTIVE_STATUS <> 'N' )--and E.TERMINATION_DATE is null )
GROUP BY
E.ENTITY_TYPE,
E.ENTITY_ID,
'POSITION',
1
union all
SELECT
E.ENTITY_TYPE,
E.ENTITY_ID,
'CASH_ACTIVITY' as "TableName",
0 as DICTIONARY_ID,
to_char(MAX(C.EFFECTIVE_DATE), 'YYYY-MM-DD') as "MaxDate"
FROM
RULESDBO.ENTITY E
INNER JOIN CASHDBO.CASH_ACTIVITY C ON (C.ENTITY_ID = E.ENTITY_ID)
WHERE
1=1
-- AND E.ENTITY_TYPE = '&var_ent_type'
-- AND E.ENTITY_ID = '&var_ent_id'
AND (E.ACTIVE_STATUS <> 'N' )--and E.TERMINATION_DATE is null )
GROUP BY
E.ENTITY_TYPE,
E.ENTITY_ID,
'CASH_ACTIVITY',
1
--ORDER BY
-- 2,3, 4
)
PIVOT
(
MAX("MaxDate")
FOR "TableName"
IN ('CASH_ACTIVITY', 'PERF_SUMMARY','POSITION')
)
Everything is possible. You only need a window function to make the value repeat across rows w/o data.
--Assuming current query is QC
With QC as (
...
)
select code, account, grouping,
--cash,
first_value(cash) over (partition by code, account order by grouping asc rows unbounded preceding) as cash_repeat,
perf,
--pos,
first_value(pos) over (partition by code, account order by grouping asc rows unbounded preceding) as pos_repeat
from QC
;
See first_value() help here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/FIRST_VALUE.html#GUID-D454EC3F-370C-4C64-9B11-33FCB10D95EC
I want to recreate this query in spark sql
SELECT
[Id],
[Group],
[Name],
min([Date]) as MinDate,
max([Date]) as MaxDate
FROM recordTable
GROUP BY [Id],[Group],[Name]
)
SELECT
t.Id,
t.[Group],
t.[Name],
c.[Date],
(SELECT top 1 ScoreCount
from recordTable x
where x.[Date] <= c.[Days]
and x.[Group] = t.[Group]
and x.[Name] = t.[Name]
order by x.[Date] desc
) ScoreCount
FROM t
LEFT JOIN calendar c ON c.[Days] BETWEEN t.MinDate AND t.MaxDate
so I have
df = spark.sql("""
WITH t as (
SELECT
Id,
Group,
Name,
min(Date) as MinDate,
max(Date) as MaxDate
FROM recordTable
GROUP BY Id,Group,Name
)
SELECT
t.Id,
t.Group,
t.Name,
c.Date,
(SELECT ScoreCount
from recordTable x
where x.Date <= c.Days
and x.Group = t.Group
and x.Name = t.Name
order by x.Date desc LIMIT 1
) ScoreCount
FROM t
LEFT JOIN calendar c ON c.Days BETWEEN t.MinDate AND t.MaxDate
""")
But I'm getting an error when trying to limit 1 and using an order by clause. Any alternatives?
"Expressions referencing the outer query are not supported outside of where/having clauses"
I have a report which I would like to create prompts for both VENUE and DATE RANGE, and after having these prompts bring back the incorrect data, I have deleted both and tried to start again from the beginning.
To begin with, I have a Crystal Report based upon the below SQL:
To summarise the code - it uses a common table expression broken separating the query out into revenue, space and event, finally joining this together at the end.
I had to convert the data type of the 'booking date' to DATE, because the value is stored as 2018-01-01 12:00:00:00.
WITH Revenue as
(
SELECT EV200_EVENT_MASTER.EV200_EVT_ID as [Event],
Revenue =
SUM(Case
When Orddtl.ER101_PHASE = '1' and ORDDTL.ER101_COMPL_STS = 'N'
then ORDDTL.ER101_EXT_CHRG
When ORDDTL.ER101_PHASE = '5' and ORDDTL.ER101_COMPL_STS = 'N'
then ORDDTL.ER101_EXT_CHRG
Else 0
End),
SM.EV800_SPACE_DESC,
SM.EV800_SPACE_CODE
FROM EV200_EVENT_MASTER WITH (NOLOCK)
LEFT OUTER JOIN EV870_ACCT_MASTER PrimCoord WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = PrimCoord.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_1 = PrimCoord.EV870_ACCT_CODE -- Event manager
LEFT OUTER JOIN EV870_ACCT_MASTER FloorMgr WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = FloorMgr.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_2 = FloorMgr.EV870_ACCT_CODE -- Floor manager
LEFT OUTER JOIN EV870_ACCT_MASTER EventAccount WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EventAccount.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = EventAccount.EV870_ACCT_CODE -- The person who made the booking
LEFT OUTER JOIN EV215_EVT_TYPE WITH (NOLOCK) -- get the event type
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV215_EVT_TYPE.EV215_ORG_CODE
AND EV200_EVENT_MASTER.EV200_EVT_TYPE = EV215_EVT_TYPE.EV215_EVT_TYPE
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) -- this might not be necessary. Only if we want to show status = completed, etc
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
LEFT OUTER JOIN EV802_SPACE_BKD SPBK
ON SPBK.EV802_ORG_CODE = EV200_ORG_CODE AND SPBK.EV802_EVT_ID = EV200_EVT_ID
LEFT OUTER JOIN ER101_ACCT_ORDER_DTL ORDDTL
ON ORDDTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND ORDDTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID
LEFT OUTER JOIN EV800_SPACE_MASTER SM
ON Sm.EV800_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND SM.EV800_SPACE_CODE = SPBK.EV802_BKD_SPACE
WHERE EV200_EVENT_MASTER.EV200_ORG_CODE = '10'
AND EV200_EVENT_MASTER.EV200_EVT_STATUS >= 30 /* only confirmed bookings */
AND EV200_EVENT_MASTER.EV200_EVT_STATUS <= 52
AND Not(EV200_EVENT_MASTER.EV200_EVT_TYPE = 'GB') -- exclude group bookings
GROUP BY EV200_EVENT_MASTER.EV200_EVT_ID,SM.EV800_SPACE_DESC,EV800_SPACE_CODE
), eventdetails as
(
SELECT EV200_EVENT_MASTER.EV200_EVT_ID as [Event],
EV215_EVT_TYP_DESC as [Event Type],
EV200_event_master.EV200_EVT_DESC,
EV200_EVENT_MASTER.EV200_PLN_ATTEND,
--EventAccount.EV870_NAME AS [Account],
PrimCoord.EV870_FIRST_NAME + ' ' + PrimCoord.EV870_LAST_NAME AS [Event Manager],
--FloorMgr.EV870_FIRST_NAME + ' ' + FloorMgr.EV870_LAST_NAME AS [Floor Manager]
EV130_STATUS_MASTER.EV130_STATUS_DESC AS 'Status'
FROM EV200_EVENT_MASTER WITH (NOLOCK)
LEFT OUTER JOIN EV870_ACCT_MASTER PrimCoord WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = PrimCoord.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_1 = PrimCoord.EV870_ACCT_CODE -- Event manager
LEFT OUTER JOIN EV870_ACCT_MASTER FloorMgr WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = FloorMgr.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_2 = FloorMgr.EV870_ACCT_CODE -- Floor manager
LEFT OUTER JOIN EV870_ACCT_MASTER EventAccount WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EventAccount.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = EventAccount.EV870_ACCT_CODE -- The person who made the booking
LEFT OUTER JOIN EV215_EVT_TYPE WITH (NOLOCK) -- get the event type
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV215_EVT_TYPE.EV215_ORG_CODE
AND EV200_EVENT_MASTER.EV200_EVT_TYPE = EV215_EVT_TYPE.EV215_EVT_TYPE
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) -- this might not be necessary. Only if we want to show status = completed, etc
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
WHERE EV200_EVENT_MASTER.EV200_ORG_CODE = '10'
AND EV200_EVENT_MASTER.EV200_EVT_STATUS >= 30 /* only confirmed bookings */
AND EV200_EVENT_MASTER.EV200_EVT_STATUS <= 52
AND Not(EV200_EVENT_MASTER.EV200_EVT_TYPE = 'GB') -- exclude group bookings
), spacedetails as
(
SELECT distinct
SPDTL.EV803_BKD_SPACE,
SPDTL.EV803_EVT_ID,
SM.EV800_SPACE_DESC,
CONVERT(DATE,SPDTL.EV803_BKG_DATE) as bkg_date,
spdtl.EV803_START_TIME,
spdtl.EV803_END_TIME,
SPDTL.EV803_BKG_START_TIME,
SPDTL.EV803_BKG_END_TIME,
SPDTL.EV803_USAGE,
EV800_NOTE_1 AS [VENUE]
FROM EV803_SPACE_BKD_DTL SPDTL
INNER JOIN EV800_SPACE_MASTER SM ON SPDTL.EV803_BKD_SPACE = SM.EV800_SPACE_CODE
)
select distinct eventdetails.Event,
[Event Type],
eventdetails.EV200_EVT_DESC as 'Description',
eventdetails.EV200_PLN_ATTEND as 'PAX',
eventdetails.[Event Manager],
ss.EV800_SPACE_DESC as 'Space',
ss.bkg_date as 'Booking Date',
ss.VENUE,
eventdetails.Status,
/*spacedetails.
(CASE
WHEN spacedetails.EV803_USAGE = 'IN' THEN ''
WHEN spacedetails.EV803_USAGE = 'ET' THEN 'EVENT'
WHEN spacedetails.EV803_USAGE = 'OUT' THEN 'BUMP OUT'
END) as 'Booking Type',
*/
/*this doesnt work because it will be evaluated one row at a time, and one row will not satisfy in,et,out
CAST((CASE WHEN spacedetails.ev803_usage = 'IN' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Bump in Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'IN' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Bump in End Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'ET' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Event Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'ET' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Event End Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'OUT' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Bump Out Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'OUT' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Bump Out End Time',
*/
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'IN'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-in Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'IN'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-in End Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'ET'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Event Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'ET'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Event End Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'OUT'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-out Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'OUT'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-out End Time',
revenue.revenue
from eventdetails
inner join spacedetails ss on eventdetails.Event = ss.EV803_EVT_ID
inner join Revenue on ss.EV803_EVT_ID = revenue.Event and ss.EV803_BKD_SPACE = Revenue.EV800_SPACE_CODE
I created a Dynamic Parameter on the 'Booking date' setting allow for 'range values',
but when i try to run the report (it is deployed in Ungerboeck Event Management) I only get the prompt for a non range value.
I was able to resolve this issue.
In my original query, I referenced two datetime fields, and converted one to a date (shown below) but failed to explicitly convert the second.
FIRST > CONVERT(DATE,SPDTL.EV803_BKG_DATE) as bkg_date,
SECOND > ss.bkg_date as 'Booking Date',
After making the change to convert the second to date, the prompt worked as it should have.
So my sphinx.conf file contains something similar. Basically I am using delta index to make things quick.
source main
{
#...
sql_query_pre = SET NAMES utf8
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM photo
sql_query = \
SELECT p.id AS id, p.search AS search, COUNT(li.id) AS total_likes \
FROM `photo` p \
LEFT JOIN `like` li \
ON p.id = li.photo_id \
WHERE p.id <= ( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) \
GROUP BY \
p.id
#...
sql_query_info = SELECT * FROM photo WHERE id=$id
}
source delta : main
{
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT p.id AS id, p.search AS search, COUNT(li.id) AS total_likes \
FROM `photo` p \
LEFT JOIN `like` li \
ON p.id = li.photo_id \
WHERE p.id > ( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) \
GROUP BY \
p.id
}
And in the php when I retrieve data I also want to have some sort of sorting methods.
$s->SetSortMode(SPH_SORT_EXTENDED, '#relevance DESC, total_likes DESC, #id DESC');
$result = $s->Query($data['query'], "delta main");
Sorting was working fine when I had only main index. But now when I search with both indexes, results from the delta index is appended at the front. What I actually want is results from both indexes are fetched and then sorted according to preferences i.e. #relevance DESC, total_likes DESC, #id DESC in my case. That is total_likes should be given preference over id
Thanks #barryhunter for the solution. The solution was that in the delta index second sql_query_pre had to be overwritten.
sql_query_pre = SET NAMES utf8
sql_query_pre =
sql_query = \