sphinxSE Setselect - sphinx

Using SphinxApi , We have using following query for sphinxApi Setselect:
$cl->SetSelect ( "*, IF( IN(watching, 1) OR ordered=1, 1, 0) AS customFilter" );
Using SphinxQL:
select IF( IN(watching, 1) OR ordered=1, 1, 0) AS customFilter from indexname
Is that same thing how to using in SphinxSE ?
Any one can help me out?

SELECT * FROM indexname WHERE query = ';select=*, IF( IN(watching, 1) OR ordered=1, 1, 0) AS customFilter';
Of course customFilter must be defined as a column on the sphinx virtual table, if you want to see the value in your results

Related

criteria api sub-select with sub-select and custom fields

I'd like to rewrite following sql query to criteria api:
select * from demands d
where exists (
select * from (
select a.demandid,
min(coalesce(a.securitylevel, a.securitylevel, -1)) themin,
max(coalesce(a.securitylevel, -1)) themax
from allocations a
group by demandid)
where demandid = d.id
and (themin = -1 and themax = -1
or themax >= 5)
);
The problem is that I'm not able to figure out how to write select clause for sub-queries, my current implementation produces CompoundSelection however subQyer.select accepts only Expression (DemandSecLevel is custom POJO/DTO).
public static Specification<Demand> bySecurityLevel(
final Integer securityLevel) {
return (root, query, cb) -> {
final Subquery<DemandSecLevel> subQuery = query.subquery(DemandSecLevel.class);
final Root<AllocationEntry> allocationEntryRoot = subQuery.from(AllocationEntry.class);
subQuery.select(cb.construct(
DemandSecLevel.class,
allocationEntryRoot.get(AllocationEntry_.incidentDemandId),
cb.min(cb.coalesce(allocationEntryRoot.get(AllocationEntry_.securityLevel), -1))
.alias("minSecLevel"),
cb.max(cb.coalesce(allocationEntryRoot.get(AllocationEntry_.securityLevel), -1))
.alias("maxSecLevel")
));

Covert SQL query to TYPO3 query builder?

How to convert SQL query to TYPO3 query builder.
SELECT * FROM tableName ORDER BY (CASE WHEN DATE(dateColumn) < DATE(GETDATE()) THEN 1 ELSE 0
END) DESC, dateColumn ASC
enter link description here
Same functionality i need in typo3 query builder.
To get the sql query you posted, you can do it like following:
// little helper function to debug querybuilder,
// queries with parameter placeholders
function debugQuery(QueryBuilder $builder)
{
$preparedStatement = $builder->getSQL();
$parameters = $builder->getParameters();
$stringParams = [];
foreach ($parameters as $key => $parameter) {
$stringParams[':' . $key] = $parameter;
}
return strtr($preparedStatement, $stringParams);
}
// get querybuilder
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName('Default')
->createQueryBuilder();
// build query
$queryBuilder
->select('*')
->from('table')
->getConcreteQueryBuilder()->orderBy(
'(CASE WHEN DATE('
.$queryBuilder->quoteIdentifier('dateColumn')
.') < DATE(GETDATE()) THEN 1 ELSE 0 END)',
'DESC'
)
;
$queryBuilder->addOrderBy('dateColumn', 'ASC');
// build query
$sql = debugQuery($queryBuilder);
The generates following sql query:
SELECT
FROM `table`
ORDER BY
(CASE WHEN DATE(`dateColumn`) < DATE(GETDATE()) THEN 1 ELSE 0 END) DESC,
`dateColumn` ASC
Some note beside:
To my knowlage GETDATE() is not a valid mysql method, more MSSQL. Eventually you want CURRENT_DATE() instead.
edit #1
Not tested/run .. just created the sql string to match what you provided. So don't blame me if provided sql query is wrong itself.

How to create an Update statement from a subquery in TSQL

I need to update all records that match my criteria. But the Sql below is giving this error:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
-- Set MasterAccountId = NULL where there is no Receivable with equivalent BillingAccountId and TaskAccountId
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
WHERE EXISTS ( SELECT * FROM MasterAccount M
WHERE (ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
Basically, I need to update all records that return in that subquery.
Does any one know how to fix it?
Can you give a try on this. This is base from the repond of https://stackoverflow.com/users/40655/robin-day on this link How do I UPDATE from a SELECT in SQL Server?.
UPDATE
R
SET
R.MasterAccountId = NULL
FROM
Receivable R
INNER JOIN
MasterAccount M
ON
(ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
I don't think you are getting the said error in posted query May be somewhere else. Again in your EXISTS subquery, instead of saying select * ... it's always better to say WHERE EXISTS ( SELECT 1 FROM MasterAccount M
Also try using the JOIN version of this query instead like
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
JOIN MasterAccount M ON M.BillingAccountId = R.BillingAccountId
OR M.TaskAccountId = R.TaskAccountId
WHERE ISNULL(M.BillingAccountId, 0) > 0
OR ISNULL(M.TaskAccountId, 0) > 0;

Return values for X and Y where X-Y = Max(X-Y)

(SQL SERVER 2005)
I have a table of multiple products that relate to an ItemCode. I can establish the best saving using the query below (I think) but what I need to include are the RRP and SellingPrice fields for the combination that provides the best saving.
Apologies in advance this is probably a common issue but I can't find a solution that fits.
SELECT ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
FROM ItemCodePricingDetail
WHERE ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y')
AND (RRP > 0) AND ([SellingPrice] > 0)
GROUP BY ItemCode
Many Thanks
select * from ItemCodePricingDetail
JOIN
(
SELECT ItemCode, MAX(RRP - [SellingPrice]) AS BestSaving
FROM ItemCodePricingDetail
WHERE ([ProductGroup] = N'SHOES') AND ([Stock Flag] = N'Y')
AND (RRP > 0) AND ([SellingPrice] > 0)
GROUP BY ItemCode
) as t1 on ItemCodePricingDetail.ItemCode=t1.ItemCode
and RRP - [SellingPrice]= t1.BestSaving

FQL WHERE IN with Null feedback in the array

I'm trying the command below:
SELECT * FROM mytable WHERE field IN(1,2,3, NULL, 5)
and the result is {r1,r2,r3}
I want {r1,r2,r3,NULL,r4}, how can I do that?
Thank you!
You can't. Facebook automatically filters out NULL results from the rows. There is also no guarantee that the results will be in the same order as the arguments you passed in your IN criteria.
You also can't SELECT * in FQL. You have to individually select fields to return.
You'll need to do something like this:
THINGS_TO_FIND = array (1,2,3,NULL,5)
RESULT = FQL_URL + urlencode('SELECT field1, field2 FROM table WHERE field1 IN ' + THINGS_TO_FIND')
foreach (THING in THINGS_TO_FIND) {
foreach (ITEM in RESULT) {
if THING == ITEM->field1 then OUTPUT->field1 = ITEM->field2
}
}